1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
/*
* Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_types.h"
#include "xfs_log.h"
#include "xfs_inum.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
#include "xfs_dir.h"
#include "xfs_dir2.h"
#include "xfs_dmapi.h"
#include "xfs_mount.h"
#include "xfs_da_btree.h"
#include "xfs_bmap_btree.h"
#include "xfs_dir_sf.h"
#include "xfs_dir2_sf.h"
#include "xfs_attr_sf.h"
#include "xfs_dinode.h"
#include "xfs_inode.h"
#include "xfs_inode_item.h"
#include "xfs_bmap.h"
#include "xfs_error.h"
#include "xfs_quota.h"
#include "xfs_refcache.h"
#include "xfs_utils.h"
#include "xfs_trans_space.h"
#include "xfs_dir_leaf.h"
/*
* Given an array of up to 4 inode pointers, unlock the pointed to inodes.
* If there are fewer than 4 entries in the array, the empty entries will
* be at the end and will have NULL pointers in them.
*/
STATIC void
xfs_rename_unlock4(
xfs_inode_t **i_tab,
uint lock_mode)
{
int i;
xfs_iunlock(i_tab[0], lock_mode);
for (i = 1; i < 4; i++) {
if (i_tab[i] == NULL) {
break;
}
/*
* Watch out for duplicate entries in the table.
*/
if (i_tab[i] != i_tab[i-1]) {
xfs_iunlock(i_tab[i], lock_mode);
}
}
}
#ifdef DEBUG
int xfs_rename_skip, xfs_rename_nskip;
#endif
/*
* The following routine will acquire the locks required for a rename
* operation. The code understands the semantics of renames and will
* validate that name1 exists under dp1 & that name2 may or may not
* exist under dp2.
*
* We are renaming dp1/name1 to dp2/name2.
*
* Return ENOENT if dp1 does not exist, other lookup errors, or 0 for success.
*/
STATIC int
xfs_lock_for_rename(
xfs_inode_t *dp1, /* old (source) directory inode */
xfs_inode_t *dp2, /* new (target) directory inode */
vname_t *vname1,/* old entry name */
vname_t *vname2,/* new entry name */
xfs_inode_t **ipp1, /* inode of old entry */
xfs_inode_t **ipp2, /* inode of new entry, if it
already exists, NULL otherwise. */
xfs_inode_t **i_tab,/* array of inode returned, sorted */
int *num_inodes) /* number of inodes in array */
{
xfs_inode_t *ip1, *ip2, *temp;
xfs_ino_t inum1, inum2;
int error;
int i, j;
uint lock_mode;
int diff_dirs = (dp1 != dp2);
ip2 = NULL;
/*
* First, find out the current inums of the entries so that we
* can determine the initial locking order. We'll have to
* sanity check stuff after all the locks have been acquired
* to see if we still have the right inodes, directories, etc.
*/
lock_mode = xfs_ilock_map_shared(dp1);
error = xfs_get_dir_entry(vname1, &ip1);
if (error) {
xfs_iunlock_map_shared(dp1, lock_mode);
return error;
}
inum1 = ip1->i_ino;
ASSERT(ip1);
ITRACE(ip1);
/*
* Unlock dp1 and lock dp2 if they are different.
*/
if (diff_dirs) {
xfs_iunlock_map_shared(dp1, lock_mode);
lock_mode = xfs_ilock_map_shared(dp2);
}
error = xfs_dir_lookup_int(XFS_ITOBHV(dp2), lock_mode,
vname2, &inum2, &ip2);
if (error == ENOENT) { /* target does not need to exist. */
inum2 = 0;
} else if (error) {
/*
* If dp2 and dp1 are the same, the next line unlocks dp1.
* Got it?
*/
xfs_iunlock_map_shared(dp2, lock_mode);
IRELE (ip1);
return error;
} else {
ITRACE(ip2);
}
/*
* i_tab contains a list of pointers to inodes. We initialize
* the table here & we'll sort it. We will then use it to
* order the acquisition of the inode locks.
*
* Note that the table may contain duplicates. e.g., dp1 == dp2.
*/
i_tab[0] = dp1;
i_tab[1] = dp2;
i_tab[2] = ip1;
if (inum2 == 0) {
*num_inodes = 3;
i_tab[3] = NULL;
} else {
*num_inodes = 4;
i_tab[3] = ip2;
}
/*
* Sort the elements via bubble sort. (Remember, there are at
* most 4 elements to sort, so this is adequate.)
*/
for (i=0; i < *num_inodes; i++) {
for (j=1; j < *num_inodes; j++) {
if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
temp = i_tab[j];
i_tab[j] = i_tab[j-1];
i_tab[j-1] = temp;
}
}
}
/*
* We have dp2 locked. If it isn't first, unlock it.
* If it is first, tell xfs_lock_inodes so it can skip it
* when locking. if dp1 == dp2, xfs_lock_inodes will skip both
* since they are equal. xfs_lock_inodes needs all these inodes
* so that it can unlock and retry if there might be a dead-lock
* potential with the log.
*/
if (i_tab[0] == dp2 && lock_mode == XFS_ILOCK_SHARED) {
#ifdef DEBUG
xfs_rename_skip++;
#endif
xfs_lock_inodes(i_tab, *num_inodes, 1, XFS_ILOCK_SHARED);
} else {
#ifdef DEBUG
xfs_rename_nskip++;
#endif
xfs_iunlock_map_shared(dp2, lock_mode);
xfs_lock_inodes(i_tab, *num_inodes, 0, XFS_ILOCK_SHARED);
}
/*
* Set the return value. Null out any unused entries in i_tab.
*/
*ipp1 = *ipp2 = NULL;
for (i=0; i < *num_inodes; i++) {
if (i_tab[i]->i_ino == inum1) {
*ipp1 = i_tab[i];
}
if (i_tab[i]->i_ino == inum2) {
*ipp2 = i_tab[i];
}
}
for (;i < 4; i++) {
i_tab[i] = NULL;
}
return 0;
}
/*
* xfs_rename
*/
int
xfs_rename(
bhv_desc_t *src_dir_bdp,
vname_t *src_vname,
vnode_t *target_dir_vp,
vname_t *target_vname,
cred_t *credp)
{
xfs_trans_t *tp;
xfs_inode_t *src_dp, *target_dp, *src_ip, *target_ip;
xfs_mount_t *mp;
int new_parent; /* moving to a new dir */
int src_is_directory; /* src_name is a directory */
int error;
xfs_bmap_free_t free_list;
xfs_fsblock_t first_block;
int cancel_flags;
int committed;
xfs_inode_t *inodes[4];
int target_ip_dropped = 0; /* dropped target_ip link? */
vnode_t *src_dir_vp;
int spaceres;
int target_link_zero = 0;
int num_inodes;
char *src_name = VNAME(src_vname);
char *target_name = VNAME(target_vname);
int src_namelen = VNAMELEN(src_vname);
int target_namelen = VNAMELEN(target_vname);
src_dir_vp = BHV_TO_VNODE(src_dir_bdp);
vn_trace_entry(src_dir_vp, "xfs_rename", (inst_t *)__return_address);
vn_trace_entry(target_dir_vp, "xfs_rename", (inst_t *)__return_address);
/*
* Find the XFS behavior descriptor for the target directory
* vnode since it was not handed to us.
*/
|