]> arthur.barton.de Git - netatalk.git/commitdiff
factorize lock test (fewer fcntl calls)
authordidg <didg>
Wed, 21 Oct 2009 13:28:17 +0000 (13:28 +0000)
committerdidg <didg>
Wed, 21 Oct 2009 13:28:17 +0000 (13:28 +0000)
include/atalk/adouble.h
libatalk/adouble/ad_lock.c
libatalk/adouble/ad_open.c

index 140ae32f2f7cec7533b2da370f0791bcb2644012..685c548c5f8f5de0d1099da799a29db0a7be99f4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: adouble.h,v 1.46 2009-10-15 12:06:07 franklahm Exp $
+ * $Id: adouble.h,v 1.47 2009-10-21 13:28:17 didg Exp $
  * Copyright (c) 1990,1991 Regents of The University of Michigan.
  * All Rights Reserved.
  *
@@ -447,6 +447,8 @@ extern int ad_fcntl_tmplock (struct adouble *, const u_int32_t /*eid*/,
                                  const int /*type*/, const off_t /*offset*/,
                                  const off_t /*len*/, const int /*user*/);
 extern int ad_testlock      (struct adouble * /*adp*/, int /*eid*/, off_t /*off*/);
+
+extern u_int16_t ad_openforks (struct adouble * /*adp*/, u_int16_t);
 extern int ad_excl_lock     (struct adouble * /*adp*/, const u_int32_t /*eid*/);
 
 #define ad_lock ad_fcntl_lock
index df529493f7d59140a8bcf3191c5611bec93c79c2..bbb5e3d474a8c7040649b0f0b5f179aea4c18a21 100644 (file)
@@ -1,5 +1,5 @@
 /* 
- * $Id: ad_lock.c,v 1.16 2009-10-21 10:49:36 didg Exp $
+ * $Id: ad_lock.c,v 1.17 2009-10-21 13:28:17 didg Exp $
  *
  * Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
  * All Rights Reserved. See COPYRIGHT for more information.
@@ -194,10 +194,10 @@ static int adf_findxlock(struct ad_fd *ad,
  *       2) if the header file doesn't exist, we stick the locks
  *          in the locations specified by AD_FILELOCK_RD/WR.
  */
-#define LOCK_RSRC_RD (0)
-#define LOCK_RSRC_WR (1)
-#define LOCK_DATA_RD (2)
-#define LOCK_DATA_WR (3)
+#define LOCK_DATA_WR (0)
+#define LOCK_DATA_RD (1)
+#define LOCK_RSRC_WR (2)
+#define LOCK_RSRC_RD (3)
 
 #define LOCK_RSRC_DRD (4)
 #define LOCK_RSRC_DWR (5)
@@ -394,41 +394,26 @@ fcntl_lock_err:
    error          ==> -1
       
 */
-int ad_testlock(struct adouble *ad, int eid, const off_t off)
+static int testlock(struct ad_fd *adf, off_t off, off_t len)
 {
   struct flock lock;
-  struct ad_fd *adf;
   adf_lock_t *plock;
   int i;
 
   lock.l_start = off;
-  if (eid == ADEID_DFORK) {
-    adf = &ad->ad_data_fork;
-    if ((ad_meta_fileno(ad) != -1)) {
-       adf = ad->ad_md;
-       lock.l_start = df2off(off);
-    }
-  } 
-  else { /* rfork */
-    if ((ad_meta_fileno(ad) == -1)) {
-        /* there's no resource fork. return no lock */
-        return 0;
-    }
-    adf = ad->ad_md;
-    lock.l_start = hf2off(off);
-  }
 
   plock = adf->adf_lock;
-  /* Do we have a lock? */
   lock.l_whence = SEEK_SET;
-  lock.l_len = 1;
+  lock.l_len = len;
+
+  /* Do we have a lock? */
   for (i = 0; i < adf->adf_lockcount; i++) {
     if (OVERLAP(lock.l_start, 1, plock[i].lock.l_start, plock[i].lock.l_len)) 
         return 1;   /* */
   }
   /* Does another process have a lock? 
   */
-  lock.l_type = (adf->adf_flags & O_RDWR) ?F_WRLCK : F_RDLCK;                                           
+  lock.l_type = (adf->adf_flags & O_RDWR) ?F_WRLCK : F_RDLCK;
 
   if (fcntl(adf->adf_fd, F_GETLK, &lock) < 0) {
       /* is that kind of error possible ?*/
@@ -441,6 +426,92 @@ int ad_testlock(struct adouble *ad, int eid, const off_t off)
   return 1;
 }
 
+/* --------------- */
+int ad_testlock(struct adouble *ad, int eid, const off_t off)
+{
+  struct ad_fd *adf;
+  off_t      lock_offset;
+
+  lock_offset = off;
+  if (eid == ADEID_DFORK) {
+    adf = &ad->ad_data_fork;
+    if (ad_meta_fileno(ad) != -1) {
+       adf = ad->ad_md;
+       lock_offset = df2off(off);
+    }
+  } 
+  else { /* rfork */
+    if (ad_meta_fileno(ad) == -1) {
+        /* there's no resource fork. return no lock */
+        return 0;
+    }
+    adf = ad->ad_md;
+    lock_offset = hf2off(off);
+  }
+  return testlock(adf, lock_offset, 1);
+}
+
+/* -------------------------
+   return if a file is open by another process.
+   Optimized for the common case:
+   - there's no locks held by another process (clients)
+   - or we already know the answer and don't need to test.
+*/
+u_int16_t ad_openforks(struct adouble *ad, u_int16_t attrbits)
+{
+  u_int16_t ret = 0;
+  struct ad_fd *adf;
+  off_t off;
+
+  if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
+      off_t len;
+      /* XXX know the locks layout: 
+         AD_FILELOCK_OPEN_WR is first 
+         and use it for merging requests
+      */
+      if (ad_meta_fileno(ad) != -1) {
+          /* there's a resource fork test the four bytes for
+           * data RW/RD and fork RW/RD locks in one request
+          */
+         adf = ad->ad_md;
+         off = LOCK_DATA_WR;
+         len = 4;
+      }
+      else {
+          /* no resource fork, only data RD/RW may exist */
+          adf = &ad->ad_data_fork;
+          off = AD_FILELOCK_OPEN_WR;
+          len = 2;
+      }
+      if (!testlock(adf, off, len))
+          return ret;
+  }
+  /* either there's a lock or we already know one 
+     fork is open
+  */
+  if (!(attrbits & ATTRBIT_DOPEN)) {
+      if (ad_meta_fileno(ad) != -1) {
+         adf = ad->ad_md;
+         off = LOCK_DATA_WR;
+      }
+      else {
+          adf = &ad->ad_data_fork;
+          off = AD_FILELOCK_OPEN_WR;
+      }
+      ret = testlock(adf, off, 2) > 0? ATTRBIT_DOPEN : 0;
+  }
+
+  if (!(attrbits & ATTRBIT_ROPEN)) {
+      if (ad_meta_fileno(ad) != -1) {
+         adf = ad->ad_md;
+          off = LOCK_RSRC_WR;
+          ret |= testlock(adf, off, 2) > 0? ATTRBIT_ROPEN : 0;
+      }
+  }
+
+  return ret;
+}
+
 /* -------------------------
 */
 int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int locktype,
index 34a96f1410bf2aa90304b7b0b120c0147b793cf5..d990824d759fafdf857a7a341a80c61fe99b127c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: ad_open.c,v 1.50 2009-10-18 06:16:05 didg Exp $
+ * $Id: ad_open.c,v 1.51 2009-10-21 13:28:17 didg Exp $
  *
  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
  * Copyright (c) 1990,1991 Regents of The University of Michigan.
@@ -1476,25 +1476,13 @@ int ad_metadata(const char *name, int flags, struct adouble *adp)
     }
 
     if (!ret && (ADFLAGS_OPENFORKS & flags)) {
-        u_int16_t attrbits = adp->ad_open_forks;
-
         /*
           we need to check if the file is open by another process.
           it's slow so we only do it if we have to:
           - it's requested.
           - we don't already have the answer!
         */
-
-        if (!(attrbits & ATTRBIT_ROPEN)) {
-            attrbits |= ad_testlock(adp, ADEID_RFORK, AD_FILELOCK_OPEN_RD) > 0? ATTRBIT_ROPEN : 0;
-            attrbits |= ad_testlock(adp, ADEID_RFORK, AD_FILELOCK_OPEN_WR) > 0? ATTRBIT_ROPEN : 0;
-        }
-
-        if (!(attrbits & ATTRBIT_DOPEN)) {
-            attrbits |= ad_testlock(adp, ADEID_DFORK, AD_FILELOCK_OPEN_RD) > 0? ATTRBIT_DOPEN : 0;
-            attrbits |= ad_testlock(adp, ADEID_DFORK, AD_FILELOCK_OPEN_WR) > 0? ATTRBIT_DOPEN : 0;
-        }
-        adp->ad_open_forks = attrbits;
+        adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
     }
     return ret;
 }