Avoid unnecessarily updating .. in SAFSS_Rename
[openafs.git] / src / viced / afsfileprocs.c
index 31bdc85..7352a93 100644 (file)
@@ -420,7 +420,6 @@ CallPostamble(register struct rx_connection *aconn, afs_int32 ret,
     struct host *thost;
     struct client *tclient;
     int translate = 0;
-    int held;
 
     H_LOCK;
     tclient = h_FindClient_r(aconn);
@@ -430,23 +429,34 @@ CallPostamble(register struct rx_connection *aconn, afs_int32 ret,
     if (thost->hostFlags & HERRORTRANS)
        translate = 1;
     h_ReleaseClient_r(tclient);
-    held = h_Held_r(thost);
-    if (held)
-       h_Release_r(thost);
-    if (ahost && ahost != thost) {
-       char hoststr[16], hoststr2[16]; 
-       ViceLog(0, ("CallPostamble: ahost %s:%d (%x) != thost %s:%d (%x)\n",
-               afs_inet_ntoa_r(ahost->host, hoststr), ntohs(ahost->port),
-               ahost, 
-               afs_inet_ntoa_r(thost->host, hoststr2), ntohs(thost->port),
-               thost));
-       h_Release_r(ahost);
-    } else if (!ahost) {
-       char hoststr[16];       
-       ViceLog(0, ("CallPostamble: null ahost for thost %s:%d (%x)\n",
-               afs_inet_ntoa_r(thost->host, hoststr), ntohs(thost->port),
-               thost));
+
+    if (ahost) {
+           if (ahost != thost) {
+                   /* host/client recycle */
+                   char hoststr[16], hoststr2[16];
+                   ViceLog(0, ("CallPostamble: ahost %s:%d (%p) != thost "
+                               "%s:%d (%p)\n",
+                               afs_inet_ntoa_r(ahost->host, hoststr),
+                               ntohs(ahost->port),
+                               ahost,
+                               afs_inet_ntoa_r(thost->host, hoststr2),
+                               ntohs(thost->port),
+                               thost));
+           }
+           /* return the reference taken in CallPreamble */
+           h_Release_r(ahost);
+    } else {
+           char hoststr[16];
+           ViceLog(0, ("CallPostamble: null ahost for thost %s:%d (%p)\n",
+                       afs_inet_ntoa_r(thost->host, hoststr),
+                       ntohs(thost->port),
+                       thost));
     }
+
+    /* return the reference taken in local h_FindClient_r--h_ReleaseClient_r
+     * does not decrement refcount on client->host */
+    h_Release_r(thost);
+
  busyout:
     H_UNLOCK;
     return (translate ? sys_error_to_et(ret) : ret);
@@ -1045,8 +1055,8 @@ RXFetch_AccessList(Vnode * targetptr, Vnode * parentwhentargetnotdir,
 {
     char *eACL;                        /* External access list placeholder */
 
-    if (acl_Externalize
-       ((targetptr->disk.type ==
+    if (acl_Externalize_pr
+       (hpr_IdToName, (targetptr->disk.type ==
          vDirectory ? VVnodeACL(targetptr) :
          VVnodeACL(parentwhentargetnotdir)), &eACL) != 0) {
        return EIO;
@@ -1074,7 +1084,8 @@ RXStore_AccessList(Vnode * targetptr, struct AFSOpaque *AccessList)
 {
     struct acl_accessList *newACL;     /* PlaceHolder for new access list */
 
-    if (acl_Internalize(AccessList->AFSOpaque_val, &newACL) != 0)
+    if (acl_Internalize_pr(hpr_NameToId, AccessList->AFSOpaque_val, &newACL)
+       != 0)
        return (EINVAL);
     if ((newACL->size + 4) > VAclSize(targetptr))
        return (E2BIG);
@@ -1095,13 +1106,13 @@ RXStore_AccessList(Vnode * targetptr, struct AFSOpaque *AccessList)
 #define        COPYBUFFSIZE    8192
 #define MAXFSIZE (~(afs_fsize_t) 0)
 static int
-CopyOnWrite(Vnode * targetptr, Volume * volptr, afs_fsize_t off, afs_fsize_t len)
+CopyOnWrite(Vnode * targetptr, Volume * volptr, afs_foff_t off, afs_fsize_t len)
 {
     Inode ino, nearInode;
-    int rdlen;
-    int wrlen;
+    ssize_t rdlen;
+    ssize_t wrlen;
     register afs_fsize_t size;
-    register int length;
+    size_t length;
     char *buff;
     int rc;                    /* return code */
     IHandle_t *newH;           /* Use until finished copying, then cp to vnode. */
@@ -1169,7 +1180,7 @@ CopyOnWrite(Vnode * targetptr, Volume * volptr, afs_fsize_t off, afs_fsize_t len
            length = COPYBUFFSIZE;
            size -= COPYBUFFSIZE;
        } else {
-           length = (int)size;
+           length = size;
            size = 0;
        }
        rdlen = FDH_READ(targFdP, buff, length);
@@ -1205,10 +1216,14 @@ CopyOnWrite(Vnode * targetptr, Volume * volptr, afs_fsize_t off, afs_fsize_t len
                free(buff);
                return ENOSPC;
            } else {
+               /* length, rdlen, and wrlen may or may not be 64-bits wide;
+                * since we never do any I/O anywhere near 2^32 bytes at a
+                * time, just case to an unsigned int for printing */
+
                ViceLog(0,
                        ("CopyOnWrite failed: volume %u in partition %s  (tried reading %u, read %u, wrote %u, errno %u) volume needs salvage\n",
-                        V_id(volptr), volptr->partition->name, length, rdlen,
-                        wrlen, errno));
+                        V_id(volptr), volptr->partition->name, (unsigned)length, (unsigned)rdlen,
+                        (unsigned)wrlen, errno));
 #if defined(AFS_DEMAND_ATTACH_FS)
                ViceLog(0, ("CopyOnWrite failed: requesting salvage\n"));
 #else
@@ -1247,11 +1262,11 @@ CopyOnWrite(Vnode * targetptr, Volume * volptr, afs_fsize_t off, afs_fsize_t len
 }                              /*CopyOnWrite */
 
 static int
-CopyOnWrite2(FdHandle_t *targFdP, FdHandle_t *newFdP, afs_fsize_t off, afs_fsize_t size) {
+CopyOnWrite2(FdHandle_t *targFdP, FdHandle_t *newFdP, afs_foff_t off, afs_fsize_t size) {
     char *buff = (char *)malloc(COPYBUFFSIZE);
-    register int length;
-    int rdlen;
-    int wrlen;
+    size_t length;
+    ssize_t rdlen;
+    ssize_t wrlen;
     int rc;
 
     FDH_SEEK(targFdP, off, SEEK_SET);
@@ -1263,7 +1278,7 @@ CopyOnWrite2(FdHandle_t *targFdP, FdHandle_t *newFdP, afs_fsize_t off, afs_fsize
            length = COPYBUFFSIZE;
            size -= COPYBUFFSIZE;
        } else {
-           length = (int)size;
+           length = size;
            size = 0;
        }
        rdlen = FDH_READ(targFdP, buff, length);
@@ -2344,10 +2359,6 @@ SRXAFS_FetchData64(struct rx_call * acall, struct AFSFid * Fid, afs_int64 Pos,
     afs_sfsize_t tPos, tLen;
 
 #ifdef AFS_64BIT_ENV
-#ifndef AFS_LARGEFILE_ENV
-    if (Pos + Len > 0x7fffffff)
-       return EFBIG;
-#endif /* !AFS_LARGEFILE_ENV */
     tPos = (afs_sfsize_t) Pos;
     tLen = (afs_sfsize_t) Len;
 #else /* AFS_64BIT_ENV */
@@ -2631,7 +2642,7 @@ SRXAFS_BulkStatus(struct rx_call * acall, struct AFSCBFids * Fids,
                              &rights, &anyrights)))
            goto Bad_BulkStatus;
        /* set volume synchronization information, but only once per call */
-       if (i == nfiles)
+       if (i == 0)
            SetVolumeSync(Sync, volptr);
 
        /* Are we allowed to fetch Fid's status? */
@@ -2724,6 +2735,7 @@ SRXAFS_InlineBulkStatus(struct rx_call * acall, struct AFSCBFids * Fids,
     struct host *thost;
     struct client *t_client = NULL;    /* tmp ptr to client data */
     AFSFetchStatus *tstatus;
+    int VolSync_set = 0;
 #if FS_STATS_DETAILED
     struct fs_stats_opTimingData *opP; /* Ptr to this op's timing struct */
     struct timeval opStartTime, opStopTime;    /* Start/stop times for RPC op */
@@ -2766,6 +2778,11 @@ SRXAFS_InlineBulkStatus(struct rx_call * acall, struct AFSCBFids * Fids,
     }
     CallBacks->AFSCBs_len = nfiles;
 
+    /* Zero out return values to avoid leaking information on partial succes */
+    memset(OutStats->AFSBulkStats_val, 0, nfiles * sizeof(struct AFSFetchStatus));
+    memset(CallBacks->AFSCBs_val, 0, nfiles * sizeof(struct AFSCallBack));
+    memset(Sync, 0, sizeof(*Sync));
+
     if ((errorCode = CallPreamble(acall, ACTIVECALL, &tcon, &thost))) {
        goto Bad_InlineBulkStatus;
     }
@@ -2792,8 +2809,10 @@ SRXAFS_InlineBulkStatus(struct rx_call * acall, struct AFSCBFids * Fids,
        }
 
        /* set volume synchronization information, but only once per call */
-       if (i == nfiles)
+       if (!VolSync_set) {
            SetVolumeSync(Sync, volptr);
+           VolSync_set = 1;
+       }
 
        /* Are we allowed to fetch Fid's status? */
        if (targetptr->disk.type != vDirectory) {
@@ -3182,10 +3201,6 @@ SRXAFS_StoreData64(struct rx_call * acall, struct AFSFid * Fid,
     afs_fsize_t tFileLength;
 
 #ifdef AFS_64BIT_ENV
-#ifndef AFS_LARGEFILE_ENV
-    if (FileLength > 0x7fffffff)
-       return EFBIG;
-#endif /* !AFS_LARGEFILE_ENV */
     tPos = (afs_fsize_t) Pos;
     tLength = (afs_fsize_t) Length;
     tFileLength = (afs_fsize_t) FileLength;
@@ -3832,6 +3847,8 @@ SAFSS_Rename(struct rx_call *acall, struct AFSFid *OldDirFid, char *OldName,
     afs_int32 newanyrights;    /* rights for any user */
     int doDelete;              /* deleted the rename target (ref count now 0) */
     int code;
+    int updatefile = 0;                /* are we changing the renamed file? (we do this
+                                * if we need to update .. on a renamed dir) */
     struct client *t_client;   /* tmp ptr to client data */
     struct in_addr logHostAddr;        /* host ip holder for inet_ntoa */
     struct rx_connection *tcon = rx_ConnectionOf(acall);
@@ -4073,10 +4090,28 @@ SAFSS_Rename(struct rx_call *acall, struct AFSFid *OldDirFid, char *OldName,
            assert(errorCode == 0);
        }
     }
+
+    if (fileptr->disk.type == vDirectory) {
+       SetDirHandle(&filedir, fileptr);
+       if (oldvptr != newvptr) {
+           /* we always need to update .. if we've moving fileptr to a
+            * different directory */
+           updatefile = 1;
+       } else {
+           struct AFSFid unused;
+
+           code = Lookup(&filedir, "..", &unused);
+           if (code == ENOENT) {
+               /* only update .. if it doesn't already exist */
+               updatefile = 1;
+           }
+       }
+    }
+
     /* Do the CopyonWrite first before modifying anything else. Copying is
-     *  required because we may have to change entries for .. 
+     * required when we have to change entries for ..
      */
-    if ((fileptr->disk.type == vDirectory) && (fileptr->disk.cloned)) {
+    if (updatefile && (fileptr->disk.cloned)) {
        ViceLog(25, ("Rename : calling CopyOnWrite on  target dir\n"));
        if ((errorCode = CopyOnWrite(fileptr, volptr, 0, MAXFSIZE)))
            goto Bad_Rename;
@@ -4152,17 +4187,23 @@ SAFSS_Rename(struct rx_call *acall, struct AFSFid *OldDirFid, char *OldName,
     if (oldvptr == newvptr)
        oldvptr->disk.dataVersion--;    /* Since it was bumped by 2! */
 
-    fileptr->disk.parent = newvptr->vnodeNumber;
-    fileptr->changed_newTime = 1;      /* status change of moved file */
+    if (fileptr->disk.parent != newvptr->vnodeNumber) {
+       fileptr->disk.parent = newvptr->vnodeNumber;
+       fileptr->changed_newTime = 1;
+    }
 
-    /* if we are dealing with a rename of a directory */
-    if (fileptr->disk.type == vDirectory) {
+    /* if we are dealing with a rename of a directory, and we need to
+     * update the .. entry of that directory */
+    if (updatefile) {
        assert(!fileptr->disk.cloned);
-       SetDirHandle(&filedir, fileptr);
+
+       fileptr->changed_newTime = 1;   /* status change of moved file */
+
        /* fix .. to point to the correct place */
        Delete(&filedir, ".."); /* No assert--some directories may be bad */
        assert(Create(&filedir, "..", NewDirFid) == 0);
        fileptr->disk.dataVersion++;
+
        /* if the parent directories are different the link counts have to be   */
        /* changed due to .. in the renamed directory */
        if (oldvptr != newvptr) {
@@ -4197,8 +4238,10 @@ SAFSS_Rename(struct rx_call *acall, struct AFSFid *OldDirFid, char *OldName,
     BreakCallBack(client->host, NewDirFid, 0);
     if (oldvptr != newvptr) {
        BreakCallBack(client->host, OldDirFid, 0);
-       if (fileptr->disk.type == vDirectory)   /* if a dir moved, .. changed */
-           BreakCallBack(client->host, &fileFid, 0);
+    }
+    if (updatefile) {
+       /* if a dir moved, .. changed */
+       BreakCallBack(client->host, &fileFid, 0);
     }
     if (newfileptr) {
        /* Note:  it is not necessary to break the callback */
@@ -4306,7 +4349,8 @@ SAFSS_Symlink(struct rx_call *acall, struct AFSFid *DirFid, char *Name,
     Vnode *targetptr = 0;      /* vnode of the new link */
     Vnode *parentwhentargetnotdir = 0; /* parent for use in SetAccessList */
     Error errorCode = 0;               /* error code */
-    int len, code = 0;
+    afs_sfsize_t len;
+    int code = 0;
     DirHandle dir;             /* Handle for dir package I/O */
     Volume *volptr = 0;                /* pointer to the volume header */
     struct client *client = 0; /* pointer to client structure */
@@ -4407,7 +4451,7 @@ SAFSS_Symlink(struct rx_call *acall, struct AFSFid *DirFid, char *Name,
     len = strlen((char *) LinkContents);
     code = (len == FDH_WRITE(fdP, (char *) LinkContents, len)) ? 0 : VDISKFULL;
     if (code) 
-       ViceLog(0, ("SAFSS_Symlink FDH_WRITE failed for len=%d, Fid=%u.%d.%d\n", len, OutFid->Volume, OutFid->Vnode, OutFid->Unique));
+       ViceLog(0, ("SAFSS_Symlink FDH_WRITE failed for len=%d, Fid=%u.%d.%d\n", (int)len, OutFid->Volume, OutFid->Vnode, OutFid->Unique));
     FDH_CLOSE(fdP);
     /*
      * Set up and return modified status for the parent dir and new symlink
@@ -6273,7 +6317,7 @@ SRXAFS_GetCapabilities(struct rx_call * acall, Capabilities * capabilities)
     dataBytes = 1 * sizeof(afs_int32);
     dataBuffP = (afs_uint32 *) malloc(dataBytes);
     dataBuffP[0] = VICED_CAPABILITY_ERRORTRANS | VICED_CAPABILITY_WRITELOCKACL;
-#if defined(AFS_64BIT_ENV) && defined(AFS_LARGEFILE_ENV)
+#if defined(AFS_64BIT_ENV)
     dataBuffP[0] |= VICED_CAPABILITY_64BITFILES;
 #endif
     if (saneacls)
@@ -6967,7 +7011,6 @@ FetchData_RXStyle(Volume * volptr, Vnode * targetptr,
     )
 {
     struct timeval StartTime, StopTime;        /* used to calculate file  transfer rates */
-    Error errorCode = 0;               /* Returned error code to caller */
     IHandle_t *ihP;
     FdHandle_t *fdP;
 #ifdef AFS_NT40_ENV
@@ -7049,14 +7092,15 @@ FetchData_RXStyle(Volume * volptr, Vnode * targetptr,
     tbuffer = AllocSendBuffer();
 #endif /* AFS_NT40_ENV */
     while (Len > 0) {
-       int wlen;
+       size_t wlen;
+       ssize_t nBytes;
        if (Len > optSize)
            wlen = optSize;
        else
-           wlen = (int)Len;
+           wlen = Len;
 #ifdef AFS_NT40_ENV
-       errorCode = FDH_READ(fdP, tbuffer, wlen);
-       if (errorCode != wlen) {
+       nBytes = FDH_READ(fdP, tbuffer, wlen);
+       if (nBytes != wlen) {
            FDH_CLOSE(fdP);
            FreeSendBuffer((struct afs_buffer *)tbuffer);
            VTakeOffline(volptr);
@@ -7064,32 +7108,32 @@ FetchData_RXStyle(Volume * volptr, Vnode * targetptr,
                        volptr->hashid));
            return EIO;
        }
-       errorCode = rx_Write(Call, tbuffer, wlen);
+       nBytes = rx_Write(Call, tbuffer, wlen);
 #else /* AFS_NT40_ENV */
-       errorCode = rx_WritevAlloc(Call, tiov, &tnio, RX_MAXIOVECS, wlen);
-       if (errorCode <= 0) {
+       nBytes = rx_WritevAlloc(Call, tiov, &tnio, RX_MAXIOVECS, wlen);
+       if (nBytes <= 0) {
            FDH_CLOSE(fdP);
            return EIO;
        }
-       wlen = errorCode;
-       errorCode = FDH_READV(fdP, tiov, tnio);
-       if (errorCode != wlen) {
+       wlen = nBytes;
+       nBytes = FDH_READV(fdP, tiov, tnio);
+       if (nBytes != wlen) {
            FDH_CLOSE(fdP);
            VTakeOffline(volptr);
            ViceLog(0, ("Volume %u now offline, must be salvaged.\n",
                        volptr->hashid));
            return EIO;
        }
-       errorCode = rx_Writev(Call, tiov, tnio, wlen);
+       nBytes = rx_Writev(Call, tiov, tnio, wlen);
 #endif /* AFS_NT40_ENV */
 #if FS_STATS_DETAILED
        /*
         * Bump the number of bytes actually sent by the number from this
         * latest iteration
         */
-       (*a_bytesFetchedP) += errorCode;
+       (*a_bytesFetchedP) += nBytes;
 #endif /* FS_STATS_DETAILED */
-       if (errorCode != wlen) {
+       if (nBytes != wlen) {
            FDH_CLOSE(fdP);
 #ifdef AFS_NT40_ENV
            FreeSendBuffer((struct afs_buffer *)tbuffer);
@@ -7210,6 +7254,7 @@ StoreData_RXStyle(Volume * volptr, Vnode * targetptr, struct AFSFid * Fid,
     afs_sfsize_t adjustSize;   /* bytes to call VAdjust... with */
     int linkCount = 0;         /* link count on inode */
     afs_fsize_t CoW_off, CoW_len;
+    ssize_t nBytes;
     FdHandle_t *fdP, *origfdP = NULL;
     struct in_addr logHostAddr;        /* host ip holder for inet_ntoa */
 
@@ -7286,8 +7331,11 @@ StoreData_RXStyle(Volume * volptr, Vnode * targetptr, struct AFSFid * Fid,
            if (Pos == 0) CopyOnWrite_off0++;
            if (CoW_len > CopyOnWrite_maxsize) CopyOnWrite_maxsize = CoW_len;
 
-           ViceLog(1, ("StoreData : calling CopyOnWrite on vnode %lu.%lu (%s) off 0x%llx size 0x%llx\n",
-                       V_id(volptr), targetptr->vnodeNumber, V_name(volptr), 0, Pos));
+           ViceLog(1, ("StoreData : calling CopyOnWrite on vnode %u.%u (%s) "
+                       "off 0x0 size 0x%llx\n",
+                       afs_printable_VolumeId_u(V_id(volptr)),
+                       afs_printable_VnodeId_u(targetptr->vnodeNumber),
+                       V_name(volptr), Pos));
            if ((errorCode = CopyOnWrite(targetptr, volptr, 0, Pos))) {
                ViceLog(25, ("StoreData : CopyOnWrite failed\n"));
                volptr->partition->flags &= ~PART_DONTUPDATE;
@@ -7368,9 +7416,11 @@ StoreData_RXStyle(Volume * volptr, Vnode * targetptr, struct AFSFid * Fid,
        /* Set the file's length; we've already done an lseek to the right
         * spot above.
         */
-       errorCode = FDH_WRITE(fdP, &tlen, 1);
-       if (errorCode != 1)
+       nBytes = FDH_WRITE(fdP, &tlen, 1);
+       if (nBytes != 1) {
+           errorCode = -1;
            goto done;
+       }
        errorCode = FDH_TRUNC(fdP, Pos);
     } else {
        /* have some data to copy */
@@ -7393,20 +7443,20 @@ StoreData_RXStyle(Volume * volptr, Vnode * targetptr, struct AFSFid * Fid,
 #else /* AFS_NT40_ENV */
            errorCode = rx_Readv(Call, tiov, &tnio, RX_MAXIOVECS, rlen);
 #endif /* AFS_NT40_ENV */
-#if FS_STATS_DETAILED
-           (*a_bytesStoredP) += errorCode;
-#endif /* FS_STATS_DETAILED */
            if (errorCode <= 0) {
                errorCode = -32;
                break;
            }
+#if FS_STATS_DETAILED
+           (*a_bytesStoredP) += errorCode;
+#endif /* FS_STATS_DETAILED */
            rlen = errorCode;
 #ifdef AFS_NT40_ENV
-           errorCode = FDH_WRITE(fdP, tbuffer, rlen);
+           nBytes = FDH_WRITE(fdP, tbuffer, rlen);
 #else /* AFS_NT40_ENV */
-           errorCode = FDH_WRITEV(fdP, tiov, tnio);
+           nBytes = FDH_WRITEV(fdP, tiov, tnio);
 #endif /* AFS_NT40_ENV */
-           if (errorCode != rlen) {
+           if (nBytes != rlen) {
                errorCode = VDISKFULL;
                break;
            }
@@ -7441,8 +7491,11 @@ StoreData_RXStyle(Volume * volptr, Vnode * targetptr, struct AFSFid * Fid,
     if (origfdP) {                                     /* finish CopyOnWrite */
        if ( (CoW_off = Pos + Length) < NewLength) {
            errorCode = CopyOnWrite2(origfdP, fdP, CoW_off, CoW_len = NewLength - CoW_off);
-           ViceLog(1, ("StoreData : CopyOnWrite2 on vnode %lu.%lu (%s) off 0x%llx size 0x%llx returns %d\n",
-                        V_id(volptr), targetptr->vnodeNumber, V_name(volptr), CoW_off, CoW_len, errorCode));
+           ViceLog(1, ("StoreData : CopyOnWrite2 on vnode %u.%u (%s) "
+                       "off 0x%llx size 0x%llx returns %d\n",
+                        afs_printable_VolumeId_u(V_id(volptr)),
+                       afs_printable_VnodeId_u(targetptr->vnodeNumber),
+                       V_name(volptr), CoW_off, CoW_len, errorCode));
        }
        FDH_CLOSE(origfdP);
     }