Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * Copyright (C) 2020-2023 Oracle. All Rights Reserved.
4 : * Author: Darrick J. Wong <djwong@kernel.org>
5 : */
6 : #include "xfs.h"
7 : #include "xfs_fs.h"
8 : #include "xfs_shared.h"
9 : #include "xfs_format.h"
10 : #include "xfs_trans_resv.h"
11 : #include "xfs_mount.h"
12 : #include "xfs_defer.h"
13 : #include "xfs_bit.h"
14 : #include "xfs_log_format.h"
15 : #include "xfs_trans.h"
16 : #include "xfs_sb.h"
17 : #include "xfs_inode.h"
18 : #include "xfs_icache.h"
19 : #include "xfs_da_format.h"
20 : #include "xfs_da_btree.h"
21 : #include "xfs_dir2.h"
22 : #include "xfs_bmap_btree.h"
23 : #include "xfs_dir2_priv.h"
24 : #include "xfs_trans_space.h"
25 : #include "xfs_health.h"
26 : #include "xfs_swapext.h"
27 : #include "xfs_parent.h"
28 : #include "xfs_attr.h"
29 : #include "xfs_bmap.h"
30 : #include "scrub/xfs_scrub.h"
31 : #include "scrub/scrub.h"
32 : #include "scrub/common.h"
33 : #include "scrub/trace.h"
34 : #include "scrub/repair.h"
35 : #include "scrub/iscan.h"
36 : #include "scrub/findparent.h"
37 : #include "scrub/readdir.h"
38 : #include "scrub/tempfile.h"
39 : #include "scrub/tempswap.h"
40 : #include "scrub/orphanage.h"
41 : #include "scrub/xfile.h"
42 : #include "scrub/xfarray.h"
43 : #include "scrub/xfblob.h"
44 : #include "scrub/attr_repair.h"
45 : #include "scrub/listxattr.h"
46 :
47 : /*
48 : * Repairing The Directory Parent Pointer
49 : * ======================================
50 : *
51 : * Currently, only directories support parent pointers (in the form of '..'
52 : * entries), so we simply scan the filesystem and update the '..' entry.
53 : *
54 : * Note that because the only parent pointer is the dotdot entry, we won't
55 : * touch an unhealthy directory, since the directory repair code is perfectly
56 : * capable of rebuilding a directory with the proper parent inode.
57 : *
58 : * See the section on locking issues in dir_repair.c for more information about
59 : * conflicts with the VFS. The findparent code wll keep our incore parent
60 : * inode up to date.
61 : *
62 : * If parent pointers are enabled, we instead reconstruct the parent pointer
63 : * information by visiting every directory entry of every directory in the
64 : * system and translating the relevant dirents into parent pointers. In this
65 : * case, it is advantageous to stash all parent pointers created from dirents
66 : * from a single parent file before replaying them into the temporary file. To
67 : * save memory, the live filesystem scan reuses the findparent object. Parent
68 : * pointer repair chooses either directory scanning or findparent, but not
69 : * both.
70 : *
71 : * When salvaging completes, the remaining stashed entries are replayed to the
72 : * temporary file. All non-parent pointer extended attributes are copied to
73 : * the temporary file's extended attributes. An atomic extent swap is used to
74 : * commit the new directory blocks to the directory being repaired. This will
75 : * disrupt attrmulti cursors.
76 : */
77 :
78 : /* Create a parent pointer in the tempfile. */
79 : #define XREP_PPTR_ADD (1)
80 :
81 : /* Remove a parent pointer from the tempfile. */
82 : #define XREP_PPTR_REMOVE (2)
83 :
84 : /* A stashed parent pointer update. */
85 : struct xrep_pptr {
86 : /* Cookie for retrieval of the pptr name. */
87 : xfblob_cookie name_cookie;
88 :
89 : /* Parent pointer attr key. */
90 : xfs_ino_t p_ino;
91 : uint32_t p_gen;
92 :
93 : /* Length of the pptr name. */
94 : uint8_t namelen;
95 :
96 : /* XREP_PPTR_{ADD,REMOVE} */
97 : uint8_t action;
98 : };
99 :
100 : /*
101 : * Stash up to 8 pages of recovered parent pointers in pptr_recs and
102 : * pptr_names before we write them to the temp file.
103 : */
104 : #define XREP_PARENT_MAX_STASH_BYTES (PAGE_SIZE * 8)
105 :
106 : struct xrep_parent {
107 : struct xfs_scrub *sc;
108 :
109 : /* Fixed-size array of xrep_pptr structures. */
110 : struct xfarray *pptr_recs;
111 :
112 : /* Blobs containing parent pointer names. */
113 : struct xfblob *pptr_names;
114 :
115 : /* xattr keys */
116 : struct xfarray *xattr_records;
117 :
118 : /* xattr values */
119 : struct xfblob *xattr_blobs;
120 :
121 : /* Scratch buffers for saving extended attributes */
122 : unsigned char *xattr_name;
123 : void *xattr_value;
124 : unsigned int xattr_value_sz;
125 :
126 : /*
127 : * Information used to swap the attr fork, if the fs supports parent
128 : * pointers.
129 : */
130 : struct xrep_tempswap tx;
131 :
132 : /*
133 : * Information used to scan the filesystem to find the inumber of the
134 : * dotdot entry for this directory. On filesystems without parent
135 : * pointers, we use the findparent_* functions on this object and
136 : * access only the parent_ino field directly.
137 : *
138 : * When parent pointers are enabled, the directory entry scanner uses
139 : * the iscan, hooks, and lock fields of this object directly.
140 : * @pscan.lock coordinates access to pptr_recs, pptr_names, pptr, and
141 : * pptr_scratch. This reduces the memory requirements of this
142 : * structure.
143 : *
144 : * The lock also controls access to xattr_records and xattr_blobs(?)
145 : */
146 : struct xrep_parent_scan_info pscan;
147 :
148 : /* Orphanage reparenting request. */
149 : struct xrep_adoption adoption;
150 :
151 : /* Have we seen any live updates of parent pointers recently? */
152 : bool saw_pptr_updates;
153 :
154 : /* xattr key and da args for parent pointer replay. */
155 : struct xfs_parent_scratch pptr_scratch;
156 :
157 : /*
158 : * Scratch buffer for scanning dirents to create pptr xattrs. At the
159 : * very end of the repair, it can also be used to compute the
160 : * lost+found filename if we need to reparent the file.
161 : */
162 : struct xfs_parent_name_irec pptr;
163 : };
164 :
165 : struct xrep_parent_xattr {
166 : /* Cookie for retrieval of the xattr name. */
167 : xfblob_cookie name_cookie;
168 :
169 : /* Cookie for retrieval of the xattr value. */
170 : xfblob_cookie value_cookie;
171 :
172 : /* XFS_ATTR_* flags */
173 : int flags;
174 :
175 : /* Length of the value and name. */
176 : uint32_t valuelen;
177 : uint16_t namelen;
178 : };
179 :
180 : /*
181 : * Stash up to 8 pages of attrs in xattr_records/xattr_blobs before we write
182 : * them to the temp file.
183 : */
184 : #define XREP_PARENT_XATTR_MAX_STASH_BYTES (PAGE_SIZE * 8)
185 :
186 : /* Tear down all the incore stuff we created. */
187 : static void
188 2301099 : xrep_parent_teardown(
189 : struct xrep_parent *rp)
190 : {
191 2301099 : xrep_findparent_scan_teardown(&rp->pscan);
192 2305943 : kvfree(rp->xattr_name);
193 2303476 : rp->xattr_name = NULL;
194 2303476 : kvfree(rp->xattr_value);
195 2299610 : rp->xattr_value = NULL;
196 2299610 : if (rp->xattr_blobs)
197 2255334 : xfblob_destroy(rp->xattr_blobs);
198 2304135 : rp->xattr_blobs = NULL;
199 2304135 : if (rp->xattr_records)
200 2259863 : xfarray_destroy(rp->xattr_records);
201 2304849 : rp->xattr_records = NULL;
202 2304849 : if (rp->pptr_names)
203 2260576 : xfblob_destroy(rp->pptr_names);
204 2306934 : rp->pptr_names = NULL;
205 2306934 : if (rp->pptr_recs)
206 2262663 : xfarray_destroy(rp->pptr_recs);
207 2307347 : rp->pptr_recs = NULL;
208 2307347 : }
209 :
210 : /* Set up for a parent repair. */
211 : int
212 6076783 : xrep_setup_parent(
213 : struct xfs_scrub *sc)
214 : {
215 6076783 : struct xrep_parent *rp;
216 6076783 : int error;
217 :
218 6076783 : xchk_fsgates_enable(sc, XCHK_FSGATES_DIRENTS);
219 :
220 6083818 : rp = kvzalloc(sizeof(struct xrep_parent), XCHK_GFP_FLAGS);
221 6089580 : if (!rp)
222 : return -ENOMEM;
223 6089580 : rp->sc = sc;
224 6089580 : sc->buf = rp;
225 :
226 6089580 : error = xrep_tempfile_create(sc, S_IFREG);
227 6093733 : if (error)
228 : return error;
229 :
230 4630670 : return xrep_orphanage_try_create(sc);
231 : }
232 :
233 : /*
234 : * Scan all files in the filesystem for a child dirent that we can turn into
235 : * the dotdot entry for this directory.
236 : */
237 : STATIC int
238 44283 : xrep_parent_find_dotdot(
239 : struct xrep_parent *rp)
240 : {
241 44283 : struct xfs_scrub *sc = rp->sc;
242 44283 : xfs_ino_t ino;
243 44283 : unsigned int sick, checked;
244 44283 : int error;
245 :
246 : /*
247 : * Avoid sick directories. There shouldn't be anyone else clearing the
248 : * directory's sick status.
249 : */
250 44283 : xfs_inode_measure_sickness(sc->ip, &sick, &checked);
251 44283 : if (sick & XFS_SICK_INO_DIR)
252 : return -EFSCORRUPTED;
253 :
254 44283 : ino = xrep_findparent_self_reference(sc);
255 44282 : if (ino != NULLFSINO) {
256 230 : xrep_findparent_scan_finish_early(&rp->pscan, ino);
257 230 : return 0;
258 : }
259 :
260 : /*
261 : * Drop the ILOCK on this directory so that we can scan for the dotdot
262 : * entry. Figure out who is going to be the parent of this directory,
263 : * then retake the ILOCK so that we can salvage directory entries.
264 : */
265 44052 : xchk_iunlock(sc, XFS_ILOCK_EXCL);
266 :
267 : /* Does the VFS dcache have an answer for us? */
268 44053 : ino = xrep_findparent_from_dcache(sc);
269 44053 : if (ino != NULLFSINO) {
270 43361 : error = xrep_findparent_confirm(sc, &ino);
271 43360 : if (!error && ino != NULLFSINO) {
272 43360 : xrep_findparent_scan_finish_early(&rp->pscan, ino);
273 43359 : goto out_relock;
274 : }
275 : }
276 :
277 : /* Scan the entire filesystem for a parent. */
278 692 : error = xrep_findparent_scan(&rp->pscan);
279 44051 : out_relock:
280 44051 : xchk_ilock(sc, XFS_ILOCK_EXCL);
281 :
282 44051 : return error;
283 : }
284 :
285 : /*
286 : * Add this stashed incore parent pointer to the temporary file.
287 : * The caller must hold the tempdir's IOLOCK, must not hold any ILOCKs, and
288 : * must not be in transaction context.
289 : */
290 : STATIC int
291 2342002 : xrep_parent_replay_update(
292 : struct xrep_parent *rp,
293 : const struct xrep_pptr *pptr)
294 : {
295 2342002 : struct xfs_scrub *sc = rp->sc;
296 2342002 : int error;
297 :
298 2342002 : rp->pptr.p_ino = pptr->p_ino;
299 2342002 : rp->pptr.p_gen = pptr->p_gen;
300 2342002 : rp->pptr.p_namelen = pptr->namelen;
301 2342002 : xfs_parent_irec_hashname(sc->mp, &rp->pptr);
302 :
303 2340940 : switch (pptr->action) {
304 2340940 : case XREP_PPTR_ADD:
305 : /* Create parent pointer. */
306 2340940 : trace_xrep_parent_replay_parentadd(sc->tempip, &rp->pptr);
307 :
308 2339930 : error = xfs_parent_set(sc->tempip, sc->ip->i_ino, &rp->pptr,
309 : &rp->pptr_scratch);
310 2343517 : if (error)
311 0 : return error;
312 : break;
313 0 : case XREP_PPTR_REMOVE:
314 : /* Remove parent pointer. */
315 0 : trace_xrep_parent_replay_parentremove(sc->tempip, &rp->pptr);
316 :
317 0 : error = xfs_parent_unset(sc->tempip, sc->ip->i_ino, &rp->pptr,
318 : &rp->pptr_scratch);
319 0 : if (error)
320 0 : return error;
321 : break;
322 0 : default:
323 0 : ASSERT(0);
324 0 : return -EIO;
325 : }
326 :
327 : return 0;
328 : }
329 :
330 : /*
331 : * Flush stashed parent pointer updates that have been recorded by the scanner.
332 : * This is done to reduce the memory requirements of the parent pointer
333 : * rebuild, since files can have a lot of hardlinks and the fs can be busy.
334 : *
335 : * Caller must not hold transactions or ILOCKs. Caller must hold the tempfile
336 : * IOLOCK.
337 : */
338 : STATIC int
339 2262636 : xrep_parent_replay_updates(
340 : struct xrep_parent *rp)
341 : {
342 2262636 : xfarray_idx_t array_cur;
343 2262636 : int error;
344 :
345 2262636 : mutex_lock(&rp->pscan.lock);
346 6869933 : foreach_xfarray_idx(rp->pptr_recs, array_cur) {
347 2341827 : struct xrep_pptr pptr;
348 :
349 2341827 : error = xfarray_load(rp->pptr_recs, array_cur, &pptr);
350 2342793 : if (error)
351 0 : goto out_unlock;
352 :
353 2342793 : error = xfblob_load(rp->pptr_names, pptr.name_cookie,
354 2342793 : rp->pptr.p_name, pptr.namelen);
355 2343114 : if (error)
356 0 : goto out_unlock;
357 2343114 : rp->pptr.p_name[MAXNAMELEN - 1] = 0;
358 2343114 : mutex_unlock(&rp->pscan.lock);
359 :
360 2343423 : error = xrep_parent_replay_update(rp, &pptr);
361 2343516 : if (error)
362 0 : return error;
363 :
364 2343516 : mutex_lock(&rp->pscan.lock);
365 : }
366 :
367 : /* Empty out both arrays now that we've added the entries. */
368 2264177 : xfarray_truncate(rp->pptr_recs);
369 2262620 : xfblob_truncate(rp->pptr_names);
370 2263943 : mutex_unlock(&rp->pscan.lock);
371 2263943 : return 0;
372 : out_unlock:
373 0 : mutex_unlock(&rp->pscan.lock);
374 0 : return error;
375 : }
376 :
377 : /*
378 : * Remember that we want to create a parent pointer in the tempfile. These
379 : * stashed actions will be replayed later.
380 : */
381 : STATIC int
382 2343653 : xrep_parent_stash_parentadd(
383 : struct xrep_parent *rp,
384 : const struct xfs_name *name,
385 : const struct xfs_inode *dp)
386 : {
387 2343653 : struct xrep_pptr pptr = {
388 : .action = XREP_PPTR_ADD,
389 2343653 : .namelen = name->len,
390 2343653 : .p_ino = dp->i_ino,
391 2343653 : .p_gen = VFS_IC(dp)->i_generation,
392 : };
393 2343653 : int error;
394 :
395 2343653 : trace_xrep_parent_stash_parentadd(rp->sc->tempip, dp, name);
396 :
397 2343650 : error = xfblob_store(rp->pptr_names, &pptr.name_cookie, name->name,
398 2343650 : name->len);
399 2343622 : if (error)
400 : return error;
401 :
402 2343622 : return xfarray_append(rp->pptr_recs, &pptr);
403 : }
404 :
405 : /*
406 : * Remember that we want to remove a parent pointer from the tempfile. These
407 : * stashed actions will be replayed later.
408 : */
409 : STATIC int
410 0 : xrep_parent_stash_parentremove(
411 : struct xrep_parent *rp,
412 : const struct xfs_name *name,
413 : const struct xfs_inode *dp)
414 : {
415 0 : struct xrep_pptr pptr = {
416 : .action = XREP_PPTR_REMOVE,
417 0 : .namelen = name->len,
418 0 : .p_ino = dp->i_ino,
419 0 : .p_gen = VFS_IC(dp)->i_generation,
420 : };
421 0 : int error;
422 :
423 0 : trace_xrep_parent_stash_parentremove(rp->sc->tempip, dp, name);
424 :
425 0 : error = xfblob_store(rp->pptr_names, &pptr.name_cookie, name->name,
426 0 : name->len);
427 0 : if (error)
428 : return error;
429 :
430 0 : return xfarray_append(rp->pptr_recs, &pptr);
431 : }
432 :
433 : /*
434 : * Examine an entry of a directory. If this dirent leads us back to the file
435 : * whose parent pointers we're rebuilding, add a pptr to the temporary
436 : * directory.
437 : */
438 : STATIC int
439 59050549965 : xrep_parent_scan_dirent(
440 : struct xfs_scrub *sc,
441 : struct xfs_inode *dp,
442 : xfs_dir2_dataptr_t dapos,
443 : const struct xfs_name *name,
444 : xfs_ino_t ino,
445 : void *priv)
446 : {
447 59050549965 : struct xrep_parent *rp = priv;
448 59050549965 : int error;
449 :
450 : /* Dirent doesn't point to this directory. */
451 59050549965 : if (ino != rp->sc->ip->i_ino)
452 : return 0;
453 :
454 : /* No weird looking names. */
455 2563264 : if (!xfs_dir2_namecheck(name->name, name->len))
456 : return -EFSCORRUPTED;
457 :
458 : /* No mismatching ftypes. */
459 2563261 : if (name->type != xfs_mode_to_ftype(VFS_I(sc->ip)->i_mode))
460 : return -EFSCORRUPTED;
461 :
462 : /* Don't pick up dot or dotdot entries; we only want child dirents. */
463 4906906 : if (xfs_dir2_samename(name, &xfs_name_dotdot) ||
464 2343649 : xfs_dir2_samename(name, &xfs_name_dot))
465 219608 : return 0;
466 :
467 : /*
468 : * Transform this dirent into a parent pointer and queue it for later
469 : * addition to the temporary file.
470 : */
471 2343649 : mutex_lock(&rp->pscan.lock);
472 2343656 : error = xrep_parent_stash_parentadd(rp, name, dp);
473 2343628 : mutex_unlock(&rp->pscan.lock);
474 2343628 : return error;
475 : }
476 :
477 : /*
478 : * Decide if we want to look for dirents in this directory. Skip the file
479 : * being repaired and any files being used to stage repairs.
480 : */
481 : static inline bool
482 96792658996 : xrep_parent_want_scan(
483 : struct xrep_parent *rp,
484 : const struct xfs_inode *ip)
485 : {
486 96792658996 : return ip != rp->sc->ip && !xrep_is_tempfile(ip);
487 : }
488 :
489 : /*
490 : * Take ILOCK on a file that we want to scan.
491 : *
492 : * Select ILOCK_EXCL if the file is a directory with an unloaded data bmbt.
493 : * Otherwise, take ILOCK_SHARED.
494 : */
495 : static inline unsigned int
496 48746493808 : xrep_parent_scan_ilock(
497 : struct xrep_parent *rp,
498 : struct xfs_inode *ip)
499 : {
500 48746493808 : uint lock_mode = XFS_ILOCK_SHARED;
501 :
502 : /* Still need to take the shared ILOCK to advance the iscan cursor. */
503 48746493808 : if (!xrep_parent_want_scan(rp, ip))
504 11079224 : goto lock;
505 :
506 53196388624 : if (S_ISDIR(VFS_I(ip)->i_mode) && xfs_need_iread_extents(&ip->i_df)) {
507 0 : lock_mode = XFS_ILOCK_EXCL;
508 0 : goto lock;
509 : }
510 :
511 48792615695 : lock:
512 48803694919 : xfs_ilock(ip, lock_mode);
513 48843254013 : return lock_mode;
514 : }
515 :
516 : /*
517 : * Scan this file for relevant child dirents that point to the file whose
518 : * parent pointers we're rebuilding.
519 : */
520 : STATIC int
521 48768611960 : xrep_parent_scan_file(
522 : struct xrep_parent *rp,
523 : struct xfs_inode *ip)
524 : {
525 48768611960 : unsigned int lock_mode;
526 48768611960 : int error = 0;
527 :
528 48768611960 : lock_mode = xrep_parent_scan_ilock(rp, ip);
529 :
530 48755195014 : if (!xrep_parent_want_scan(rp, ip))
531 11079215 : goto scan_done;
532 :
533 48674860506 : if (S_ISDIR(VFS_I(ip)->i_mode)) {
534 4401343265 : error = xchk_dir_walk(rp->sc, ip, xrep_parent_scan_dirent, rp);
535 4405411204 : if (error)
536 0 : goto scan_done;
537 : }
538 :
539 48678928445 : scan_done:
540 48690007660 : xchk_iscan_mark_visited(&rp->pscan.iscan, ip);
541 48714557423 : xfs_iunlock(ip, lock_mode);
542 48369245833 : return error;
543 : }
544 :
545 : /* Decide if we've stashed too much pptr data in memory. */
546 : static inline bool
547 49071129411 : xrep_parent_want_flush_stashed(
548 : struct xrep_parent *rp)
549 : {
550 49071129411 : unsigned long long bytes;
551 :
552 49071129411 : bytes = xfarray_bytes(rp->pptr_recs) + xfblob_bytes(rp->pptr_names);
553 48952583703 : return bytes > XREP_PARENT_MAX_STASH_BYTES;
554 : }
555 :
556 : /*
557 : * Scan all directories in the filesystem to look for dirents that we can turn
558 : * into parent pointers.
559 : */
560 : STATIC int
561 2264339 : xrep_parent_scan_dirtree(
562 : struct xrep_parent *rp)
563 : {
564 2264339 : struct xfs_scrub *sc = rp->sc;
565 2264339 : struct xfs_inode *ip;
566 2264339 : int error;
567 :
568 : /*
569 : * Filesystem scans are time consuming. Drop the file ILOCK and all
570 : * other resources for the duration of the scan and hope for the best.
571 : * The live update hooks will keep our scan information up to date.
572 : */
573 2264339 : xchk_trans_cancel(sc);
574 2264318 : if (sc->ilock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL))
575 2264315 : xchk_iunlock(sc, sc->ilock_flags & (XFS_ILOCK_SHARED |
576 : XFS_ILOCK_EXCL));
577 2264323 : error = xchk_trans_alloc_empty(sc);
578 2264293 : if (error)
579 : return error;
580 :
581 48766453385 : while ((error = xchk_iscan_iter(&rp->pscan.iscan, &ip)) == 1) {
582 48824299684 : bool flush;
583 :
584 48824299684 : error = xrep_parent_scan_file(rp, ip);
585 48359100168 : xchk_irele(sc, ip);
586 48999117788 : if (error)
587 : break;
588 :
589 : /* Flush stashed pptr updates to constrain memory usage. */
590 48999117788 : mutex_lock(&rp->pscan.lock);
591 49140196900 : flush = xrep_parent_want_flush_stashed(rp);
592 48890223088 : mutex_unlock(&rp->pscan.lock);
593 49187376896 : if (flush) {
594 4 : xchk_trans_cancel(sc);
595 :
596 4 : error = xrep_tempfile_iolock_polled(sc);
597 4 : if (error)
598 : break;
599 :
600 4 : error = xrep_parent_replay_updates(rp);
601 4 : xrep_tempfile_iounlock(sc);
602 4 : if (error)
603 : break;
604 :
605 4 : error = xchk_trans_alloc_empty(sc);
606 4 : if (error)
607 : break;
608 : }
609 :
610 49187376896 : if (xchk_should_terminate(sc, &error))
611 : break;
612 : }
613 2264311 : xchk_iscan_iter_finish(&rp->pscan.iscan);
614 2264319 : if (error) {
615 : /*
616 : * If we couldn't grab an inode that was busy with a state
617 : * change, change the error code so that we exit to userspace
618 : * as quickly as possible.
619 : */
620 174 : if (error == -EBUSY)
621 : return -ECANCELED;
622 174 : return error;
623 : }
624 :
625 : /*
626 : * Retake sc->ip's ILOCK now that we're done flushing stashed parent
627 : * pointers. We end this function with an empty transaction and the
628 : * ILOCK.
629 : */
630 2264145 : xchk_ilock(rp->sc, XFS_ILOCK_EXCL);
631 2264145 : return 0;
632 : }
633 :
634 : /*
635 : * Capture dirent updates being made by other threads which are relevant to the
636 : * file being repaired.
637 : */
638 : STATIC int
639 166991250 : xrep_parent_live_update(
640 : struct notifier_block *nb,
641 : unsigned long action,
642 : void *data)
643 : {
644 166991250 : struct xfs_dir_update_params *p = data;
645 166991250 : struct xrep_parent *rp;
646 166991250 : struct xfs_scrub *sc;
647 166991250 : int error;
648 :
649 166991250 : rp = container_of(nb, struct xrep_parent, pscan.hooks.dirent_hook.nb);
650 166991250 : sc = rp->sc;
651 :
652 : /*
653 : * This thread updated a dirent that points to the file that we're
654 : * repairing, so stash the update for replay against the temporary
655 : * file.
656 : */
657 166991250 : if (p->ip->i_ino == sc->ip->i_ino &&
658 0 : xchk_iscan_want_live_update(&rp->pscan.iscan, p->dp->i_ino)) {
659 0 : mutex_lock(&rp->pscan.lock);
660 0 : if (p->delta > 0)
661 0 : error = xrep_parent_stash_parentadd(rp, p->name, p->dp);
662 : else
663 0 : error = xrep_parent_stash_parentremove(rp, p->name,
664 : p->dp);
665 0 : if (!error)
666 0 : rp->saw_pptr_updates = true;
667 0 : mutex_unlock(&rp->pscan.lock);
668 0 : if (error)
669 0 : goto out_abort;
670 : }
671 :
672 : return NOTIFY_DONE;
673 : out_abort:
674 0 : xchk_iscan_abort(&rp->pscan.iscan);
675 0 : return NOTIFY_DONE;
676 : }
677 :
678 : /* Reset a directory's dotdot entry, if needed. */
679 : STATIC int
680 252454 : xrep_parent_reset_dotdot(
681 : struct xrep_parent *rp)
682 : {
683 252454 : struct xfs_scrub *sc = rp->sc;
684 252454 : xfs_ino_t ino;
685 252454 : unsigned int spaceres;
686 252454 : int error = 0;
687 :
688 252454 : ASSERT(sc->ilock_flags & XFS_ILOCK_EXCL);
689 :
690 252454 : error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot, &ino);
691 252413 : if (error || ino == rp->pscan.parent_ino)
692 : return error;
693 :
694 0 : xfs_trans_ijoin(sc->tp, sc->ip, 0);
695 :
696 0 : trace_xrep_parent_reset_dotdot(sc->ip, rp->pscan.parent_ino);
697 :
698 : /*
699 : * Reserve more space just in case we have to expand the dir. We're
700 : * allowed to exceed quota to repair inconsistent metadata.
701 : */
702 0 : spaceres = xfs_rename_space_res(sc->mp, 0, false, xfs_name_dotdot.len,
703 : false);
704 0 : error = xfs_trans_reserve_more_inode(sc->tp, sc->ip, spaceres, 0,
705 : true);
706 0 : if (error)
707 : return error;
708 :
709 0 : return xfs_dir_replace(sc->tp, sc->ip, &xfs_name_dotdot,
710 : rp->pscan.parent_ino, spaceres);
711 : }
712 :
713 : /*
714 : * Move the current file to the orphanage.
715 : *
716 : * Caller must hold IOLOCK_EXCL on @sc->ip, and no other inode locks. Upon
717 : * successful return, the scrub transaction will have enough extra reservation
718 : * to make the move; it will hold IOLOCK_EXCL and ILOCK_EXCL of @sc->ip and the
719 : * orphanage; and both inodes will be ijoined.
720 : */
721 : STATIC int
722 24 : xrep_parent_move_to_orphanage(
723 : struct xrep_parent *rp)
724 : {
725 24 : struct xfs_scrub *sc = rp->sc;
726 24 : xfs_ino_t orig_parent, new_parent;
727 24 : int error;
728 :
729 : if (S_ISDIR(VFS_I(sc->ip)->i_mode)) {
730 24 : /*
731 : * We are about to drop the ILOCK on sc->ip to lock the
732 : * orphanage and prepare for the adoption. Therefore, look up
733 24 : * the old dotdot entry for sc->ip so that we can compare it
734 : * after we re-lock sc->ip.
735 : */
736 : error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot,
737 : &orig_parent);
738 : if (error)
739 : return error;
740 0 : } else {
741 : /*
742 0 : * We haven't dropped the ILOCK since we swapped in the new
743 : * parent pointers, which means that the file cannot have been
744 : * moved in the directory tree, and there are no parents.
745 : */
746 : orig_parent = NULLFSINO;
747 : }
748 :
749 : /*
750 24 : * Because the orphanage is just another directory in the filesystem,
751 : * we must take its IOLOCK to coordinate with the VFS. We cannot take
752 : * an IOLOCK while holding an ILOCK, so we must drop the ILOCK. We
753 : * may have to drop the IOLOCK as well.
754 : */
755 : xchk_iunlock(sc, XFS_ILOCK_EXCL);
756 :
757 : error = xrep_adoption_init(sc, &rp->adoption);
758 : if (error)
759 24 : return error;
760 :
761 24 : /* If we can take the orphanage's iolock then we're ready to move. */
762 24 : if (!xrep_orphanage_ilock_nowait(sc, XFS_IOLOCK_EXCL)) {
763 : xchk_iunlock(sc, sc->ilock_flags);
764 : error = xrep_orphanage_iolock_two(sc);
765 : if (error)
766 24 : goto err_adoption;
767 0 : }
768 0 :
769 0 : /* Prepare for the adoption and lock both down. */
770 0 : error = xrep_adoption_prep(&rp->adoption);
771 : if (error)
772 : goto err_adoption;
773 :
774 24 : error = xrep_adoption_compute_name(&rp->adoption, rp->pptr.p_name);
775 24 : if (error)
776 0 : goto err_adoption;
777 :
778 24 : /*
779 24 : * Now that we've reacquired the ILOCK on sc->ip, look up the dotdot
780 0 : * entry again. If the parent changed or the child was unlinked while
781 : * the child directory was unlocked, we don't need to move the child to
782 : * the orphanage after all. For a non-directory, we have to scan for
783 : * the first parent pointer to see if one has been added.
784 : */
785 : if (S_ISDIR(VFS_I(sc->ip)->i_mode))
786 : error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot,
787 : &new_parent);
788 : else
789 24 : error = xrep_findparent_from_pptrs(sc, &new_parent);
790 0 : if (error)
791 : goto err_adoption;
792 : if (orig_parent != new_parent || VFS_I(sc->ip)->i_nlink == 0) {
793 24 : error = 0;
794 24 : goto err_adoption;
795 0 : }
796 24 :
797 24 : return xrep_adoption_commit(&rp->adoption);
798 24 : err_adoption:
799 : xrep_adoption_cancel(&rp->adoption, error);
800 : return error;
801 0 : }
802 24 :
803 24 : /* Ensure that the xattr value buffer is large enough. */
804 24 : STATIC int
805 : xrep_parent_alloc_xattr_value(
806 : struct xrep_parent *rp,
807 : size_t bufsize)
808 : {
809 2264351 : void *new_val;
810 :
811 : if (rp->xattr_value_sz >= bufsize)
812 : return 0;
813 2264351 :
814 : if (rp->xattr_value) {
815 2264351 : kvfree(rp->xattr_value);
816 : rp->xattr_value = NULL;
817 : rp->xattr_value_sz = 0;
818 2264333 : }
819 5 :
820 5 : new_val = kvmalloc(bufsize, XCHK_GFP_FLAGS);
821 5 : if (!new_val)
822 : return -ENOMEM;
823 :
824 2264333 : rp->xattr_value = new_val;
825 2264339 : rp->xattr_value_sz = bufsize;
826 : return 0;
827 : }
828 2264339 :
829 2264339 : /* Retrieve the (remote) value of a non-pptr xattr. */
830 2264339 : STATIC int
831 : xrep_parent_fetch_xattr_remote(
832 : struct xrep_parent *rp,
833 : struct xfs_inode *ip,
834 : unsigned int attr_flags,
835 23 : const unsigned char *name,
836 : unsigned int namelen,
837 : unsigned int valuelen)
838 : {
839 : struct xfs_scrub *sc = rp->sc;
840 : struct xfs_da_args args = {
841 : .op_flags = XFS_DA_OP_NOTIME,
842 : .attr_filter = attr_flags & XFS_ATTR_NSP_ONDISK_MASK,
843 23 : .geo = sc->mp->m_attr_geo,
844 46 : .whichfork = XFS_ATTR_FORK,
845 : .dp = ip,
846 23 : .name = name,
847 23 : .namelen = namelen,
848 : .hashval = xfs_da_hashname(name, namelen),
849 : .trans = sc->tp,
850 : .valuelen = valuelen,
851 : .owner = ip->i_ino,
852 23 : };
853 23 : int error;
854 :
855 23 : /*
856 : * If we need a larger value buffer, try to allocate one. If that
857 23 : * fails, return with -EDEADLOCK to try harder.
858 : */
859 : error = xrep_parent_alloc_xattr_value(rp, valuelen);
860 : if (error == -ENOMEM)
861 : return -EDEADLOCK;
862 : if (error)
863 23 : return error;
864 23 :
865 : args.value = rp->xattr_value;
866 23 : return xfs_attr_get_ilocked(&args);
867 : }
868 :
869 23 : /* Stash non-pptr attributes for later replay into the temporary file. */
870 23 : STATIC int
871 : xrep_parent_stash_xattr(
872 : struct xfs_scrub *sc,
873 : struct xfs_inode *ip,
874 : unsigned int attr_flags,
875 2672818 : const unsigned char *name,
876 : unsigned int namelen,
877 : const void *value,
878 : unsigned int valuelen,
879 : void *priv)
880 : {
881 : struct xrep_parent_xattr key = {
882 : .valuelen = valuelen,
883 : .namelen = namelen,
884 : .flags = attr_flags & XFS_ATTR_NSP_ONDISK_MASK,
885 2672818 : };
886 : struct xrep_parent *rp = priv;
887 : int error;
888 2672818 :
889 : if (attr_flags & (XFS_ATTR_INCOMPLETE | XFS_ATTR_PARENT))
890 2672818 : return 0;
891 2672818 :
892 : if (!value) {
893 2672818 : error = xrep_parent_fetch_xattr_remote(rp, ip, attr_flags,
894 : name, namelen, valuelen);
895 : if (error)
896 329642 : return error;
897 23 :
898 : value = rp->xattr_value;
899 23 : }
900 :
901 : trace_xrep_parent_stash_xattr(rp->sc->tempip, key.flags, (void *)name,
902 23 : key.namelen, key.valuelen);
903 :
904 : error = xfblob_store(rp->xattr_blobs, &key.name_cookie, name,
905 329642 : key.namelen);
906 329642 : if (error)
907 : return error;
908 329641 :
909 329641 : error = xfblob_store(rp->xattr_blobs, &key.value_cookie, value,
910 329630 : key.valuelen);
911 : if (error)
912 : return error;
913 329630 :
914 : return xfarray_append(rp->xattr_records, &key);
915 329636 : }
916 :
917 : /* Insert one xattr key/value. */
918 329636 : STATIC int
919 : xrep_parent_insert_xattr(
920 : struct xrep_parent *rp,
921 : const struct xrep_parent_xattr *key)
922 : {
923 329605 : struct xfs_da_args args = {
924 : .dp = rp->sc->tempip,
925 : .attr_filter = key->flags,
926 : .namelen = key->namelen,
927 329605 : .valuelen = key->valuelen,
928 329605 : .op_flags = XFS_DA_OP_NOTIME,
929 329605 : .owner = rp->sc->ip->i_ino,
930 329605 : };
931 329605 : int error;
932 :
933 329605 : ASSERT(!(key->flags & XFS_ATTR_PARENT));
934 :
935 329605 : /*
936 : * Grab pointers to the scrub buffer so that we can use them to insert
937 329605 : * attrs into the temp file.
938 : */
939 : args.name = rp->xattr_name;
940 : args.value = rp->xattr_value;
941 :
942 : /*
943 329605 : * The attribute name is stored near the end of the in-core buffer,
944 329605 : * though we reserve one more byte to ensure null termination.
945 : */
946 : rp->xattr_name[XATTR_NAME_MAX] = 0;
947 :
948 : error = xfblob_load(rp->xattr_blobs, key->name_cookie, rp->xattr_name,
949 : key->namelen);
950 329605 : if (error)
951 : return error;
952 329605 :
953 329605 : error = xfblob_free(rp->xattr_blobs, key->name_cookie);
954 329631 : if (error)
955 : return error;
956 :
957 329632 : error = xfblob_load(rp->xattr_blobs, key->value_cookie, args.value,
958 329625 : key->valuelen);
959 : if (error)
960 : return error;
961 329625 :
962 329625 : error = xfblob_free(rp->xattr_blobs, key->value_cookie);
963 329637 : if (error)
964 : return error;
965 :
966 329632 : rp->xattr_name[key->namelen] = 0;
967 329637 :
968 : trace_xrep_parent_insert_xattr(rp->sc->tempip, key->flags,
969 : rp->xattr_name, key->namelen, key->valuelen);
970 329637 :
971 : error = xfs_attr_set(&args);
972 329637 : if (error) {
973 329637 : ASSERT(error != -EEXIST);
974 : return error;
975 329610 : }
976 329643 :
977 0 : return 0;
978 0 : }
979 :
980 : /*
981 : * Periodically flush salvaged attributes to the temporary file. This is done
982 : * to reduce the memory requirements of the xattr rebuild because files can
983 : * contain millions of attributes.
984 : */
985 : STATIC int
986 : xrep_parent_flush_xattrs(
987 : struct xrep_parent *rp)
988 : {
989 : xfarray_idx_t array_cur;
990 146245 : int error;
991 :
992 : /*
993 146245 : * Entering this function, the scrub context has a reference to the
994 146245 : * inode being repaired, the temporary file, and the empty scrub
995 : * transaction that we created for the xattr scan. We hold ILOCK_EXCL
996 : * on the inode being repaired.
997 : *
998 : * To constrain kernel memory use, we occasionally flush salvaged
999 : * xattrs from the xfarray and xfblob structures into the temporary
1000 : * file in preparation for swapping the xattr structures at the end.
1001 : * Updating the temporary file requires a transaction, so we commit the
1002 : * scrub transaction and drop the ILOCK so that xfs_attr_set can
1003 : * allocate whatever transaction it wants.
1004 : *
1005 : * We still hold IOLOCK_EXCL on the inode being repaired, which
1006 : * prevents anyone from adding xattrs (or parent pointers) while we're
1007 : * flushing.
1008 : */
1009 : xchk_trans_cancel(rp->sc);
1010 : xchk_iunlock(rp->sc, XFS_ILOCK_EXCL);
1011 :
1012 : /*
1013 146245 : * Take the IOLOCK of the temporary file while we modify xattrs. This
1014 146248 : * isn't strictly required because the temporary file is never revealed
1015 : * to userspace, but we follow the same locking rules. We still hold
1016 : * sc->ip's IOLOCK.
1017 : */
1018 : error = xrep_tempfile_iolock_polled(rp->sc);
1019 : if (error)
1020 : return error;
1021 :
1022 146249 : /* Add all the salvaged attrs to the temporary file. */
1023 146231 : foreach_xfarray_idx(rp->xattr_records, array_cur) {
1024 : struct xrep_parent_xattr key;
1025 :
1026 : error = xfarray_load(rp->xattr_records, array_cur, &key);
1027 475874 : if (error)
1028 329599 : return error;
1029 :
1030 329599 : error = xrep_parent_insert_xattr(rp, &key);
1031 329615 : if (error)
1032 0 : return error;
1033 : }
1034 329615 :
1035 329643 : /* Empty out both arrays now that we've added the entries. */
1036 0 : xfarray_truncate(rp->xattr_records);
1037 : xfblob_truncate(rp->xattr_blobs);
1038 :
1039 : xrep_tempfile_iounlock(rp->sc);
1040 146258 :
1041 146241 : /* Recreate the empty transaction and relock the inode. */
1042 : error = xchk_trans_alloc_empty(rp->sc);
1043 146250 : if (error)
1044 : return error;
1045 : xchk_ilock(rp->sc, XFS_ILOCK_EXCL);
1046 146234 : return 0;
1047 146201 : }
1048 :
1049 146202 : /* Decide if we've stashed too much xattr data in memory. */
1050 146202 : static inline bool
1051 : xrep_parent_want_flush_xattrs(
1052 : struct xrep_parent *rp)
1053 : {
1054 : unsigned long long bytes;
1055 5736 :
1056 : bytes = xfarray_bytes(rp->xattr_records) +
1057 : xfblob_bytes(rp->xattr_blobs);
1058 5736 : return bytes > XREP_PARENT_XATTR_MAX_STASH_BYTES;
1059 : }
1060 5736 :
1061 5736 : /* Flush staged attributes to the temporary file if we're over the limit. */
1062 5736 : STATIC int
1063 : xrep_parent_try_flush_xattrs(
1064 : struct xfs_scrub *sc,
1065 : void *priv)
1066 : {
1067 5736 : struct xrep_parent *rp = priv;
1068 : int error;
1069 :
1070 : if (!xrep_parent_want_flush_xattrs(rp))
1071 5736 : return 0;
1072 5736 :
1073 : error = xrep_parent_flush_xattrs(rp);
1074 5736 : if (error)
1075 : return error;
1076 :
1077 351 : /*
1078 351 : * If there were any parent pointer updates to the xattr structure
1079 : * while we dropped the ILOCK, the xattr structure is now stale.
1080 : * Signal to the attr copy process that we need to start over, but
1081 : * this time without opportunistic attr flushing.
1082 : *
1083 : * This is unlikely to happen, so we're ok with restarting the copy.
1084 : */
1085 : mutex_lock(&rp->pscan.lock);
1086 : if (rp->saw_pptr_updates)
1087 : error = -ESTALE;
1088 : mutex_unlock(&rp->pscan.lock);
1089 351 : return error;
1090 351 : }
1091 0 :
1092 351 : /* Copy all the non-pptr extended attributes into the temporary file. */
1093 351 : STATIC int
1094 : xrep_parent_copy_xattrs(
1095 : struct xrep_parent *rp)
1096 : {
1097 : struct xfs_scrub *sc = rp->sc;
1098 2263982 : int error;
1099 :
1100 : /*
1101 2263982 : * Clear the pptr updates flag. We hold sc->ip ILOCKed, so there
1102 2263982 : * can't be any parent pointer updates in progress.
1103 : */
1104 : mutex_lock(&rp->pscan.lock);
1105 : rp->saw_pptr_updates = false;
1106 : mutex_unlock(&rp->pscan.lock);
1107 :
1108 2263982 : /* Copy xattrs, stopping periodically to flush the incore buffers. */
1109 2264030 : error = xchk_xattr_walk(sc, sc->ip, xrep_parent_stash_xattr,
1110 2264030 : xrep_parent_try_flush_xattrs, rp);
1111 : if (error && error != -ESTALE)
1112 : return error;
1113 2263958 :
1114 : if (error == -ESTALE) {
1115 2263972 : /*
1116 : * The xattr copy collided with a parent pointer update.
1117 : * Restart the copy, but this time hold the ILOCK all the way
1118 2263972 : * to the end to lock out any directory parent pointer updates.
1119 : */
1120 : error = xchk_xattr_walk(sc, sc->ip, xrep_parent_stash_xattr,
1121 : NULL, rp);
1122 : if (error)
1123 : return error;
1124 0 : }
1125 :
1126 0 : /* Flush any remaining stashed xattrs to the temporary file. */
1127 : if (xfarray_bytes(rp->xattr_records) == 0)
1128 : return 0;
1129 :
1130 : return xrep_parent_flush_xattrs(rp);
1131 2263972 : }
1132 :
1133 : /*
1134 145902 : * Ensure that @sc->ip and @sc->tempip both have attribute forks before we
1135 : * head into the attr fork swap transaction. All files on a filesystem with
1136 : * parent pointers must have an attr fork because the parent pointer code
1137 : * does not itself add attribute forks.
1138 : *
1139 : * Note: Unlinkable unlinked files don't need one, but the overhead of having
1140 : * an unnecessary attr fork is not justified by the additional code complexity
1141 : * that would be needed to track that state correctly.
1142 : */
1143 : STATIC int
1144 : xrep_parent_ensure_attr_fork(
1145 : struct xrep_parent *rp)
1146 : {
1147 : struct xfs_scrub *sc = rp->sc;
1148 2263399 : int error;
1149 :
1150 : error = xfs_attr_add_fork(sc->tempip,
1151 2263399 : sizeof(struct xfs_attr_sf_hdr), 1);
1152 2263399 : if (error)
1153 : return error;
1154 2263399 : return xfs_attr_add_fork(sc->ip, sizeof(struct xfs_attr_sf_hdr), 1);
1155 : }
1156 2263222 :
1157 : /*
1158 2263289 : * Finish replaying stashed parent pointer updates, allocate a transaction for
1159 : * swapping extents, and take the ILOCKs of both files before we commit the new
1160 : * attribute structure.
1161 : */
1162 : STATIC int
1163 : xrep_parent_finalize_tempfile(
1164 : struct xrep_parent *rp)
1165 : {
1166 : struct xfs_scrub *sc = rp->sc;
1167 2263356 : int error;
1168 :
1169 : error = xrep_parent_replay_updates(rp);
1170 2263356 : if (error)
1171 2263356 : return error;
1172 :
1173 2263356 : error = xrep_parent_ensure_attr_fork(rp);
1174 2264025 : if (error)
1175 : return error;
1176 :
1177 2263936 : error = xrep_tempswap_trans_alloc(sc, XFS_ATTR_FORK, &rp->tx);
1178 2263896 : if (error)
1179 : return error;
1180 :
1181 2263787 : /*
1182 2251801 : * We rely on the caller's hold on @sc->ip's IOLOCK_EXCL to quiesce all
1183 : * possible parent pointer updates during the time when we did not hold
1184 : * the ILOCK. There should not be any pptr updates to replay, but
1185 : * check anyway.
1186 : */
1187 : if (xfarray_length(rp->pptr_recs) != 0) {
1188 : ASSERT(xfarray_length(rp->pptr_recs) == 0);
1189 : return -EFSCORRUPTED;
1190 : }
1191 2251679 :
1192 0 : return 0;
1193 0 : }
1194 :
1195 : /*
1196 : * Replay all the stashed parent pointers into the temporary file, copy all
1197 : * the non-pptr xattrs from the file being repaired into the temporary file,
1198 : * and swap the extents atomically.
1199 : */
1200 : STATIC int
1201 : xrep_parent_rebuild_pptrs(
1202 : struct xrep_parent *rp)
1203 : {
1204 : struct xfs_scrub *sc = rp->sc;
1205 2263957 : xfs_ino_t parent_ino = NULLFSINO;
1206 : int error;
1207 :
1208 2263957 : /*
1209 2263957 : * Copy non-ppttr xattrs from the file being repaired into the
1210 2263957 : * temporary file's xattr structure. We hold sc->ip's IOLOCK, which
1211 : * prevents setxattr/removexattr calls from occurring, but renames
1212 : * update the parent pointers without holding IOLOCK. If we detect
1213 : * stale attr structures, we restart the scan but only flush at the
1214 : * end.
1215 : */
1216 : error = xrep_parent_copy_xattrs(rp);
1217 : if (error)
1218 : return error;
1219 :
1220 2263957 : /*
1221 2264041 : * Cancel the empty transaction that we used to walk and copy attrs,
1222 : * and drop the ILOCK so that we can take the IOLOCK on the temporary
1223 : * file. We still hold sc->ip's IOLOCK.
1224 : */
1225 : xchk_trans_cancel(sc);
1226 : xchk_iunlock(sc, XFS_ILOCK_EXCL);
1227 :
1228 : error = xrep_tempfile_iolock_polled(sc);
1229 2264035 : if (error)
1230 2263561 : return error;
1231 :
1232 2263625 : error = xrep_parent_finalize_tempfile(rp);
1233 2263242 : if (error)
1234 : return error;
1235 :
1236 2263212 : /* Last chance to abort before we start committing pptr fixes. */
1237 2255505 : if (xchk_should_terminate(sc, &error))
1238 : return error;
1239 :
1240 : if (xchk_iscan_aborted(&rp->pscan.iscan))
1241 2256697 : return -ECANCELED;
1242 0 :
1243 : /*
1244 2252668 : * Swap the attr fork and junk the old attr fork contents, which are
1245 : * now in the tempfile.
1246 : */
1247 : error = xrep_xattr_swap(sc, &rp->tx);
1248 : if (error)
1249 : return error;
1250 : error = xrep_xattr_reset_tempfile_fork(sc);
1251 2251883 : if (error)
1252 2261108 : return error;
1253 :
1254 2261067 : /*
1255 2262199 : * Roll transaction to detach both inodes from the transaction, then
1256 : * drop the ILOCK of the temporary file since we no longer need it.
1257 : */
1258 : error = xfs_trans_roll(&sc->tp);
1259 : if (error)
1260 : return error;
1261 : xrep_tempfile_iunlock(sc);
1262 2261598 :
1263 2262570 : /*
1264 : * We've committed the new parent pointers. Find at least one parent
1265 2262562 : * so that we can decide if we're moving this file to the orphanage.
1266 : * For this purpose, root directories are their own parents.
1267 : */
1268 : if (sc->ip == sc->mp->m_rootip || sc->ip == sc->mp->m_metadirip) {
1269 : xrep_findparent_scan_found(&rp->pscan, sc->ip->i_ino);
1270 : } else {
1271 : error = xrep_findparent_from_pptrs(sc, &parent_ino);
1272 2259569 : if (error)
1273 3654 : return error;
1274 : if (parent_ino != NULLFSINO)
1275 2255915 : xrep_findparent_scan_found(&rp->pscan, parent_ino);
1276 2245027 : }
1277 : return 0;
1278 2245027 : }
1279 2243975 :
1280 : /*
1281 : * Commit the new parent pointer structure (currently only the dotdot entry) to
1282 : * the file that we're repairing.
1283 : */
1284 : STATIC int
1285 : xrep_parent_rebuild_tree(
1286 : struct xrep_parent *rp)
1287 : {
1288 : int error;
1289 2308496 :
1290 : if (xfs_has_parent(rp->sc->mp)) {
1291 : error = xrep_parent_rebuild_pptrs(rp);
1292 2308496 : if (error)
1293 : return error;
1294 2308496 : }
1295 2264080 :
1296 2258644 : if (rp->pscan.parent_ino == NULLFSINO) {
1297 : if (xrep_orphanage_can_adopt(rp->sc))
1298 : return xrep_parent_move_to_orphanage(rp);
1299 : return -EFSCORRUPTED;
1300 2303048 : }
1301 24 :
1302 : if (S_ISDIR(VFS_I(rp->sc->ip)->i_mode))
1303 2303024 : return xrep_parent_reset_dotdot(rp);
1304 :
1305 : return 0;
1306 252492 : }
1307 :
1308 : /* Set up the filesystem scan so we can look for parents. */
1309 : STATIC int
1310 : xrep_parent_setup_scan(
1311 2308618 : struct xrep_parent *rp)
1312 : {
1313 : struct xfs_scrub *sc = rp->sc;
1314 2308618 : char *descr;
1315 2308618 : struct xfs_da_geometry *geo = sc->mp->m_attr_geo;
1316 2308618 : int max_len;
1317 2308618 : int error;
1318 2308618 :
1319 : if (!xfs_has_parent(sc->mp))
1320 2308618 : return xrep_findparent_scan_start(sc, &rp->pscan);
1321 44283 :
1322 : /* Buffers for copying non-pptr attrs to the tempfile */
1323 : rp->xattr_name = kvmalloc(XATTR_NAME_MAX + 1, XCHK_GFP_FLAGS);
1324 2264335 : if (!rp->xattr_name)
1325 2264335 : return -ENOMEM;
1326 :
1327 : /*
1328 : * Allocate enough memory to handle loading local attr values from the
1329 : * xfblob data while flushing stashed attrs to the temporary file.
1330 : * We only realloc the buffer when salvaging remote attr values, so
1331 : * TRY_HARDER means we allocate the maximal attr value size.
1332 : */
1333 : if (sc->flags & XCHK_TRY_HARDER)
1334 2264335 : max_len = XATTR_SIZE_MAX;
1335 : else
1336 : max_len = xfs_attr_leaf_entsize_local_max(geo->blksize);
1337 2264314 : error = xrep_parent_alloc_xattr_value(rp, max_len);
1338 2264335 : if (error)
1339 2264333 : goto out_xattr_name;
1340 0 :
1341 : /* Set up some staging memory for logging parent pointer updates. */
1342 : descr = xchk_xfile_ino_descr(sc, "parent pointer entries");
1343 2264333 : error = xfarray_create(descr, 0, sizeof(struct xrep_pptr),
1344 2264324 : &rp->pptr_recs);
1345 : kfree(descr);
1346 2264340 : if (error)
1347 2264343 : goto out_xattr_value;
1348 0 :
1349 : descr = xchk_xfile_ino_descr(sc, "parent pointer names");
1350 2264343 : error = xfblob_create(descr, &rp->pptr_names);
1351 2264330 : kfree(descr);
1352 2264348 : if (error)
1353 2264345 : goto out_recs;
1354 0 :
1355 : /* Set up some storage for copying attrs before the swap */
1356 : descr = xchk_xfile_ino_descr(sc,
1357 2264345 : "parent pointer retained xattr entries");
1358 : error = xfarray_create(descr, 0, sizeof(struct xrep_parent_xattr),
1359 2264340 : &rp->xattr_records);
1360 : kfree(descr);
1361 2264345 : if (error)
1362 2264338 : goto out_names;
1363 0 :
1364 : descr = xchk_xfile_ino_descr(sc,
1365 2264338 : "parent pointer retained xattr values");
1366 : error = xfblob_create(descr, &rp->xattr_blobs);
1367 2264338 : kfree(descr);
1368 2264348 : if (error)
1369 2264347 : goto out_attr_keys;
1370 0 :
1371 : error = __xrep_findparent_scan_start(sc, &rp->pscan,
1372 2264347 : xrep_parent_live_update);
1373 : if (error)
1374 2264348 : goto out_attr_values;
1375 0 :
1376 : return 0;
1377 :
1378 : out_attr_values:
1379 : xfblob_destroy(rp->xattr_blobs);
1380 0 : rp->xattr_blobs = NULL;
1381 0 : out_attr_keys:
1382 0 : xfarray_destroy(rp->xattr_records);
1383 0 : rp->xattr_records = NULL;
1384 0 : out_names:
1385 0 : xfblob_destroy(rp->pptr_names);
1386 0 : rp->pptr_names = NULL;
1387 0 : out_recs:
1388 0 : xfarray_destroy(rp->pptr_recs);
1389 0 : rp->pptr_recs = NULL;
1390 0 : out_xattr_value:
1391 0 : kvfree(rp->xattr_value);
1392 0 : rp->xattr_value = NULL;
1393 0 : out_xattr_name:
1394 0 : kvfree(rp->xattr_name);
1395 0 : rp->xattr_name = NULL;
1396 0 : return error;
1397 0 : }
1398 :
1399 : int
1400 : xrep_parent(
1401 2313642 : struct xfs_scrub *sc)
1402 : {
1403 : struct xrep_parent *rp = sc->buf;
1404 2313642 : int error;
1405 2313642 :
1406 : /*
1407 : * When the parent pointers feature is enabled, repairs are committed
1408 : * by atomically committing a new xattr structure and reaping the old
1409 : * attr fork. Reaping requires rmap to be enabled.
1410 : */
1411 : if (xfs_has_parent(sc->mp) && !xfs_has_rmapbt(sc->mp))
1412 2313642 : return -EOPNOTSUPP;
1413 :
1414 : error = xrep_parent_setup_scan(rp);
1415 2308607 : if (error)
1416 2308631 : return error;
1417 :
1418 : if (xfs_has_parent(sc->mp))
1419 2308631 : error = xrep_parent_scan_dirtree(rp);
1420 2264348 : else
1421 : error = xrep_parent_find_dotdot(rp);
1422 44283 : if (error)
1423 2308523 : goto out_teardown;
1424 278 :
1425 : /* Last chance to abort before we start committing dotdot fixes. */
1426 : if (xchk_should_terminate(sc, &error))
1427 2308245 : goto out_teardown;
1428 0 :
1429 : error = xrep_parent_rebuild_tree(rp);
1430 2308316 : if (error)
1431 2304298 : goto out_teardown;
1432 :
1433 : out_teardown:
1434 2304298 : xrep_parent_teardown(rp);
1435 2304576 : return error;
1436 2307278 : }
|