c4d0bac56a5a80bcc7f0778155a1ea87aa76a90e
[safe/jmp/linux-2.6] / fs / xfs / xfs_rename.c
1 /*
2  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir2.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_attr_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h"
36 #include "xfs_bmap.h"
37 #include "xfs_error.h"
38 #include "xfs_quota.h"
39 #include "xfs_utils.h"
40 #include "xfs_trans_space.h"
41 #include "xfs_vnodeops.h"
42
43
44 /*
45  * Given an array of up to 4 inode pointers, unlock the pointed to inodes.
46  * If there are fewer than 4 entries in the array, the empty entries will
47  * be at the end and will have NULL pointers in them.
48  */
49 STATIC void
50 xfs_rename_unlock4(
51         xfs_inode_t     **i_tab,
52         uint            lock_mode)
53 {
54         int     i;
55
56         xfs_iunlock(i_tab[0], lock_mode);
57         for (i = 1; i < 4; i++) {
58                 if (i_tab[i] == NULL) {
59                         break;
60                 }
61                 /*
62                  * Watch out for duplicate entries in the table.
63                  */
64                 if (i_tab[i] != i_tab[i-1]) {
65                         xfs_iunlock(i_tab[i], lock_mode);
66                 }
67         }
68 }
69
70 #ifdef DEBUG
71 int xfs_rename_skip, xfs_rename_nskip;
72 #endif
73
74 /*
75  * The following routine will acquire the locks required for a rename
76  * operation. The code understands the semantics of renames and will
77  * validate that name1 exists under dp1 & that name2 may or may not
78  * exist under dp2.
79  *
80  * We are renaming dp1/name1 to dp2/name2.
81  *
82  * Return ENOENT if dp1 does not exist, other lookup errors, or 0 for success.
83  */
84 STATIC int
85 xfs_lock_for_rename(
86         xfs_inode_t     *dp1,   /* old (source) directory inode */
87         xfs_inode_t     *dp2,   /* new (target) directory inode */
88         bhv_vname_t     *vname1,/* old entry name */
89         bhv_vname_t     *vname2,/* new entry name */
90         xfs_inode_t     **ipp1, /* inode of old entry */
91         xfs_inode_t     **ipp2, /* inode of new entry, if it
92                                    already exists, NULL otherwise. */
93         xfs_inode_t     **i_tab,/* array of inode returned, sorted */
94         int             *num_inodes)  /* number of inodes in array */
95 {
96         xfs_inode_t             *ip1 = VNAME_TO_INODE(vname1);
97         xfs_inode_t             *ip2, *temp;
98         xfs_ino_t               inum1, inum2;
99         int                     error;
100         int                     i, j;
101         uint                    lock_mode;
102         int                     diff_dirs = (dp1 != dp2);
103
104         ip2 = NULL;
105
106         /*
107          * First, find out the current inums of the entries so that we
108          * can determine the initial locking order.  We'll have to
109          * sanity check stuff after all the locks have been acquired
110          * to see if we still have the right inodes, directories, etc.
111          */
112         lock_mode = xfs_ilock_map_shared(dp1);
113         IHOLD(ip1);
114         xfs_itrace_ref(ip1);
115
116         inum1 = ip1->i_ino;
117
118
119         /*
120          * Unlock dp1 and lock dp2 if they are different.
121          */
122
123         if (diff_dirs) {
124                 xfs_iunlock_map_shared(dp1, lock_mode);
125                 lock_mode = xfs_ilock_map_shared(dp2);
126         }
127
128         error = xfs_dir_lookup_int(dp2, lock_mode, vname2, &inum2, &ip2);
129         if (error == ENOENT) {          /* target does not need to exist. */
130                 inum2 = 0;
131         } else if (error) {
132                 /*
133                  * If dp2 and dp1 are the same, the next line unlocks dp1.
134                  * Got it?
135                  */
136                 xfs_iunlock_map_shared(dp2, lock_mode);
137                 IRELE (ip1);
138                 return error;
139         } else {
140                 xfs_itrace_ref(ip2);
141         }
142
143         /*
144          * i_tab contains a list of pointers to inodes.  We initialize
145          * the table here & we'll sort it.  We will then use it to
146          * order the acquisition of the inode locks.
147          *
148          * Note that the table may contain duplicates.  e.g., dp1 == dp2.
149          */
150         i_tab[0] = dp1;
151         i_tab[1] = dp2;
152         i_tab[2] = ip1;
153         if (inum2 == 0) {
154                 *num_inodes = 3;
155                 i_tab[3] = NULL;
156         } else {
157                 *num_inodes = 4;
158                 i_tab[3] = ip2;
159         }
160
161         /*
162          * Sort the elements via bubble sort.  (Remember, there are at
163          * most 4 elements to sort, so this is adequate.)
164          */
165         for (i=0; i < *num_inodes; i++) {
166                 for (j=1; j < *num_inodes; j++) {
167                         if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
168                                 temp = i_tab[j];
169                                 i_tab[j] = i_tab[j-1];
170                                 i_tab[j-1] = temp;
171                         }
172                 }
173         }
174
175         /*
176          * We have dp2 locked. If it isn't first, unlock it.
177          * If it is first, tell xfs_lock_inodes so it can skip it
178          * when locking. if dp1 == dp2, xfs_lock_inodes will skip both
179          * since they are equal. xfs_lock_inodes needs all these inodes
180          * so that it can unlock and retry if there might be a dead-lock
181          * potential with the log.
182          */
183
184         if (i_tab[0] == dp2 && lock_mode == XFS_ILOCK_SHARED) {
185 #ifdef DEBUG
186                 xfs_rename_skip++;
187 #endif
188                 xfs_lock_inodes(i_tab, *num_inodes, 1, XFS_ILOCK_SHARED);
189         } else {
190 #ifdef DEBUG
191                 xfs_rename_nskip++;
192 #endif
193                 xfs_iunlock_map_shared(dp2, lock_mode);
194                 xfs_lock_inodes(i_tab, *num_inodes, 0, XFS_ILOCK_SHARED);
195         }
196
197         /*
198          * Set the return value. Null out any unused entries in i_tab.
199          */
200         *ipp1 = *ipp2 = NULL;
201         for (i=0; i < *num_inodes; i++) {
202                 if (i_tab[i]->i_ino == inum1) {
203                         *ipp1 = i_tab[i];
204                 }
205                 if (i_tab[i]->i_ino == inum2) {
206                         *ipp2 = i_tab[i];
207                 }
208         }
209         for (;i < 4; i++) {
210                 i_tab[i] = NULL;
211         }
212         return 0;
213 }
214
215 /*
216  * xfs_rename
217  */
218 int
219 xfs_rename(
220         xfs_inode_t     *src_dp,
221         bhv_vname_t     *src_vname,
222         xfs_inode_t     *target_dp,
223         bhv_vname_t     *target_vname)
224 {
225         xfs_trans_t     *tp;
226         xfs_inode_t     *src_ip, *target_ip;
227         xfs_mount_t     *mp = src_dp->i_mount;
228         int             new_parent;             /* moving to a new dir */
229         int             src_is_directory;       /* src_name is a directory */
230         int             error;
231         xfs_bmap_free_t free_list;
232         xfs_fsblock_t   first_block;
233         int             cancel_flags;
234         int             committed;
235         xfs_inode_t     *inodes[4];
236         int             target_ip_dropped = 0;  /* dropped target_ip link? */
237         int             spaceres;
238         int             target_link_zero = 0;
239         int             num_inodes;
240         char            *src_name = VNAME(src_vname);
241         char            *target_name = VNAME(target_vname);
242         int             src_namelen = VNAMELEN(src_vname);
243         int             target_namelen = VNAMELEN(target_vname);
244
245         xfs_itrace_entry(src_dp);
246         xfs_itrace_entry(target_dp);
247
248         if (DM_EVENT_ENABLED(src_dp, DM_EVENT_RENAME) ||
249             DM_EVENT_ENABLED(target_dp, DM_EVENT_RENAME)) {
250                 error = XFS_SEND_NAMESP(mp, DM_EVENT_RENAME,
251                                         src_dp, DM_RIGHT_NULL,
252                                         target_dp, DM_RIGHT_NULL,
253                                         src_name, target_name,
254                                         0, 0, 0);
255                 if (error) {
256                         return error;
257                 }
258         }
259         /* Return through std_return after this point. */
260
261         /*
262          * Lock all the participating inodes. Depending upon whether
263          * the target_name exists in the target directory, and
264          * whether the target directory is the same as the source
265          * directory, we can lock from 2 to 4 inodes.
266          * xfs_lock_for_rename() will return ENOENT if src_name
267          * does not exist in the source directory.
268          */
269         tp = NULL;
270         error = xfs_lock_for_rename(src_dp, target_dp, src_vname,
271                         target_vname, &src_ip, &target_ip, inodes,
272                         &num_inodes);
273
274         if (error) {
275                 /*
276                  * We have nothing locked, no inode references, and
277                  * no transaction, so just get out.
278                  */
279                 goto std_return;
280         }
281
282         ASSERT(src_ip != NULL);
283
284         if ((src_ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
285                 /*
286                  * Check for link count overflow on target_dp
287                  */
288                 if (target_ip == NULL && (src_dp != target_dp) &&
289                     target_dp->i_d.di_nlink >= XFS_MAXLINK) {
290                         error = XFS_ERROR(EMLINK);
291                         xfs_rename_unlock4(inodes, XFS_ILOCK_SHARED);
292                         goto rele_return;
293                 }
294         }
295
296         /*
297          * If we are using project inheritance, we only allow renames
298          * into our tree when the project IDs are the same; else the
299          * tree quota mechanism would be circumvented.
300          */
301         if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
302                      (target_dp->i_d.di_projid != src_ip->i_d.di_projid))) {
303                 error = XFS_ERROR(EXDEV);
304                 xfs_rename_unlock4(inodes, XFS_ILOCK_SHARED);
305                 goto rele_return;
306         }
307
308         new_parent = (src_dp != target_dp);
309         src_is_directory = ((src_ip->i_d.di_mode & S_IFMT) == S_IFDIR);
310
311         /*
312          * Drop the locks on our inodes so that we can start the transaction.
313          */
314         xfs_rename_unlock4(inodes, XFS_ILOCK_SHARED);
315
316         XFS_BMAP_INIT(&free_list, &first_block);
317         tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
318         cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
319         spaceres = XFS_RENAME_SPACE_RES(mp, target_namelen);
320         error = xfs_trans_reserve(tp, spaceres, XFS_RENAME_LOG_RES(mp), 0,
321                         XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT);
322         if (error == ENOSPC) {
323                 spaceres = 0;
324                 error = xfs_trans_reserve(tp, 0, XFS_RENAME_LOG_RES(mp), 0,
325                                 XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT);
326         }
327         if (error) {
328                 xfs_trans_cancel(tp, 0);
329                 goto rele_return;
330         }
331
332         /*
333          * Attach the dquots to the inodes
334          */
335         if ((error = XFS_QM_DQVOPRENAME(mp, inodes))) {
336                 xfs_trans_cancel(tp, cancel_flags);
337                 goto rele_return;
338         }
339
340         /*
341          * Reacquire the inode locks we dropped above.
342          */
343         xfs_lock_inodes(inodes, num_inodes, 0, XFS_ILOCK_EXCL);
344
345         /*
346          * Join all the inodes to the transaction. From this point on,
347          * we can rely on either trans_commit or trans_cancel to unlock
348          * them.  Note that we need to add a vnode reference to the
349          * directories since trans_commit & trans_cancel will decrement
350          * them when they unlock the inodes.  Also, we need to be careful
351          * not to add an inode to the transaction more than once.
352          */
353         IHOLD(src_dp);
354         xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
355         if (new_parent) {
356                 IHOLD(target_dp);
357                 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
358         }
359         if ((src_ip != src_dp) && (src_ip != target_dp)) {
360                 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
361         }
362         if ((target_ip != NULL) &&
363             (target_ip != src_ip) &&
364             (target_ip != src_dp) &&
365             (target_ip != target_dp)) {
366                 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
367         }
368
369         /*
370          * Set up the target.
371          */
372         if (target_ip == NULL) {
373                 /*
374                  * If there's no space reservation, check the entry will
375                  * fit before actually inserting it.
376                  */
377                 if (spaceres == 0 &&
378                     (error = xfs_dir_canenter(tp, target_dp, target_name,
379                                                 target_namelen)))
380                         goto error_return;
381                 /*
382                  * If target does not exist and the rename crosses
383                  * directories, adjust the target directory link count
384                  * to account for the ".." reference from the new entry.
385                  */
386                 error = xfs_dir_createname(tp, target_dp, target_name,
387                                            target_namelen, src_ip->i_ino,
388                                            &first_block, &free_list, spaceres);
389                 if (error == ENOSPC)
390                         goto error_return;
391                 if (error)
392                         goto abort_return;
393                 xfs_ichgtime(target_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
394
395                 if (new_parent && src_is_directory) {
396                         error = xfs_bumplink(tp, target_dp);
397                         if (error)
398                                 goto abort_return;
399                 }
400         } else { /* target_ip != NULL */
401                 /*
402                  * If target exists and it's a directory, check that both
403                  * target and source are directories and that target can be
404                  * destroyed, or that neither is a directory.
405                  */
406                 if ((target_ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
407                         /*
408                          * Make sure target dir is empty.
409                          */
410                         if (!(xfs_dir_isempty(target_ip)) ||
411                             (target_ip->i_d.di_nlink > 2)) {
412                                 error = XFS_ERROR(EEXIST);
413                                 goto error_return;
414                         }
415                 }
416
417                 /*
418                  * Link the source inode under the target name.
419                  * If the source inode is a directory and we are moving
420                  * it across directories, its ".." entry will be
421                  * inconsistent until we replace that down below.
422                  *
423                  * In case there is already an entry with the same
424                  * name at the destination directory, remove it first.
425                  */
426                 error = xfs_dir_replace(tp, target_dp, target_name,
427                                         target_namelen, src_ip->i_ino,
428                                         &first_block, &free_list, spaceres);
429                 if (error)
430                         goto abort_return;
431                 xfs_ichgtime(target_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
432
433                 /*
434                  * Decrement the link count on the target since the target
435                  * dir no longer points to it.
436                  */
437                 error = xfs_droplink(tp, target_ip);
438                 if (error)
439                         goto abort_return;
440                 target_ip_dropped = 1;
441
442                 if (src_is_directory) {
443                         /*
444                          * Drop the link from the old "." entry.
445                          */
446                         error = xfs_droplink(tp, target_ip);
447                         if (error)
448                                 goto abort_return;
449                 }
450
451                 /* Do this test while we still hold the locks */
452                 target_link_zero = (target_ip)->i_d.di_nlink==0;
453
454         } /* target_ip != NULL */
455
456         /*
457          * Remove the source.
458          */
459         if (new_parent && src_is_directory) {
460                 /*
461                  * Rewrite the ".." entry to point to the new
462                  * directory.
463                  */
464                 error = xfs_dir_replace(tp, src_ip, "..", 2, target_dp->i_ino,
465                                         &first_block, &free_list, spaceres);
466                 ASSERT(error != EEXIST);
467                 if (error)
468                         goto abort_return;
469                 xfs_ichgtime(src_ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
470
471         } else {
472                 /*
473                  * We always want to hit the ctime on the source inode.
474                  * We do it in the if clause above for the 'new_parent &&
475                  * src_is_directory' case, and here we get all the other
476                  * cases.  This isn't strictly required by the standards
477                  * since the source inode isn't really being changed,
478                  * but old unix file systems did it and some incremental
479                  * backup programs won't work without it.
480                  */
481                 xfs_ichgtime(src_ip, XFS_ICHGTIME_CHG);
482         }
483
484         /*
485          * Adjust the link count on src_dp.  This is necessary when
486          * renaming a directory, either within one parent when
487          * the target existed, or across two parent directories.
488          */
489         if (src_is_directory && (new_parent || target_ip != NULL)) {
490
491                 /*
492                  * Decrement link count on src_directory since the
493                  * entry that's moved no longer points to it.
494                  */
495                 error = xfs_droplink(tp, src_dp);
496                 if (error)
497                         goto abort_return;
498         }
499
500         error = xfs_dir_removename(tp, src_dp, src_name, src_namelen,
501                         src_ip->i_ino, &first_block, &free_list, spaceres);
502         if (error)
503                 goto abort_return;
504         xfs_ichgtime(src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
505
506         /*
507          * Update the generation counts on all the directory inodes
508          * that we're modifying.
509          */
510         src_dp->i_gen++;
511         xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
512
513         if (new_parent) {
514                 target_dp->i_gen++;
515                 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
516         }
517
518         /*
519          * If there was a target inode, take an extra reference on
520          * it here so that it doesn't go to xfs_inactive() from
521          * within the commit.
522          */
523         if (target_ip != NULL) {
524                 IHOLD(target_ip);
525         }
526
527         /*
528          * If this is a synchronous mount, make sure that the
529          * rename transaction goes to disk before returning to
530          * the user.
531          */
532         if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
533                 xfs_trans_set_sync(tp);
534         }
535
536         /*
537          * Take refs. for vop_link_removed calls below.  No need to worry
538          * about directory refs. because the caller holds them.
539          *
540          * Do holds before the xfs_bmap_finish since it might rele them down
541          * to zero.
542          */
543
544         if (target_ip_dropped)
545                 IHOLD(target_ip);
546         IHOLD(src_ip);
547
548         error = xfs_bmap_finish(&tp, &free_list, &committed);
549         if (error) {
550                 xfs_bmap_cancel(&free_list);
551                 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
552                                  XFS_TRANS_ABORT));
553                 if (target_ip != NULL) {
554                         IRELE(target_ip);
555                 }
556                 if (target_ip_dropped) {
557                         IRELE(target_ip);
558                 }
559                 IRELE(src_ip);
560                 goto std_return;
561         }
562
563         /*
564          * trans_commit will unlock src_ip, target_ip & decrement
565          * the vnode references.
566          */
567         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
568         if (target_ip != NULL)
569                 IRELE(target_ip);
570         /*
571          * Let interposed file systems know about removed links.
572          */
573         if (target_ip_dropped)
574                 IRELE(target_ip);
575
576         IRELE(src_ip);
577
578         /* Fall through to std_return with error = 0 or errno from
579          * xfs_trans_commit      */
580 std_return:
581         if (DM_EVENT_ENABLED(src_dp, DM_EVENT_POSTRENAME) ||
582             DM_EVENT_ENABLED(target_dp, DM_EVENT_POSTRENAME)) {
583                 (void) XFS_SEND_NAMESP (mp, DM_EVENT_POSTRENAME,
584                                         src_dp, DM_RIGHT_NULL,
585                                         target_dp, DM_RIGHT_NULL,
586                                         src_name, target_name,
587                                         0, error, 0);
588         }
589         return error;
590
591  abort_return:
592         cancel_flags |= XFS_TRANS_ABORT;
593         /* FALLTHROUGH */
594  error_return:
595         xfs_bmap_cancel(&free_list);
596         xfs_trans_cancel(tp, cancel_flags);
597         goto std_return;
598
599  rele_return:
600         IRELE(src_ip);
601         if (target_ip != NULL) {
602                 IRELE(target_ip);
603         }
604         goto std_return;
605 }