Index: src/client/sysint/sys-remove.sm
===================================================================
RCS file: /anoncvs/pvfs2/src/client/sysint/sys-remove.sm,v
retrieving revision 1.93
diff -a -u -r1.93 sys-remove.sm
--- src/client/sysint/sys-remove.sm	20 Dec 2005 00:08:25 -0000	1.93
+++ src/client/sysint/sys-remove.sm	20 Dec 2005 19:13:13 -0000
@@ -99,7 +99,8 @@
     state init
     {
         run remove_init;
-        default => rmdirent_setup_msgpair;
+        success => rmdirent_setup_msgpair;
+        default => terminate;
     }
 
     state rmdirent_setup_msgpair
@@ -269,9 +270,47 @@
 static int remove_init(PINT_client_sm *sm_p,
 		       job_status_s *js_p)
 {
+    int ret;
+    PVFS_object_ref object_ref;
     gossip_debug(GOSSIP_CLIENT_DEBUG, "remove state: init\n");
 
     assert(js_p->error_code == 0);
+
+    /* first check the lookup and attr caches for the object.
+     * If we get hits, and if the type of the object is a directory,
+     * and the directory isn't empty (dirent_count != 0), then we
+     * can just return ENOTEMPTY right away.
+     */
+
+    ret = PINT_ncache_lookup(
+        sm_p->u.remove.object_name, 
+        PVFS2_LOOKUP_LINK_NO_FOLLOW,
+        sm_p->parent_ref, &object_ref);
+    if(ret == 0)
+    {
+        PVFS_object_attr attr;
+        int status, size_status;
+        PVFS_size size;
+
+        /* the name is cached, now look for the attribute */
+        ret = PINT_acache_get_cached_entry(
+            object_ref, &attr, &status, &size, &size_status);
+        if(ret == 0 && status == 0)
+        {
+            if(attr.objtype == PVFS_TYPE_DIRECTORY &&
+               attr.mask & PVFS_ATTR_DIR_DIRENT_COUNT &&
+               attr.u.dir.dirent_count != 0)
+            {
+                /* if we're removing a directory, and the
+                 * cache tells us its not empty, then we
+                 * can return ENOTEMPTY right away.
+                 */
+                js_p->error_code = -PVFS_ENOTEMPTY;
+                return 1;
+            }
+        }
+    }        
+
     return 1;
 }
 
@@ -450,9 +489,15 @@
 
     /* NOTE: acache is invalidated by remove_helper now */
 
-    PINT_ncache_remove(sm_p->u.remove.object_name,
-                       0 /* PVFS2_LOOKUP_LINK_NO_FOLLOW */,
-                       sm_p->parent_ref, NULL);
+    /* But if the directory wasn't empty and we got a -PVF_ENOTEMPTY, then
+     * we don't need to remove the name from the cache.
+     */
+    if(sm_p->u.remove.stored_error_code != -PVFS_ENOTEMPTY)
+    {
+        PINT_ncache_remove(sm_p->u.remove.object_name,
+                           PVFS2_LOOKUP_LINK_NO_FOLLOW,
+                           sm_p->parent_ref, NULL);
+    }
 
     if (sm_p->msgarray && (sm_p->msgarray != &sm_p->msgpair))
     {
