]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
3e57ecf6 | 3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
7b718769 | 4 | * All Rights Reserved. |
1da177e4 | 5 | */ |
f0e28280 | 6 | #include <linux/iversion.h> |
40ebd81d | 7 | |
1da177e4 | 8 | #include "xfs.h" |
a844f451 | 9 | #include "xfs_fs.h" |
70a9883c | 10 | #include "xfs_shared.h" |
239880ef DC |
11 | #include "xfs_format.h" |
12 | #include "xfs_log_format.h" | |
13 | #include "xfs_trans_resv.h" | |
1da177e4 | 14 | #include "xfs_sb.h" |
1da177e4 | 15 | #include "xfs_mount.h" |
3ab78df2 | 16 | #include "xfs_defer.h" |
a4fbe6ab | 17 | #include "xfs_inode.h" |
c24b5dfa | 18 | #include "xfs_dir2.h" |
c24b5dfa | 19 | #include "xfs_attr.h" |
239880ef DC |
20 | #include "xfs_trans_space.h" |
21 | #include "xfs_trans.h" | |
1da177e4 | 22 | #include "xfs_buf_item.h" |
a844f451 | 23 | #include "xfs_inode_item.h" |
a844f451 NS |
24 | #include "xfs_ialloc.h" |
25 | #include "xfs_bmap.h" | |
68988114 | 26 | #include "xfs_bmap_util.h" |
e9e899a2 | 27 | #include "xfs_errortag.h" |
1da177e4 | 28 | #include "xfs_error.h" |
1da177e4 | 29 | #include "xfs_quota.h" |
2a82b8be | 30 | #include "xfs_filestream.h" |
0b1b213f | 31 | #include "xfs_trace.h" |
33479e05 | 32 | #include "xfs_icache.h" |
c24b5dfa | 33 | #include "xfs_symlink.h" |
239880ef DC |
34 | #include "xfs_trans_priv.h" |
35 | #include "xfs_log.h" | |
a4fbe6ab | 36 | #include "xfs_bmap_btree.h" |
aa8968f2 | 37 | #include "xfs_reflink.h" |
1da177e4 | 38 | |
1da177e4 | 39 | kmem_zone_t *xfs_inode_zone; |
1da177e4 LT |
40 | |
41 | /* | |
8f04c47a | 42 | * Used in xfs_itruncate_extents(). This is the maximum number of extents |
1da177e4 LT |
43 | * freed from a file in a single transaction. |
44 | */ | |
45 | #define XFS_ITRUNC_MAX_EXTENTS 2 | |
46 | ||
54d7b5c1 DC |
47 | STATIC int xfs_iflush_int(struct xfs_inode *, struct xfs_buf *); |
48 | STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *); | |
49 | STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *); | |
ab297431 | 50 | |
2a0ec1d9 DC |
51 | /* |
52 | * helper function to extract extent size hint from inode | |
53 | */ | |
54 | xfs_extlen_t | |
55 | xfs_get_extsz_hint( | |
56 | struct xfs_inode *ip) | |
57 | { | |
bdb2ed2d CH |
58 | /* |
59 | * No point in aligning allocations if we need to COW to actually | |
60 | * write to them. | |
61 | */ | |
62 | if (xfs_is_always_cow_inode(ip)) | |
63 | return 0; | |
2a0ec1d9 DC |
64 | if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize) |
65 | return ip->i_d.di_extsize; | |
66 | if (XFS_IS_REALTIME_INODE(ip)) | |
67 | return ip->i_mount->m_sb.sb_rextsize; | |
68 | return 0; | |
69 | } | |
70 | ||
f7ca3522 DW |
71 | /* |
72 | * Helper function to extract CoW extent size hint from inode. | |
73 | * Between the extent size hint and the CoW extent size hint, we | |
e153aa79 DW |
74 | * return the greater of the two. If the value is zero (automatic), |
75 | * use the default size. | |
f7ca3522 DW |
76 | */ |
77 | xfs_extlen_t | |
78 | xfs_get_cowextsz_hint( | |
79 | struct xfs_inode *ip) | |
80 | { | |
81 | xfs_extlen_t a, b; | |
82 | ||
83 | a = 0; | |
84 | if (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) | |
85 | a = ip->i_d.di_cowextsize; | |
86 | b = xfs_get_extsz_hint(ip); | |
87 | ||
e153aa79 DW |
88 | a = max(a, b); |
89 | if (a == 0) | |
90 | return XFS_DEFAULT_COWEXTSZ_HINT; | |
91 | return a; | |
f7ca3522 DW |
92 | } |
93 | ||
fa96acad | 94 | /* |
efa70be1 CH |
95 | * These two are wrapper routines around the xfs_ilock() routine used to |
96 | * centralize some grungy code. They are used in places that wish to lock the | |
97 | * inode solely for reading the extents. The reason these places can't just | |
98 | * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to | |
99 | * bringing in of the extents from disk for a file in b-tree format. If the | |
100 | * inode is in b-tree format, then we need to lock the inode exclusively until | |
101 | * the extents are read in. Locking it exclusively all the time would limit | |
102 | * our parallelism unnecessarily, though. What we do instead is check to see | |
103 | * if the extents have been read in yet, and only lock the inode exclusively | |
104 | * if they have not. | |
fa96acad | 105 | * |
efa70be1 | 106 | * The functions return a value which should be given to the corresponding |
01f4f327 | 107 | * xfs_iunlock() call. |
fa96acad DC |
108 | */ |
109 | uint | |
309ecac8 CH |
110 | xfs_ilock_data_map_shared( |
111 | struct xfs_inode *ip) | |
fa96acad | 112 | { |
309ecac8 | 113 | uint lock_mode = XFS_ILOCK_SHARED; |
fa96acad | 114 | |
f7e67b20 | 115 | if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE && |
309ecac8 | 116 | (ip->i_df.if_flags & XFS_IFEXTENTS) == 0) |
fa96acad | 117 | lock_mode = XFS_ILOCK_EXCL; |
fa96acad | 118 | xfs_ilock(ip, lock_mode); |
fa96acad DC |
119 | return lock_mode; |
120 | } | |
121 | ||
efa70be1 CH |
122 | uint |
123 | xfs_ilock_attr_map_shared( | |
124 | struct xfs_inode *ip) | |
fa96acad | 125 | { |
efa70be1 CH |
126 | uint lock_mode = XFS_ILOCK_SHARED; |
127 | ||
f7e67b20 CH |
128 | if (ip->i_afp && |
129 | ip->i_afp->if_format == XFS_DINODE_FMT_BTREE && | |
efa70be1 CH |
130 | (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0) |
131 | lock_mode = XFS_ILOCK_EXCL; | |
132 | xfs_ilock(ip, lock_mode); | |
133 | return lock_mode; | |
fa96acad DC |
134 | } |
135 | ||
136 | /* | |
65523218 CH |
137 | * In addition to i_rwsem in the VFS inode, the xfs inode contains 2 |
138 | * multi-reader locks: i_mmap_lock and the i_lock. This routine allows | |
139 | * various combinations of the locks to be obtained. | |
fa96acad | 140 | * |
653c60b6 DC |
141 | * The 3 locks should always be ordered so that the IO lock is obtained first, |
142 | * the mmap lock second and the ilock last in order to prevent deadlock. | |
fa96acad | 143 | * |
653c60b6 DC |
144 | * Basic locking order: |
145 | * | |
65523218 | 146 | * i_rwsem -> i_mmap_lock -> page_lock -> i_ilock |
653c60b6 | 147 | * |
c1e8d7c6 | 148 | * mmap_lock locking order: |
653c60b6 | 149 | * |
c1e8d7c6 ML |
150 | * i_rwsem -> page lock -> mmap_lock |
151 | * mmap_lock -> i_mmap_lock -> page_lock | |
653c60b6 | 152 | * |
c1e8d7c6 | 153 | * The difference in mmap_lock locking order mean that we cannot hold the |
653c60b6 | 154 | * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can |
c1e8d7c6 | 155 | * fault in pages during copy in/out (for buffered IO) or require the mmap_lock |
653c60b6 | 156 | * in get_user_pages() to map the user pages into the kernel address space for |
65523218 | 157 | * direct IO. Similarly the i_rwsem cannot be taken inside a page fault because |
c1e8d7c6 | 158 | * page faults already hold the mmap_lock. |
653c60b6 DC |
159 | * |
160 | * Hence to serialise fully against both syscall and mmap based IO, we need to | |
65523218 | 161 | * take both the i_rwsem and the i_mmap_lock. These locks should *only* be both |
653c60b6 DC |
162 | * taken in places where we need to invalidate the page cache in a race |
163 | * free manner (e.g. truncate, hole punch and other extent manipulation | |
164 | * functions). | |
fa96acad DC |
165 | */ |
166 | void | |
167 | xfs_ilock( | |
168 | xfs_inode_t *ip, | |
169 | uint lock_flags) | |
170 | { | |
171 | trace_xfs_ilock(ip, lock_flags, _RET_IP_); | |
172 | ||
173 | /* | |
174 | * You can't set both SHARED and EXCL for the same lock, | |
175 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
176 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
177 | */ | |
178 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
179 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
653c60b6 DC |
180 | ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) != |
181 | (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)); | |
fa96acad DC |
182 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
183 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
0952c818 | 184 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0); |
fa96acad | 185 | |
65523218 CH |
186 | if (lock_flags & XFS_IOLOCK_EXCL) { |
187 | down_write_nested(&VFS_I(ip)->i_rwsem, | |
188 | XFS_IOLOCK_DEP(lock_flags)); | |
189 | } else if (lock_flags & XFS_IOLOCK_SHARED) { | |
190 | down_read_nested(&VFS_I(ip)->i_rwsem, | |
191 | XFS_IOLOCK_DEP(lock_flags)); | |
192 | } | |
fa96acad | 193 | |
653c60b6 DC |
194 | if (lock_flags & XFS_MMAPLOCK_EXCL) |
195 | mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags)); | |
196 | else if (lock_flags & XFS_MMAPLOCK_SHARED) | |
197 | mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags)); | |
198 | ||
fa96acad DC |
199 | if (lock_flags & XFS_ILOCK_EXCL) |
200 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); | |
201 | else if (lock_flags & XFS_ILOCK_SHARED) | |
202 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); | |
203 | } | |
204 | ||
205 | /* | |
206 | * This is just like xfs_ilock(), except that the caller | |
207 | * is guaranteed not to sleep. It returns 1 if it gets | |
208 | * the requested locks and 0 otherwise. If the IO lock is | |
209 | * obtained but the inode lock cannot be, then the IO lock | |
210 | * is dropped before returning. | |
211 | * | |
212 | * ip -- the inode being locked | |
213 | * lock_flags -- this parameter indicates the inode's locks to be | |
214 | * to be locked. See the comment for xfs_ilock() for a list | |
215 | * of valid values. | |
216 | */ | |
217 | int | |
218 | xfs_ilock_nowait( | |
219 | xfs_inode_t *ip, | |
220 | uint lock_flags) | |
221 | { | |
222 | trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_); | |
223 | ||
224 | /* | |
225 | * You can't set both SHARED and EXCL for the same lock, | |
226 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
227 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
228 | */ | |
229 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
230 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
653c60b6 DC |
231 | ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) != |
232 | (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)); | |
fa96acad DC |
233 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
234 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
0952c818 | 235 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0); |
fa96acad DC |
236 | |
237 | if (lock_flags & XFS_IOLOCK_EXCL) { | |
65523218 | 238 | if (!down_write_trylock(&VFS_I(ip)->i_rwsem)) |
fa96acad DC |
239 | goto out; |
240 | } else if (lock_flags & XFS_IOLOCK_SHARED) { | |
65523218 | 241 | if (!down_read_trylock(&VFS_I(ip)->i_rwsem)) |
fa96acad DC |
242 | goto out; |
243 | } | |
653c60b6 DC |
244 | |
245 | if (lock_flags & XFS_MMAPLOCK_EXCL) { | |
246 | if (!mrtryupdate(&ip->i_mmaplock)) | |
247 | goto out_undo_iolock; | |
248 | } else if (lock_flags & XFS_MMAPLOCK_SHARED) { | |
249 | if (!mrtryaccess(&ip->i_mmaplock)) | |
250 | goto out_undo_iolock; | |
251 | } | |
252 | ||
fa96acad DC |
253 | if (lock_flags & XFS_ILOCK_EXCL) { |
254 | if (!mrtryupdate(&ip->i_lock)) | |
653c60b6 | 255 | goto out_undo_mmaplock; |
fa96acad DC |
256 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
257 | if (!mrtryaccess(&ip->i_lock)) | |
653c60b6 | 258 | goto out_undo_mmaplock; |
fa96acad DC |
259 | } |
260 | return 1; | |
261 | ||
653c60b6 DC |
262 | out_undo_mmaplock: |
263 | if (lock_flags & XFS_MMAPLOCK_EXCL) | |
264 | mrunlock_excl(&ip->i_mmaplock); | |
265 | else if (lock_flags & XFS_MMAPLOCK_SHARED) | |
266 | mrunlock_shared(&ip->i_mmaplock); | |
267 | out_undo_iolock: | |
fa96acad | 268 | if (lock_flags & XFS_IOLOCK_EXCL) |
65523218 | 269 | up_write(&VFS_I(ip)->i_rwsem); |
fa96acad | 270 | else if (lock_flags & XFS_IOLOCK_SHARED) |
65523218 | 271 | up_read(&VFS_I(ip)->i_rwsem); |
653c60b6 | 272 | out: |
fa96acad DC |
273 | return 0; |
274 | } | |
275 | ||
276 | /* | |
277 | * xfs_iunlock() is used to drop the inode locks acquired with | |
278 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass | |
279 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so | |
280 | * that we know which locks to drop. | |
281 | * | |
282 | * ip -- the inode being unlocked | |
283 | * lock_flags -- this parameter indicates the inode's locks to be | |
284 | * to be unlocked. See the comment for xfs_ilock() for a list | |
285 | * of valid values for this parameter. | |
286 | * | |
287 | */ | |
288 | void | |
289 | xfs_iunlock( | |
290 | xfs_inode_t *ip, | |
291 | uint lock_flags) | |
292 | { | |
293 | /* | |
294 | * You can't set both SHARED and EXCL for the same lock, | |
295 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
296 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
297 | */ | |
298 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
299 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
653c60b6 DC |
300 | ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) != |
301 | (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)); | |
fa96acad DC |
302 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
303 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
0952c818 | 304 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0); |
fa96acad DC |
305 | ASSERT(lock_flags != 0); |
306 | ||
307 | if (lock_flags & XFS_IOLOCK_EXCL) | |
65523218 | 308 | up_write(&VFS_I(ip)->i_rwsem); |
fa96acad | 309 | else if (lock_flags & XFS_IOLOCK_SHARED) |
65523218 | 310 | up_read(&VFS_I(ip)->i_rwsem); |
fa96acad | 311 | |
653c60b6 DC |
312 | if (lock_flags & XFS_MMAPLOCK_EXCL) |
313 | mrunlock_excl(&ip->i_mmaplock); | |
314 | else if (lock_flags & XFS_MMAPLOCK_SHARED) | |
315 | mrunlock_shared(&ip->i_mmaplock); | |
316 | ||
fa96acad DC |
317 | if (lock_flags & XFS_ILOCK_EXCL) |
318 | mrunlock_excl(&ip->i_lock); | |
319 | else if (lock_flags & XFS_ILOCK_SHARED) | |
320 | mrunlock_shared(&ip->i_lock); | |
321 | ||
322 | trace_xfs_iunlock(ip, lock_flags, _RET_IP_); | |
323 | } | |
324 | ||
325 | /* | |
326 | * give up write locks. the i/o lock cannot be held nested | |
327 | * if it is being demoted. | |
328 | */ | |
329 | void | |
330 | xfs_ilock_demote( | |
331 | xfs_inode_t *ip, | |
332 | uint lock_flags) | |
333 | { | |
653c60b6 DC |
334 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)); |
335 | ASSERT((lock_flags & | |
336 | ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); | |
fa96acad DC |
337 | |
338 | if (lock_flags & XFS_ILOCK_EXCL) | |
339 | mrdemote(&ip->i_lock); | |
653c60b6 DC |
340 | if (lock_flags & XFS_MMAPLOCK_EXCL) |
341 | mrdemote(&ip->i_mmaplock); | |
fa96acad | 342 | if (lock_flags & XFS_IOLOCK_EXCL) |
65523218 | 343 | downgrade_write(&VFS_I(ip)->i_rwsem); |
fa96acad DC |
344 | |
345 | trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_); | |
346 | } | |
347 | ||
742ae1e3 | 348 | #if defined(DEBUG) || defined(XFS_WARN) |
fa96acad DC |
349 | int |
350 | xfs_isilocked( | |
351 | xfs_inode_t *ip, | |
352 | uint lock_flags) | |
353 | { | |
354 | if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { | |
355 | if (!(lock_flags & XFS_ILOCK_SHARED)) | |
356 | return !!ip->i_lock.mr_writer; | |
357 | return rwsem_is_locked(&ip->i_lock.mr_lock); | |
358 | } | |
359 | ||
653c60b6 DC |
360 | if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) { |
361 | if (!(lock_flags & XFS_MMAPLOCK_SHARED)) | |
362 | return !!ip->i_mmaplock.mr_writer; | |
363 | return rwsem_is_locked(&ip->i_mmaplock.mr_lock); | |
364 | } | |
365 | ||
fa96acad DC |
366 | if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) { |
367 | if (!(lock_flags & XFS_IOLOCK_SHARED)) | |
65523218 CH |
368 | return !debug_locks || |
369 | lockdep_is_held_type(&VFS_I(ip)->i_rwsem, 0); | |
370 | return rwsem_is_locked(&VFS_I(ip)->i_rwsem); | |
fa96acad DC |
371 | } |
372 | ||
373 | ASSERT(0); | |
374 | return 0; | |
375 | } | |
376 | #endif | |
377 | ||
b6a9947e DC |
378 | /* |
379 | * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when | |
380 | * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined | |
381 | * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build | |
382 | * errors and warnings. | |
383 | */ | |
384 | #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP) | |
3403ccc0 DC |
385 | static bool |
386 | xfs_lockdep_subclass_ok( | |
387 | int subclass) | |
388 | { | |
389 | return subclass < MAX_LOCKDEP_SUBCLASSES; | |
390 | } | |
391 | #else | |
392 | #define xfs_lockdep_subclass_ok(subclass) (true) | |
393 | #endif | |
394 | ||
c24b5dfa | 395 | /* |
653c60b6 | 396 | * Bump the subclass so xfs_lock_inodes() acquires each lock with a different |
0952c818 DC |
397 | * value. This can be called for any type of inode lock combination, including |
398 | * parent locking. Care must be taken to ensure we don't overrun the subclass | |
399 | * storage fields in the class mask we build. | |
c24b5dfa DC |
400 | */ |
401 | static inline int | |
402 | xfs_lock_inumorder(int lock_mode, int subclass) | |
403 | { | |
0952c818 DC |
404 | int class = 0; |
405 | ||
406 | ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP | | |
407 | XFS_ILOCK_RTSUM))); | |
3403ccc0 | 408 | ASSERT(xfs_lockdep_subclass_ok(subclass)); |
0952c818 | 409 | |
653c60b6 | 410 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) { |
0952c818 | 411 | ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS); |
0952c818 | 412 | class += subclass << XFS_IOLOCK_SHIFT; |
653c60b6 DC |
413 | } |
414 | ||
415 | if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) { | |
0952c818 DC |
416 | ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS); |
417 | class += subclass << XFS_MMAPLOCK_SHIFT; | |
653c60b6 DC |
418 | } |
419 | ||
0952c818 DC |
420 | if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) { |
421 | ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS); | |
422 | class += subclass << XFS_ILOCK_SHIFT; | |
423 | } | |
c24b5dfa | 424 | |
0952c818 | 425 | return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class; |
c24b5dfa DC |
426 | } |
427 | ||
428 | /* | |
95afcf5c DC |
429 | * The following routine will lock n inodes in exclusive mode. We assume the |
430 | * caller calls us with the inodes in i_ino order. | |
c24b5dfa | 431 | * |
95afcf5c DC |
432 | * We need to detect deadlock where an inode that we lock is in the AIL and we |
433 | * start waiting for another inode that is locked by a thread in a long running | |
434 | * transaction (such as truncate). This can result in deadlock since the long | |
435 | * running trans might need to wait for the inode we just locked in order to | |
436 | * push the tail and free space in the log. | |
0952c818 DC |
437 | * |
438 | * xfs_lock_inodes() can only be used to lock one type of lock at a time - | |
439 | * the iolock, the mmaplock or the ilock, but not more than one at a time. If we | |
440 | * lock more than one at a time, lockdep will report false positives saying we | |
441 | * have violated locking orders. | |
c24b5dfa | 442 | */ |
0d5a75e9 | 443 | static void |
c24b5dfa | 444 | xfs_lock_inodes( |
efe2330f CH |
445 | struct xfs_inode **ips, |
446 | int inodes, | |
447 | uint lock_mode) | |
c24b5dfa | 448 | { |
efe2330f CH |
449 | int attempts = 0, i, j, try_lock; |
450 | struct xfs_log_item *lp; | |
c24b5dfa | 451 | |
0952c818 DC |
452 | /* |
453 | * Currently supports between 2 and 5 inodes with exclusive locking. We | |
454 | * support an arbitrary depth of locking here, but absolute limits on | |
455 | * inodes depend on the the type of locking and the limits placed by | |
456 | * lockdep annotations in xfs_lock_inumorder. These are all checked by | |
457 | * the asserts. | |
458 | */ | |
95afcf5c | 459 | ASSERT(ips && inodes >= 2 && inodes <= 5); |
0952c818 DC |
460 | ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL | |
461 | XFS_ILOCK_EXCL)); | |
462 | ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED | | |
463 | XFS_ILOCK_SHARED))); | |
0952c818 DC |
464 | ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) || |
465 | inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1); | |
466 | ASSERT(!(lock_mode & XFS_ILOCK_EXCL) || | |
467 | inodes <= XFS_ILOCK_MAX_SUBCLASS + 1); | |
468 | ||
469 | if (lock_mode & XFS_IOLOCK_EXCL) { | |
470 | ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL))); | |
471 | } else if (lock_mode & XFS_MMAPLOCK_EXCL) | |
472 | ASSERT(!(lock_mode & XFS_ILOCK_EXCL)); | |
c24b5dfa DC |
473 | |
474 | try_lock = 0; | |
475 | i = 0; | |
c24b5dfa DC |
476 | again: |
477 | for (; i < inodes; i++) { | |
478 | ASSERT(ips[i]); | |
479 | ||
95afcf5c | 480 | if (i && (ips[i] == ips[i - 1])) /* Already locked */ |
c24b5dfa DC |
481 | continue; |
482 | ||
483 | /* | |
95afcf5c DC |
484 | * If try_lock is not set yet, make sure all locked inodes are |
485 | * not in the AIL. If any are, set try_lock to be used later. | |
c24b5dfa | 486 | */ |
c24b5dfa DC |
487 | if (!try_lock) { |
488 | for (j = (i - 1); j >= 0 && !try_lock; j--) { | |
b3b14aac | 489 | lp = &ips[j]->i_itemp->ili_item; |
22525c17 | 490 | if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) |
c24b5dfa | 491 | try_lock++; |
c24b5dfa DC |
492 | } |
493 | } | |
494 | ||
495 | /* | |
496 | * If any of the previous locks we have locked is in the AIL, | |
497 | * we must TRY to get the second and subsequent locks. If | |
498 | * we can't get any, we must release all we have | |
499 | * and try again. | |
500 | */ | |
95afcf5c DC |
501 | if (!try_lock) { |
502 | xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); | |
503 | continue; | |
504 | } | |
505 | ||
506 | /* try_lock means we have an inode locked that is in the AIL. */ | |
507 | ASSERT(i != 0); | |
508 | if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) | |
509 | continue; | |
c24b5dfa | 510 | |
95afcf5c DC |
511 | /* |
512 | * Unlock all previous guys and try again. xfs_iunlock will try | |
513 | * to push the tail if the inode is in the AIL. | |
514 | */ | |
515 | attempts++; | |
516 | for (j = i - 1; j >= 0; j--) { | |
c24b5dfa | 517 | /* |
95afcf5c DC |
518 | * Check to see if we've already unlocked this one. Not |
519 | * the first one going back, and the inode ptr is the | |
520 | * same. | |
c24b5dfa | 521 | */ |
95afcf5c DC |
522 | if (j != (i - 1) && ips[j] == ips[j + 1]) |
523 | continue; | |
c24b5dfa | 524 | |
95afcf5c DC |
525 | xfs_iunlock(ips[j], lock_mode); |
526 | } | |
c24b5dfa | 527 | |
95afcf5c DC |
528 | if ((attempts % 5) == 0) { |
529 | delay(1); /* Don't just spin the CPU */ | |
c24b5dfa | 530 | } |
95afcf5c DC |
531 | i = 0; |
532 | try_lock = 0; | |
533 | goto again; | |
c24b5dfa | 534 | } |
c24b5dfa DC |
535 | } |
536 | ||
537 | /* | |
653c60b6 | 538 | * xfs_lock_two_inodes() can only be used to lock one type of lock at a time - |
7c2d238a DW |
539 | * the mmaplock or the ilock, but not more than one type at a time. If we lock |
540 | * more than one at a time, lockdep will report false positives saying we have | |
541 | * violated locking orders. The iolock must be double-locked separately since | |
542 | * we use i_rwsem for that. We now support taking one lock EXCL and the other | |
543 | * SHARED. | |
c24b5dfa DC |
544 | */ |
545 | void | |
546 | xfs_lock_two_inodes( | |
7c2d238a DW |
547 | struct xfs_inode *ip0, |
548 | uint ip0_mode, | |
549 | struct xfs_inode *ip1, | |
550 | uint ip1_mode) | |
c24b5dfa | 551 | { |
7c2d238a DW |
552 | struct xfs_inode *temp; |
553 | uint mode_temp; | |
c24b5dfa | 554 | int attempts = 0; |
efe2330f | 555 | struct xfs_log_item *lp; |
c24b5dfa | 556 | |
7c2d238a DW |
557 | ASSERT(hweight32(ip0_mode) == 1); |
558 | ASSERT(hweight32(ip1_mode) == 1); | |
559 | ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))); | |
560 | ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))); | |
561 | ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) || | |
562 | !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))); | |
563 | ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) || | |
564 | !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))); | |
565 | ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) || | |
566 | !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))); | |
567 | ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) || | |
568 | !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))); | |
653c60b6 | 569 | |
c24b5dfa DC |
570 | ASSERT(ip0->i_ino != ip1->i_ino); |
571 | ||
572 | if (ip0->i_ino > ip1->i_ino) { | |
573 | temp = ip0; | |
574 | ip0 = ip1; | |
575 | ip1 = temp; | |
7c2d238a DW |
576 | mode_temp = ip0_mode; |
577 | ip0_mode = ip1_mode; | |
578 | ip1_mode = mode_temp; | |
c24b5dfa DC |
579 | } |
580 | ||
581 | again: | |
7c2d238a | 582 | xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0)); |
c24b5dfa DC |
583 | |
584 | /* | |
585 | * If the first lock we have locked is in the AIL, we must TRY to get | |
586 | * the second lock. If we can't get it, we must release the first one | |
587 | * and try again. | |
588 | */ | |
b3b14aac | 589 | lp = &ip0->i_itemp->ili_item; |
22525c17 | 590 | if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) { |
7c2d238a DW |
591 | if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) { |
592 | xfs_iunlock(ip0, ip0_mode); | |
c24b5dfa DC |
593 | if ((++attempts % 5) == 0) |
594 | delay(1); /* Don't just spin the CPU */ | |
595 | goto again; | |
596 | } | |
597 | } else { | |
7c2d238a | 598 | xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1)); |
c24b5dfa DC |
599 | } |
600 | } | |
601 | ||
fa96acad DC |
602 | void |
603 | __xfs_iflock( | |
604 | struct xfs_inode *ip) | |
605 | { | |
606 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT); | |
607 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT); | |
608 | ||
609 | do { | |
21417136 | 610 | prepare_to_wait_exclusive(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); |
fa96acad DC |
611 | if (xfs_isiflocked(ip)) |
612 | io_schedule(); | |
613 | } while (!xfs_iflock_nowait(ip)); | |
614 | ||
21417136 | 615 | finish_wait(wq, &wait.wq_entry); |
fa96acad DC |
616 | } |
617 | ||
1da177e4 LT |
618 | STATIC uint |
619 | _xfs_dic2xflags( | |
c8ce540d | 620 | uint16_t di_flags, |
58f88ca2 DC |
621 | uint64_t di_flags2, |
622 | bool has_attr) | |
1da177e4 LT |
623 | { |
624 | uint flags = 0; | |
625 | ||
626 | if (di_flags & XFS_DIFLAG_ANY) { | |
627 | if (di_flags & XFS_DIFLAG_REALTIME) | |
e7b89481 | 628 | flags |= FS_XFLAG_REALTIME; |
1da177e4 | 629 | if (di_flags & XFS_DIFLAG_PREALLOC) |
e7b89481 | 630 | flags |= FS_XFLAG_PREALLOC; |
1da177e4 | 631 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
e7b89481 | 632 | flags |= FS_XFLAG_IMMUTABLE; |
1da177e4 | 633 | if (di_flags & XFS_DIFLAG_APPEND) |
e7b89481 | 634 | flags |= FS_XFLAG_APPEND; |
1da177e4 | 635 | if (di_flags & XFS_DIFLAG_SYNC) |
e7b89481 | 636 | flags |= FS_XFLAG_SYNC; |
1da177e4 | 637 | if (di_flags & XFS_DIFLAG_NOATIME) |
e7b89481 | 638 | flags |= FS_XFLAG_NOATIME; |
1da177e4 | 639 | if (di_flags & XFS_DIFLAG_NODUMP) |
e7b89481 | 640 | flags |= FS_XFLAG_NODUMP; |
1da177e4 | 641 | if (di_flags & XFS_DIFLAG_RTINHERIT) |
e7b89481 | 642 | flags |= FS_XFLAG_RTINHERIT; |
1da177e4 | 643 | if (di_flags & XFS_DIFLAG_PROJINHERIT) |
e7b89481 | 644 | flags |= FS_XFLAG_PROJINHERIT; |
1da177e4 | 645 | if (di_flags & XFS_DIFLAG_NOSYMLINKS) |
e7b89481 | 646 | flags |= FS_XFLAG_NOSYMLINKS; |
dd9f438e | 647 | if (di_flags & XFS_DIFLAG_EXTSIZE) |
e7b89481 | 648 | flags |= FS_XFLAG_EXTSIZE; |
dd9f438e | 649 | if (di_flags & XFS_DIFLAG_EXTSZINHERIT) |
e7b89481 | 650 | flags |= FS_XFLAG_EXTSZINHERIT; |
d3446eac | 651 | if (di_flags & XFS_DIFLAG_NODEFRAG) |
e7b89481 | 652 | flags |= FS_XFLAG_NODEFRAG; |
2a82b8be | 653 | if (di_flags & XFS_DIFLAG_FILESTREAM) |
e7b89481 | 654 | flags |= FS_XFLAG_FILESTREAM; |
1da177e4 LT |
655 | } |
656 | ||
58f88ca2 DC |
657 | if (di_flags2 & XFS_DIFLAG2_ANY) { |
658 | if (di_flags2 & XFS_DIFLAG2_DAX) | |
659 | flags |= FS_XFLAG_DAX; | |
f7ca3522 DW |
660 | if (di_flags2 & XFS_DIFLAG2_COWEXTSIZE) |
661 | flags |= FS_XFLAG_COWEXTSIZE; | |
58f88ca2 DC |
662 | } |
663 | ||
664 | if (has_attr) | |
665 | flags |= FS_XFLAG_HASATTR; | |
666 | ||
1da177e4 LT |
667 | return flags; |
668 | } | |
669 | ||
670 | uint | |
671 | xfs_ip2xflags( | |
58f88ca2 | 672 | struct xfs_inode *ip) |
1da177e4 | 673 | { |
58f88ca2 | 674 | struct xfs_icdinode *dic = &ip->i_d; |
1da177e4 | 675 | |
58f88ca2 | 676 | return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip)); |
1da177e4 LT |
677 | } |
678 | ||
c24b5dfa DC |
679 | /* |
680 | * Lookups up an inode from "name". If ci_name is not NULL, then a CI match | |
681 | * is allowed, otherwise it has to be an exact match. If a CI match is found, | |
682 | * ci_name->name will point to a the actual name (caller must free) or | |
683 | * will be set to NULL if an exact match is found. | |
684 | */ | |
685 | int | |
686 | xfs_lookup( | |
687 | xfs_inode_t *dp, | |
688 | struct xfs_name *name, | |
689 | xfs_inode_t **ipp, | |
690 | struct xfs_name *ci_name) | |
691 | { | |
692 | xfs_ino_t inum; | |
693 | int error; | |
c24b5dfa DC |
694 | |
695 | trace_xfs_lookup(dp, name); | |
696 | ||
697 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) | |
2451337d | 698 | return -EIO; |
c24b5dfa | 699 | |
c24b5dfa | 700 | error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); |
c24b5dfa | 701 | if (error) |
dbad7c99 | 702 | goto out_unlock; |
c24b5dfa DC |
703 | |
704 | error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp); | |
705 | if (error) | |
706 | goto out_free_name; | |
707 | ||
708 | return 0; | |
709 | ||
710 | out_free_name: | |
711 | if (ci_name) | |
712 | kmem_free(ci_name->name); | |
dbad7c99 | 713 | out_unlock: |
c24b5dfa DC |
714 | *ipp = NULL; |
715 | return error; | |
716 | } | |
717 | ||
1da177e4 LT |
718 | /* |
719 | * Allocate an inode on disk and return a copy of its in-core version. | |
720 | * The in-core inode is locked exclusively. Set mode, nlink, and rdev | |
721 | * appropriately within the inode. The uid and gid for the inode are | |
722 | * set according to the contents of the given cred structure. | |
723 | * | |
724 | * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc() | |
cd856db6 CM |
725 | * has a free inode available, call xfs_iget() to obtain the in-core |
726 | * version of the allocated inode. Finally, fill in the inode and | |
727 | * log its initial contents. In this case, ialloc_context would be | |
728 | * set to NULL. | |
1da177e4 | 729 | * |
cd856db6 CM |
730 | * If xfs_dialloc() does not have an available inode, it will replenish |
731 | * its supply by doing an allocation. Since we can only do one | |
732 | * allocation within a transaction without deadlocks, we must commit | |
733 | * the current transaction before returning the inode itself. | |
734 | * In this case, therefore, we will set ialloc_context and return. | |
1da177e4 LT |
735 | * The caller should then commit the current transaction, start a new |
736 | * transaction, and call xfs_ialloc() again to actually get the inode. | |
737 | * | |
738 | * To ensure that some other process does not grab the inode that | |
739 | * was allocated during the first call to xfs_ialloc(), this routine | |
740 | * also returns the [locked] bp pointing to the head of the freelist | |
741 | * as ialloc_context. The caller should hold this buffer across | |
742 | * the commit and pass it back into this routine on the second call. | |
b11f94d5 DC |
743 | * |
744 | * If we are allocating quota inodes, we do not have a parent inode | |
745 | * to attach to or associate with (i.e. pip == NULL) because they | |
746 | * are not linked into the directory structure - they are attached | |
747 | * directly to the superblock - and so have no parent. | |
1da177e4 | 748 | */ |
0d5a75e9 | 749 | static int |
1da177e4 LT |
750 | xfs_ialloc( |
751 | xfs_trans_t *tp, | |
752 | xfs_inode_t *pip, | |
576b1d67 | 753 | umode_t mode, |
31b084ae | 754 | xfs_nlink_t nlink, |
66f36464 | 755 | dev_t rdev, |
6743099c | 756 | prid_t prid, |
1da177e4 | 757 | xfs_buf_t **ialloc_context, |
1da177e4 LT |
758 | xfs_inode_t **ipp) |
759 | { | |
93848a99 | 760 | struct xfs_mount *mp = tp->t_mountp; |
1da177e4 LT |
761 | xfs_ino_t ino; |
762 | xfs_inode_t *ip; | |
1da177e4 LT |
763 | uint flags; |
764 | int error; | |
95582b00 | 765 | struct timespec64 tv; |
3987848c | 766 | struct inode *inode; |
1da177e4 LT |
767 | |
768 | /* | |
769 | * Call the space management code to pick | |
770 | * the on-disk inode to be allocated. | |
771 | */ | |
f59cf5c2 | 772 | error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, |
08358906 | 773 | ialloc_context, &ino); |
bf904248 | 774 | if (error) |
1da177e4 | 775 | return error; |
08358906 | 776 | if (*ialloc_context || ino == NULLFSINO) { |
1da177e4 LT |
777 | *ipp = NULL; |
778 | return 0; | |
779 | } | |
780 | ASSERT(*ialloc_context == NULL); | |
781 | ||
8b26984d DC |
782 | /* |
783 | * Protect against obviously corrupt allocation btree records. Later | |
784 | * xfs_iget checks will catch re-allocation of other active in-memory | |
785 | * and on-disk inodes. If we don't catch reallocating the parent inode | |
786 | * here we will deadlock in xfs_iget() so we have to do these checks | |
787 | * first. | |
788 | */ | |
789 | if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) { | |
790 | xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino); | |
791 | return -EFSCORRUPTED; | |
792 | } | |
793 | ||
1da177e4 LT |
794 | /* |
795 | * Get the in-core inode with the lock held exclusively. | |
796 | * This is because we're setting fields here we need | |
797 | * to prevent others from looking at until we're done. | |
798 | */ | |
93848a99 | 799 | error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, |
ec3ba85f | 800 | XFS_ILOCK_EXCL, &ip); |
bf904248 | 801 | if (error) |
1da177e4 | 802 | return error; |
1da177e4 | 803 | ASSERT(ip != NULL); |
3987848c | 804 | inode = VFS_I(ip); |
c19b3b05 | 805 | inode->i_mode = mode; |
54d7b5c1 | 806 | set_nlink(inode, nlink); |
3d8f2821 | 807 | inode->i_uid = current_fsuid(); |
66f36464 | 808 | inode->i_rdev = rdev; |
de7a866f | 809 | ip->i_d.di_projid = prid; |
1da177e4 | 810 | |
bd186aa9 | 811 | if (pip && XFS_INHERIT_GID(pip)) { |
3d8f2821 | 812 | inode->i_gid = VFS_I(pip)->i_gid; |
c19b3b05 DC |
813 | if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode)) |
814 | inode->i_mode |= S_ISGID; | |
3d8f2821 CH |
815 | } else { |
816 | inode->i_gid = current_fsgid(); | |
1da177e4 LT |
817 | } |
818 | ||
819 | /* | |
820 | * If the group ID of the new file does not match the effective group | |
821 | * ID or one of the supplementary group IDs, the S_ISGID bit is cleared | |
822 | * (and only if the irix_sgid_inherit compatibility variable is set). | |
823 | */ | |
54295159 CH |
824 | if (irix_sgid_inherit && |
825 | (inode->i_mode & S_ISGID) && !in_group_p(inode->i_gid)) | |
c19b3b05 | 826 | inode->i_mode &= ~S_ISGID; |
1da177e4 LT |
827 | |
828 | ip->i_d.di_size = 0; | |
daf83964 | 829 | ip->i_df.if_nextents = 0; |
1da177e4 | 830 | ASSERT(ip->i_d.di_nblocks == 0); |
dff35fd4 | 831 | |
c2050a45 | 832 | tv = current_time(inode); |
3987848c DC |
833 | inode->i_mtime = tv; |
834 | inode->i_atime = tv; | |
835 | inode->i_ctime = tv; | |
dff35fd4 | 836 | |
1da177e4 LT |
837 | ip->i_d.di_extsize = 0; |
838 | ip->i_d.di_dmevmask = 0; | |
839 | ip->i_d.di_dmstate = 0; | |
840 | ip->i_d.di_flags = 0; | |
93848a99 | 841 | |
6471e9c5 | 842 | if (xfs_sb_version_has_v3inode(&mp->m_sb)) { |
f0e28280 | 843 | inode_set_iversion(inode, 1); |
93848a99 | 844 | ip->i_d.di_flags2 = 0; |
f7ca3522 | 845 | ip->i_d.di_cowextsize = 0; |
8d2d878d | 846 | ip->i_d.di_crtime = tv; |
93848a99 CH |
847 | } |
848 | ||
1da177e4 LT |
849 | flags = XFS_ILOG_CORE; |
850 | switch (mode & S_IFMT) { | |
851 | case S_IFIFO: | |
852 | case S_IFCHR: | |
853 | case S_IFBLK: | |
854 | case S_IFSOCK: | |
f7e67b20 | 855 | ip->i_df.if_format = XFS_DINODE_FMT_DEV; |
1da177e4 LT |
856 | ip->i_df.if_flags = 0; |
857 | flags |= XFS_ILOG_DEV; | |
858 | break; | |
859 | case S_IFREG: | |
860 | case S_IFDIR: | |
b11f94d5 | 861 | if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { |
58f88ca2 | 862 | uint di_flags = 0; |
365ca83d | 863 | |
abbede1b | 864 | if (S_ISDIR(mode)) { |
365ca83d NS |
865 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
866 | di_flags |= XFS_DIFLAG_RTINHERIT; | |
dd9f438e NS |
867 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
868 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; | |
869 | ip->i_d.di_extsize = pip->i_d.di_extsize; | |
870 | } | |
9336e3a7 DC |
871 | if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
872 | di_flags |= XFS_DIFLAG_PROJINHERIT; | |
abbede1b | 873 | } else if (S_ISREG(mode)) { |
613d7043 | 874 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
365ca83d | 875 | di_flags |= XFS_DIFLAG_REALTIME; |
dd9f438e NS |
876 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
877 | di_flags |= XFS_DIFLAG_EXTSIZE; | |
878 | ip->i_d.di_extsize = pip->i_d.di_extsize; | |
879 | } | |
1da177e4 LT |
880 | } |
881 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) && | |
882 | xfs_inherit_noatime) | |
365ca83d | 883 | di_flags |= XFS_DIFLAG_NOATIME; |
1da177e4 LT |
884 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) && |
885 | xfs_inherit_nodump) | |
365ca83d | 886 | di_flags |= XFS_DIFLAG_NODUMP; |
1da177e4 LT |
887 | if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) && |
888 | xfs_inherit_sync) | |
365ca83d | 889 | di_flags |= XFS_DIFLAG_SYNC; |
1da177e4 LT |
890 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) && |
891 | xfs_inherit_nosymlinks) | |
365ca83d | 892 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
d3446eac BN |
893 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) && |
894 | xfs_inherit_nodefrag) | |
895 | di_flags |= XFS_DIFLAG_NODEFRAG; | |
2a82b8be DC |
896 | if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM) |
897 | di_flags |= XFS_DIFLAG_FILESTREAM; | |
58f88ca2 | 898 | |
365ca83d | 899 | ip->i_d.di_flags |= di_flags; |
1da177e4 | 900 | } |
b3d1d375 | 901 | if (pip && (pip->i_d.di_flags2 & XFS_DIFLAG2_ANY)) { |
f7ca3522 | 902 | if (pip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) { |
b3d1d375 | 903 | ip->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE; |
f7ca3522 DW |
904 | ip->i_d.di_cowextsize = pip->i_d.di_cowextsize; |
905 | } | |
56bdf855 | 906 | if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX) |
b3d1d375 | 907 | ip->i_d.di_flags2 |= XFS_DIFLAG2_DAX; |
f7ca3522 | 908 | } |
1da177e4 LT |
909 | /* FALLTHROUGH */ |
910 | case S_IFLNK: | |
f7e67b20 | 911 | ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS; |
1da177e4 | 912 | ip->i_df.if_flags = XFS_IFEXTENTS; |
fcacbc3f | 913 | ip->i_df.if_bytes = 0; |
6bdcf26a | 914 | ip->i_df.if_u1.if_root = NULL; |
1da177e4 LT |
915 | break; |
916 | default: | |
917 | ASSERT(0); | |
918 | } | |
1da177e4 LT |
919 | |
920 | /* | |
921 | * Log the new values stuffed into the inode. | |
922 | */ | |
ddc3415a | 923 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
1da177e4 LT |
924 | xfs_trans_log_inode(tp, ip, flags); |
925 | ||
58c90473 | 926 | /* now that we have an i_mode we can setup the inode structure */ |
41be8bed | 927 | xfs_setup_inode(ip); |
1da177e4 LT |
928 | |
929 | *ipp = ip; | |
930 | return 0; | |
931 | } | |
932 | ||
e546cb79 DC |
933 | /* |
934 | * Allocates a new inode from disk and return a pointer to the | |
935 | * incore copy. This routine will internally commit the current | |
936 | * transaction and allocate a new one if the Space Manager needed | |
937 | * to do an allocation to replenish the inode free-list. | |
938 | * | |
939 | * This routine is designed to be called from xfs_create and | |
940 | * xfs_create_dir. | |
941 | * | |
942 | */ | |
943 | int | |
944 | xfs_dir_ialloc( | |
945 | xfs_trans_t **tpp, /* input: current transaction; | |
946 | output: may be a new transaction. */ | |
947 | xfs_inode_t *dp, /* directory within whose allocate | |
948 | the inode. */ | |
949 | umode_t mode, | |
950 | xfs_nlink_t nlink, | |
66f36464 | 951 | dev_t rdev, |
e546cb79 | 952 | prid_t prid, /* project id */ |
c959025e | 953 | xfs_inode_t **ipp) /* pointer to inode; it will be |
e546cb79 | 954 | locked. */ |
e546cb79 DC |
955 | { |
956 | xfs_trans_t *tp; | |
e546cb79 DC |
957 | xfs_inode_t *ip; |
958 | xfs_buf_t *ialloc_context = NULL; | |
959 | int code; | |
e546cb79 DC |
960 | void *dqinfo; |
961 | uint tflags; | |
962 | ||
963 | tp = *tpp; | |
964 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); | |
965 | ||
966 | /* | |
967 | * xfs_ialloc will return a pointer to an incore inode if | |
968 | * the Space Manager has an available inode on the free | |
969 | * list. Otherwise, it will do an allocation and replenish | |
970 | * the freelist. Since we can only do one allocation per | |
971 | * transaction without deadlocks, we will need to commit the | |
972 | * current transaction and start a new one. We will then | |
973 | * need to call xfs_ialloc again to get the inode. | |
974 | * | |
975 | * If xfs_ialloc did an allocation to replenish the freelist, | |
976 | * it returns the bp containing the head of the freelist as | |
977 | * ialloc_context. We will hold a lock on it across the | |
978 | * transaction commit so that no other process can steal | |
979 | * the inode(s) that we've just allocated. | |
980 | */ | |
f59cf5c2 CH |
981 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context, |
982 | &ip); | |
e546cb79 DC |
983 | |
984 | /* | |
985 | * Return an error if we were unable to allocate a new inode. | |
986 | * This should only happen if we run out of space on disk or | |
987 | * encounter a disk error. | |
988 | */ | |
989 | if (code) { | |
990 | *ipp = NULL; | |
991 | return code; | |
992 | } | |
993 | if (!ialloc_context && !ip) { | |
994 | *ipp = NULL; | |
2451337d | 995 | return -ENOSPC; |
e546cb79 DC |
996 | } |
997 | ||
998 | /* | |
999 | * If the AGI buffer is non-NULL, then we were unable to get an | |
1000 | * inode in one operation. We need to commit the current | |
1001 | * transaction and call xfs_ialloc() again. It is guaranteed | |
1002 | * to succeed the second time. | |
1003 | */ | |
1004 | if (ialloc_context) { | |
1005 | /* | |
1006 | * Normally, xfs_trans_commit releases all the locks. | |
1007 | * We call bhold to hang on to the ialloc_context across | |
1008 | * the commit. Holding this buffer prevents any other | |
1009 | * processes from doing any allocations in this | |
1010 | * allocation group. | |
1011 | */ | |
1012 | xfs_trans_bhold(tp, ialloc_context); | |
e546cb79 DC |
1013 | |
1014 | /* | |
1015 | * We want the quota changes to be associated with the next | |
1016 | * transaction, NOT this one. So, detach the dqinfo from this | |
1017 | * and attach it to the next transaction. | |
1018 | */ | |
1019 | dqinfo = NULL; | |
1020 | tflags = 0; | |
1021 | if (tp->t_dqinfo) { | |
1022 | dqinfo = (void *)tp->t_dqinfo; | |
1023 | tp->t_dqinfo = NULL; | |
1024 | tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY; | |
1025 | tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY); | |
1026 | } | |
1027 | ||
411350df | 1028 | code = xfs_trans_roll(&tp); |
3d3c8b52 | 1029 | |
e546cb79 DC |
1030 | /* |
1031 | * Re-attach the quota info that we detached from prev trx. | |
1032 | */ | |
1033 | if (dqinfo) { | |
1034 | tp->t_dqinfo = dqinfo; | |
1035 | tp->t_flags |= tflags; | |
1036 | } | |
1037 | ||
1038 | if (code) { | |
1039 | xfs_buf_relse(ialloc_context); | |
2e6db6c4 | 1040 | *tpp = tp; |
e546cb79 DC |
1041 | *ipp = NULL; |
1042 | return code; | |
1043 | } | |
1044 | xfs_trans_bjoin(tp, ialloc_context); | |
1045 | ||
1046 | /* | |
1047 | * Call ialloc again. Since we've locked out all | |
1048 | * other allocations in this allocation group, | |
1049 | * this call should always succeed. | |
1050 | */ | |
1051 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, | |
f59cf5c2 | 1052 | &ialloc_context, &ip); |
e546cb79 DC |
1053 | |
1054 | /* | |
1055 | * If we get an error at this point, return to the caller | |
1056 | * so that the current transaction can be aborted. | |
1057 | */ | |
1058 | if (code) { | |
1059 | *tpp = tp; | |
1060 | *ipp = NULL; | |
1061 | return code; | |
1062 | } | |
1063 | ASSERT(!ialloc_context && ip); | |
1064 | ||
e546cb79 DC |
1065 | } |
1066 | ||
1067 | *ipp = ip; | |
1068 | *tpp = tp; | |
1069 | ||
1070 | return 0; | |
1071 | } | |
1072 | ||
1073 | /* | |
54d7b5c1 DC |
1074 | * Decrement the link count on an inode & log the change. If this causes the |
1075 | * link count to go to zero, move the inode to AGI unlinked list so that it can | |
1076 | * be freed when the last active reference goes away via xfs_inactive(). | |
e546cb79 | 1077 | */ |
0d5a75e9 | 1078 | static int /* error */ |
e546cb79 DC |
1079 | xfs_droplink( |
1080 | xfs_trans_t *tp, | |
1081 | xfs_inode_t *ip) | |
1082 | { | |
e546cb79 DC |
1083 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); |
1084 | ||
e546cb79 DC |
1085 | drop_nlink(VFS_I(ip)); |
1086 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1087 | ||
54d7b5c1 DC |
1088 | if (VFS_I(ip)->i_nlink) |
1089 | return 0; | |
1090 | ||
1091 | return xfs_iunlink(tp, ip); | |
e546cb79 DC |
1092 | } |
1093 | ||
e546cb79 DC |
1094 | /* |
1095 | * Increment the link count on an inode & log the change. | |
1096 | */ | |
91083269 | 1097 | static void |
e546cb79 DC |
1098 | xfs_bumplink( |
1099 | xfs_trans_t *tp, | |
1100 | xfs_inode_t *ip) | |
1101 | { | |
1102 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); | |
1103 | ||
e546cb79 | 1104 | inc_nlink(VFS_I(ip)); |
e546cb79 | 1105 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
e546cb79 DC |
1106 | } |
1107 | ||
c24b5dfa DC |
1108 | int |
1109 | xfs_create( | |
1110 | xfs_inode_t *dp, | |
1111 | struct xfs_name *name, | |
1112 | umode_t mode, | |
66f36464 | 1113 | dev_t rdev, |
c24b5dfa DC |
1114 | xfs_inode_t **ipp) |
1115 | { | |
1116 | int is_dir = S_ISDIR(mode); | |
1117 | struct xfs_mount *mp = dp->i_mount; | |
1118 | struct xfs_inode *ip = NULL; | |
1119 | struct xfs_trans *tp = NULL; | |
1120 | int error; | |
c24b5dfa | 1121 | bool unlock_dp_on_error = false; |
c24b5dfa DC |
1122 | prid_t prid; |
1123 | struct xfs_dquot *udqp = NULL; | |
1124 | struct xfs_dquot *gdqp = NULL; | |
1125 | struct xfs_dquot *pdqp = NULL; | |
062647a8 | 1126 | struct xfs_trans_res *tres; |
c24b5dfa | 1127 | uint resblks; |
c24b5dfa DC |
1128 | |
1129 | trace_xfs_create(dp, name); | |
1130 | ||
1131 | if (XFS_FORCED_SHUTDOWN(mp)) | |
2451337d | 1132 | return -EIO; |
c24b5dfa | 1133 | |
163467d3 | 1134 | prid = xfs_get_initial_prid(dp); |
c24b5dfa DC |
1135 | |
1136 | /* | |
1137 | * Make sure that we have allocated dquot(s) on disk. | |
1138 | */ | |
54295159 | 1139 | error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, |
c24b5dfa DC |
1140 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, |
1141 | &udqp, &gdqp, &pdqp); | |
1142 | if (error) | |
1143 | return error; | |
1144 | ||
1145 | if (is_dir) { | |
c24b5dfa | 1146 | resblks = XFS_MKDIR_SPACE_RES(mp, name->len); |
062647a8 | 1147 | tres = &M_RES(mp)->tr_mkdir; |
c24b5dfa DC |
1148 | } else { |
1149 | resblks = XFS_CREATE_SPACE_RES(mp, name->len); | |
062647a8 | 1150 | tres = &M_RES(mp)->tr_create; |
c24b5dfa DC |
1151 | } |
1152 | ||
c24b5dfa DC |
1153 | /* |
1154 | * Initially assume that the file does not exist and | |
1155 | * reserve the resources for that case. If that is not | |
1156 | * the case we'll drop the one we have and get a more | |
1157 | * appropriate transaction later. | |
1158 | */ | |
253f4911 | 1159 | error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp); |
2451337d | 1160 | if (error == -ENOSPC) { |
c24b5dfa DC |
1161 | /* flush outstanding delalloc blocks and retry */ |
1162 | xfs_flush_inodes(mp); | |
253f4911 | 1163 | error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp); |
c24b5dfa | 1164 | } |
4906e215 | 1165 | if (error) |
253f4911 | 1166 | goto out_release_inode; |
c24b5dfa | 1167 | |
65523218 | 1168 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
c24b5dfa DC |
1169 | unlock_dp_on_error = true; |
1170 | ||
c24b5dfa DC |
1171 | /* |
1172 | * Reserve disk quota and the inode. | |
1173 | */ | |
1174 | error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, | |
1175 | pdqp, resblks, 1, 0); | |
1176 | if (error) | |
1177 | goto out_trans_cancel; | |
1178 | ||
c24b5dfa DC |
1179 | /* |
1180 | * A newly created regular or special file just has one directory | |
1181 | * entry pointing to them, but a directory also the "." entry | |
1182 | * pointing to itself. | |
1183 | */ | |
c959025e | 1184 | error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip); |
d6077aa3 | 1185 | if (error) |
4906e215 | 1186 | goto out_trans_cancel; |
c24b5dfa DC |
1187 | |
1188 | /* | |
1189 | * Now we join the directory inode to the transaction. We do not do it | |
1190 | * earlier because xfs_dir_ialloc might commit the previous transaction | |
1191 | * (and release all the locks). An error from here on will result in | |
1192 | * the transaction cancel unlocking dp so don't do it explicitly in the | |
1193 | * error path. | |
1194 | */ | |
65523218 | 1195 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
c24b5dfa DC |
1196 | unlock_dp_on_error = false; |
1197 | ||
381eee69 | 1198 | error = xfs_dir_createname(tp, dp, name, ip->i_ino, |
63337b63 | 1199 | resblks - XFS_IALLOC_SPACE_RES(mp)); |
c24b5dfa | 1200 | if (error) { |
2451337d | 1201 | ASSERT(error != -ENOSPC); |
4906e215 | 1202 | goto out_trans_cancel; |
c24b5dfa DC |
1203 | } |
1204 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
1205 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
1206 | ||
1207 | if (is_dir) { | |
1208 | error = xfs_dir_init(tp, ip, dp); | |
1209 | if (error) | |
c8eac49e | 1210 | goto out_trans_cancel; |
c24b5dfa | 1211 | |
91083269 | 1212 | xfs_bumplink(tp, dp); |
c24b5dfa DC |
1213 | } |
1214 | ||
1215 | /* | |
1216 | * If this is a synchronous mount, make sure that the | |
1217 | * create transaction goes to disk before returning to | |
1218 | * the user. | |
1219 | */ | |
1220 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) | |
1221 | xfs_trans_set_sync(tp); | |
1222 | ||
1223 | /* | |
1224 | * Attach the dquot(s) to the inodes and modify them incore. | |
1225 | * These ids of the inode couldn't have changed since the new | |
1226 | * inode has been locked ever since it was created. | |
1227 | */ | |
1228 | xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); | |
1229 | ||
70393313 | 1230 | error = xfs_trans_commit(tp); |
c24b5dfa DC |
1231 | if (error) |
1232 | goto out_release_inode; | |
1233 | ||
1234 | xfs_qm_dqrele(udqp); | |
1235 | xfs_qm_dqrele(gdqp); | |
1236 | xfs_qm_dqrele(pdqp); | |
1237 | ||
1238 | *ipp = ip; | |
1239 | return 0; | |
1240 | ||
c24b5dfa | 1241 | out_trans_cancel: |
4906e215 | 1242 | xfs_trans_cancel(tp); |
c24b5dfa DC |
1243 | out_release_inode: |
1244 | /* | |
58c90473 DC |
1245 | * Wait until after the current transaction is aborted to finish the |
1246 | * setup of the inode and release the inode. This prevents recursive | |
1247 | * transactions and deadlocks from xfs_inactive. | |
c24b5dfa | 1248 | */ |
58c90473 DC |
1249 | if (ip) { |
1250 | xfs_finish_inode_setup(ip); | |
44a8736b | 1251 | xfs_irele(ip); |
58c90473 | 1252 | } |
c24b5dfa DC |
1253 | |
1254 | xfs_qm_dqrele(udqp); | |
1255 | xfs_qm_dqrele(gdqp); | |
1256 | xfs_qm_dqrele(pdqp); | |
1257 | ||
1258 | if (unlock_dp_on_error) | |
65523218 | 1259 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
c24b5dfa DC |
1260 | return error; |
1261 | } | |
1262 | ||
99b6436b ZYW |
1263 | int |
1264 | xfs_create_tmpfile( | |
1265 | struct xfs_inode *dp, | |
330033d6 BF |
1266 | umode_t mode, |
1267 | struct xfs_inode **ipp) | |
99b6436b ZYW |
1268 | { |
1269 | struct xfs_mount *mp = dp->i_mount; | |
1270 | struct xfs_inode *ip = NULL; | |
1271 | struct xfs_trans *tp = NULL; | |
1272 | int error; | |
99b6436b ZYW |
1273 | prid_t prid; |
1274 | struct xfs_dquot *udqp = NULL; | |
1275 | struct xfs_dquot *gdqp = NULL; | |
1276 | struct xfs_dquot *pdqp = NULL; | |
1277 | struct xfs_trans_res *tres; | |
1278 | uint resblks; | |
1279 | ||
1280 | if (XFS_FORCED_SHUTDOWN(mp)) | |
2451337d | 1281 | return -EIO; |
99b6436b ZYW |
1282 | |
1283 | prid = xfs_get_initial_prid(dp); | |
1284 | ||
1285 | /* | |
1286 | * Make sure that we have allocated dquot(s) on disk. | |
1287 | */ | |
54295159 | 1288 | error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, |
99b6436b ZYW |
1289 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, |
1290 | &udqp, &gdqp, &pdqp); | |
1291 | if (error) | |
1292 | return error; | |
1293 | ||
1294 | resblks = XFS_IALLOC_SPACE_RES(mp); | |
99b6436b | 1295 | tres = &M_RES(mp)->tr_create_tmpfile; |
253f4911 CH |
1296 | |
1297 | error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp); | |
4906e215 | 1298 | if (error) |
253f4911 | 1299 | goto out_release_inode; |
99b6436b ZYW |
1300 | |
1301 | error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, | |
1302 | pdqp, resblks, 1, 0); | |
1303 | if (error) | |
1304 | goto out_trans_cancel; | |
1305 | ||
c4a6bf7f | 1306 | error = xfs_dir_ialloc(&tp, dp, mode, 0, 0, prid, &ip); |
d6077aa3 | 1307 | if (error) |
4906e215 | 1308 | goto out_trans_cancel; |
99b6436b ZYW |
1309 | |
1310 | if (mp->m_flags & XFS_MOUNT_WSYNC) | |
1311 | xfs_trans_set_sync(tp); | |
1312 | ||
1313 | /* | |
1314 | * Attach the dquot(s) to the inodes and modify them incore. | |
1315 | * These ids of the inode couldn't have changed since the new | |
1316 | * inode has been locked ever since it was created. | |
1317 | */ | |
1318 | xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); | |
1319 | ||
99b6436b ZYW |
1320 | error = xfs_iunlink(tp, ip); |
1321 | if (error) | |
4906e215 | 1322 | goto out_trans_cancel; |
99b6436b | 1323 | |
70393313 | 1324 | error = xfs_trans_commit(tp); |
99b6436b ZYW |
1325 | if (error) |
1326 | goto out_release_inode; | |
1327 | ||
1328 | xfs_qm_dqrele(udqp); | |
1329 | xfs_qm_dqrele(gdqp); | |
1330 | xfs_qm_dqrele(pdqp); | |
1331 | ||
330033d6 | 1332 | *ipp = ip; |
99b6436b ZYW |
1333 | return 0; |
1334 | ||
99b6436b | 1335 | out_trans_cancel: |
4906e215 | 1336 | xfs_trans_cancel(tp); |
99b6436b ZYW |
1337 | out_release_inode: |
1338 | /* | |
58c90473 DC |
1339 | * Wait until after the current transaction is aborted to finish the |
1340 | * setup of the inode and release the inode. This prevents recursive | |
1341 | * transactions and deadlocks from xfs_inactive. | |
99b6436b | 1342 | */ |
58c90473 DC |
1343 | if (ip) { |
1344 | xfs_finish_inode_setup(ip); | |
44a8736b | 1345 | xfs_irele(ip); |
58c90473 | 1346 | } |
99b6436b ZYW |
1347 | |
1348 | xfs_qm_dqrele(udqp); | |
1349 | xfs_qm_dqrele(gdqp); | |
1350 | xfs_qm_dqrele(pdqp); | |
1351 | ||
1352 | return error; | |
1353 | } | |
1354 | ||
c24b5dfa DC |
1355 | int |
1356 | xfs_link( | |
1357 | xfs_inode_t *tdp, | |
1358 | xfs_inode_t *sip, | |
1359 | struct xfs_name *target_name) | |
1360 | { | |
1361 | xfs_mount_t *mp = tdp->i_mount; | |
1362 | xfs_trans_t *tp; | |
1363 | int error; | |
c24b5dfa DC |
1364 | int resblks; |
1365 | ||
1366 | trace_xfs_link(tdp, target_name); | |
1367 | ||
c19b3b05 | 1368 | ASSERT(!S_ISDIR(VFS_I(sip)->i_mode)); |
c24b5dfa DC |
1369 | |
1370 | if (XFS_FORCED_SHUTDOWN(mp)) | |
2451337d | 1371 | return -EIO; |
c24b5dfa | 1372 | |
c14cfcca | 1373 | error = xfs_qm_dqattach(sip); |
c24b5dfa DC |
1374 | if (error) |
1375 | goto std_return; | |
1376 | ||
c14cfcca | 1377 | error = xfs_qm_dqattach(tdp); |
c24b5dfa DC |
1378 | if (error) |
1379 | goto std_return; | |
1380 | ||
c24b5dfa | 1381 | resblks = XFS_LINK_SPACE_RES(mp, target_name->len); |
253f4911 | 1382 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp); |
2451337d | 1383 | if (error == -ENOSPC) { |
c24b5dfa | 1384 | resblks = 0; |
253f4911 | 1385 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp); |
c24b5dfa | 1386 | } |
4906e215 | 1387 | if (error) |
253f4911 | 1388 | goto std_return; |
c24b5dfa | 1389 | |
7c2d238a | 1390 | xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL); |
c24b5dfa DC |
1391 | |
1392 | xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL); | |
65523218 | 1393 | xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL); |
c24b5dfa DC |
1394 | |
1395 | /* | |
1396 | * If we are using project inheritance, we only allow hard link | |
1397 | * creation in our tree when the project IDs are the same; else | |
1398 | * the tree quota mechanism could be circumvented. | |
1399 | */ | |
1400 | if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && | |
de7a866f | 1401 | tdp->i_d.di_projid != sip->i_d.di_projid)) { |
2451337d | 1402 | error = -EXDEV; |
c24b5dfa DC |
1403 | goto error_return; |
1404 | } | |
1405 | ||
94f3cad5 ES |
1406 | if (!resblks) { |
1407 | error = xfs_dir_canenter(tp, tdp, target_name); | |
1408 | if (error) | |
1409 | goto error_return; | |
1410 | } | |
c24b5dfa | 1411 | |
54d7b5c1 DC |
1412 | /* |
1413 | * Handle initial link state of O_TMPFILE inode | |
1414 | */ | |
1415 | if (VFS_I(sip)->i_nlink == 0) { | |
ab297431 ZYW |
1416 | error = xfs_iunlink_remove(tp, sip); |
1417 | if (error) | |
4906e215 | 1418 | goto error_return; |
ab297431 ZYW |
1419 | } |
1420 | ||
c24b5dfa | 1421 | error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, |
381eee69 | 1422 | resblks); |
c24b5dfa | 1423 | if (error) |
4906e215 | 1424 | goto error_return; |
c24b5dfa DC |
1425 | xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
1426 | xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); | |
1427 | ||
91083269 | 1428 | xfs_bumplink(tp, sip); |
c24b5dfa DC |
1429 | |
1430 | /* | |
1431 | * If this is a synchronous mount, make sure that the | |
1432 | * link transaction goes to disk before returning to | |
1433 | * the user. | |
1434 | */ | |
f6106efa | 1435 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) |
c24b5dfa | 1436 | xfs_trans_set_sync(tp); |
c24b5dfa | 1437 | |
70393313 | 1438 | return xfs_trans_commit(tp); |
c24b5dfa | 1439 | |
c24b5dfa | 1440 | error_return: |
4906e215 | 1441 | xfs_trans_cancel(tp); |
c24b5dfa DC |
1442 | std_return: |
1443 | return error; | |
1444 | } | |
1445 | ||
363e59ba DW |
1446 | /* Clear the reflink flag and the cowblocks tag if possible. */ |
1447 | static void | |
1448 | xfs_itruncate_clear_reflink_flags( | |
1449 | struct xfs_inode *ip) | |
1450 | { | |
1451 | struct xfs_ifork *dfork; | |
1452 | struct xfs_ifork *cfork; | |
1453 | ||
1454 | if (!xfs_is_reflink_inode(ip)) | |
1455 | return; | |
1456 | dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | |
1457 | cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK); | |
1458 | if (dfork->if_bytes == 0 && cfork->if_bytes == 0) | |
1459 | ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK; | |
1460 | if (cfork->if_bytes == 0) | |
1461 | xfs_inode_clear_cowblocks_tag(ip); | |
1462 | } | |
1463 | ||
1da177e4 | 1464 | /* |
8f04c47a CH |
1465 | * Free up the underlying blocks past new_size. The new size must be smaller |
1466 | * than the current size. This routine can be used both for the attribute and | |
1467 | * data fork, and does not modify the inode size, which is left to the caller. | |
1da177e4 | 1468 | * |
f6485057 DC |
1469 | * The transaction passed to this routine must have made a permanent log |
1470 | * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the | |
1471 | * given transaction and start new ones, so make sure everything involved in | |
1472 | * the transaction is tidy before calling here. Some transaction will be | |
1473 | * returned to the caller to be committed. The incoming transaction must | |
1474 | * already include the inode, and both inode locks must be held exclusively. | |
1475 | * The inode must also be "held" within the transaction. On return the inode | |
1476 | * will be "held" within the returned transaction. This routine does NOT | |
1477 | * require any disk space to be reserved for it within the transaction. | |
1da177e4 | 1478 | * |
f6485057 DC |
1479 | * If we get an error, we must return with the inode locked and linked into the |
1480 | * current transaction. This keeps things simple for the higher level code, | |
1481 | * because it always knows that the inode is locked and held in the transaction | |
1482 | * that returns to it whether errors occur or not. We don't mark the inode | |
1483 | * dirty on error so that transactions can be easily aborted if possible. | |
1da177e4 LT |
1484 | */ |
1485 | int | |
4e529339 | 1486 | xfs_itruncate_extents_flags( |
8f04c47a CH |
1487 | struct xfs_trans **tpp, |
1488 | struct xfs_inode *ip, | |
1489 | int whichfork, | |
13b86fc3 | 1490 | xfs_fsize_t new_size, |
4e529339 | 1491 | int flags) |
1da177e4 | 1492 | { |
8f04c47a CH |
1493 | struct xfs_mount *mp = ip->i_mount; |
1494 | struct xfs_trans *tp = *tpp; | |
8f04c47a | 1495 | xfs_fileoff_t first_unmap_block; |
8f04c47a | 1496 | xfs_filblks_t unmap_len; |
8f04c47a | 1497 | int error = 0; |
1da177e4 | 1498 | |
0b56185b CH |
1499 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1500 | ASSERT(!atomic_read(&VFS_I(ip)->i_count) || | |
1501 | xfs_isilocked(ip, XFS_IOLOCK_EXCL)); | |
ce7ae151 | 1502 | ASSERT(new_size <= XFS_ISIZE(ip)); |
8f04c47a | 1503 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
1da177e4 | 1504 | ASSERT(ip->i_itemp != NULL); |
898621d5 | 1505 | ASSERT(ip->i_itemp->ili_lock_flags == 0); |
8f04c47a | 1506 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
1da177e4 | 1507 | |
673e8e59 CH |
1508 | trace_xfs_itruncate_extents_start(ip, new_size); |
1509 | ||
4e529339 | 1510 | flags |= xfs_bmapi_aflag(whichfork); |
13b86fc3 | 1511 | |
1da177e4 LT |
1512 | /* |
1513 | * Since it is possible for space to become allocated beyond | |
1514 | * the end of the file (in a crash where the space is allocated | |
1515 | * but the inode size is not yet updated), simply remove any | |
1516 | * blocks which show up between the new EOF and the maximum | |
4bbb04ab DW |
1517 | * possible file size. |
1518 | * | |
1519 | * We have to free all the blocks to the bmbt maximum offset, even if | |
1520 | * the page cache can't scale that far. | |
1da177e4 | 1521 | */ |
8f04c47a | 1522 | first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size); |
4bbb04ab DW |
1523 | if (first_unmap_block >= XFS_MAX_FILEOFF) { |
1524 | WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF); | |
8f04c47a | 1525 | return 0; |
4bbb04ab | 1526 | } |
8f04c47a | 1527 | |
4bbb04ab DW |
1528 | unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1; |
1529 | while (unmap_len > 0) { | |
02dff7bf | 1530 | ASSERT(tp->t_firstblock == NULLFSBLOCK); |
4bbb04ab DW |
1531 | error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len, |
1532 | flags, XFS_ITRUNC_MAX_EXTENTS); | |
8f04c47a | 1533 | if (error) |
d5a2e289 | 1534 | goto out; |
1da177e4 LT |
1535 | |
1536 | /* | |
1537 | * Duplicate the transaction that has the permanent | |
1538 | * reservation and commit the old transaction. | |
1539 | */ | |
9e28a242 | 1540 | error = xfs_defer_finish(&tp); |
8f04c47a | 1541 | if (error) |
9b1f4e98 | 1542 | goto out; |
1da177e4 | 1543 | |
411350df | 1544 | error = xfs_trans_roll_inode(&tp, ip); |
f6485057 | 1545 | if (error) |
8f04c47a | 1546 | goto out; |
1da177e4 | 1547 | } |
8f04c47a | 1548 | |
4919d42a DW |
1549 | if (whichfork == XFS_DATA_FORK) { |
1550 | /* Remove all pending CoW reservations. */ | |
1551 | error = xfs_reflink_cancel_cow_blocks(ip, &tp, | |
4bbb04ab | 1552 | first_unmap_block, XFS_MAX_FILEOFF, true); |
4919d42a DW |
1553 | if (error) |
1554 | goto out; | |
aa8968f2 | 1555 | |
4919d42a DW |
1556 | xfs_itruncate_clear_reflink_flags(ip); |
1557 | } | |
aa8968f2 | 1558 | |
673e8e59 CH |
1559 | /* |
1560 | * Always re-log the inode so that our permanent transaction can keep | |
1561 | * on rolling it forward in the log. | |
1562 | */ | |
1563 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1564 | ||
1565 | trace_xfs_itruncate_extents_end(ip, new_size); | |
1566 | ||
8f04c47a CH |
1567 | out: |
1568 | *tpp = tp; | |
1569 | return error; | |
8f04c47a CH |
1570 | } |
1571 | ||
c24b5dfa DC |
1572 | int |
1573 | xfs_release( | |
1574 | xfs_inode_t *ip) | |
1575 | { | |
1576 | xfs_mount_t *mp = ip->i_mount; | |
1577 | int error; | |
1578 | ||
c19b3b05 | 1579 | if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0)) |
c24b5dfa DC |
1580 | return 0; |
1581 | ||
1582 | /* If this is a read-only mount, don't do this (would generate I/O) */ | |
1583 | if (mp->m_flags & XFS_MOUNT_RDONLY) | |
1584 | return 0; | |
1585 | ||
1586 | if (!XFS_FORCED_SHUTDOWN(mp)) { | |
1587 | int truncated; | |
1588 | ||
c24b5dfa DC |
1589 | /* |
1590 | * If we previously truncated this file and removed old data | |
1591 | * in the process, we want to initiate "early" writeout on | |
1592 | * the last close. This is an attempt to combat the notorious | |
1593 | * NULL files problem which is particularly noticeable from a | |
1594 | * truncate down, buffered (re-)write (delalloc), followed by | |
1595 | * a crash. What we are effectively doing here is | |
1596 | * significantly reducing the time window where we'd otherwise | |
1597 | * be exposed to that problem. | |
1598 | */ | |
1599 | truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); | |
1600 | if (truncated) { | |
1601 | xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE); | |
eac152b4 | 1602 | if (ip->i_delayed_blks > 0) { |
2451337d | 1603 | error = filemap_flush(VFS_I(ip)->i_mapping); |
c24b5dfa DC |
1604 | if (error) |
1605 | return error; | |
1606 | } | |
1607 | } | |
1608 | } | |
1609 | ||
54d7b5c1 | 1610 | if (VFS_I(ip)->i_nlink == 0) |
c24b5dfa DC |
1611 | return 0; |
1612 | ||
1613 | if (xfs_can_free_eofblocks(ip, false)) { | |
1614 | ||
a36b9261 BF |
1615 | /* |
1616 | * Check if the inode is being opened, written and closed | |
1617 | * frequently and we have delayed allocation blocks outstanding | |
1618 | * (e.g. streaming writes from the NFS server), truncating the | |
1619 | * blocks past EOF will cause fragmentation to occur. | |
1620 | * | |
1621 | * In this case don't do the truncation, but we have to be | |
1622 | * careful how we detect this case. Blocks beyond EOF show up as | |
1623 | * i_delayed_blks even when the inode is clean, so we need to | |
1624 | * truncate them away first before checking for a dirty release. | |
1625 | * Hence on the first dirty close we will still remove the | |
1626 | * speculative allocation, but after that we will leave it in | |
1627 | * place. | |
1628 | */ | |
1629 | if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE)) | |
1630 | return 0; | |
c24b5dfa DC |
1631 | /* |
1632 | * If we can't get the iolock just skip truncating the blocks | |
c1e8d7c6 | 1633 | * past EOF because we could deadlock with the mmap_lock |
a36b9261 | 1634 | * otherwise. We'll get another chance to drop them once the |
c24b5dfa DC |
1635 | * last reference to the inode is dropped, so we'll never leak |
1636 | * blocks permanently. | |
c24b5dfa | 1637 | */ |
a36b9261 BF |
1638 | if (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { |
1639 | error = xfs_free_eofblocks(ip); | |
1640 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); | |
1641 | if (error) | |
1642 | return error; | |
1643 | } | |
c24b5dfa DC |
1644 | |
1645 | /* delalloc blocks after truncation means it really is dirty */ | |
1646 | if (ip->i_delayed_blks) | |
1647 | xfs_iflags_set(ip, XFS_IDIRTY_RELEASE); | |
1648 | } | |
1649 | return 0; | |
1650 | } | |
1651 | ||
f7be2d7f BF |
1652 | /* |
1653 | * xfs_inactive_truncate | |
1654 | * | |
1655 | * Called to perform a truncate when an inode becomes unlinked. | |
1656 | */ | |
1657 | STATIC int | |
1658 | xfs_inactive_truncate( | |
1659 | struct xfs_inode *ip) | |
1660 | { | |
1661 | struct xfs_mount *mp = ip->i_mount; | |
1662 | struct xfs_trans *tp; | |
1663 | int error; | |
1664 | ||
253f4911 | 1665 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); |
f7be2d7f BF |
1666 | if (error) { |
1667 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); | |
f7be2d7f BF |
1668 | return error; |
1669 | } | |
f7be2d7f BF |
1670 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
1671 | xfs_trans_ijoin(tp, ip, 0); | |
1672 | ||
1673 | /* | |
1674 | * Log the inode size first to prevent stale data exposure in the event | |
1675 | * of a system crash before the truncate completes. See the related | |
69bca807 | 1676 | * comment in xfs_vn_setattr_size() for details. |
f7be2d7f BF |
1677 | */ |
1678 | ip->i_d.di_size = 0; | |
1679 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1680 | ||
1681 | error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0); | |
1682 | if (error) | |
1683 | goto error_trans_cancel; | |
1684 | ||
daf83964 | 1685 | ASSERT(ip->i_df.if_nextents == 0); |
f7be2d7f | 1686 | |
70393313 | 1687 | error = xfs_trans_commit(tp); |
f7be2d7f BF |
1688 | if (error) |
1689 | goto error_unlock; | |
1690 | ||
1691 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1692 | return 0; | |
1693 | ||
1694 | error_trans_cancel: | |
4906e215 | 1695 | xfs_trans_cancel(tp); |
f7be2d7f BF |
1696 | error_unlock: |
1697 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1698 | return error; | |
1699 | } | |
1700 | ||
88877d2b BF |
1701 | /* |
1702 | * xfs_inactive_ifree() | |
1703 | * | |
1704 | * Perform the inode free when an inode is unlinked. | |
1705 | */ | |
1706 | STATIC int | |
1707 | xfs_inactive_ifree( | |
1708 | struct xfs_inode *ip) | |
1709 | { | |
88877d2b BF |
1710 | struct xfs_mount *mp = ip->i_mount; |
1711 | struct xfs_trans *tp; | |
1712 | int error; | |
1713 | ||
9d43b180 | 1714 | /* |
76d771b4 CH |
1715 | * We try to use a per-AG reservation for any block needed by the finobt |
1716 | * tree, but as the finobt feature predates the per-AG reservation | |
1717 | * support a degraded file system might not have enough space for the | |
1718 | * reservation at mount time. In that case try to dip into the reserved | |
1719 | * pool and pray. | |
9d43b180 BF |
1720 | * |
1721 | * Send a warning if the reservation does happen to fail, as the inode | |
1722 | * now remains allocated and sits on the unlinked list until the fs is | |
1723 | * repaired. | |
1724 | */ | |
e1f6ca11 | 1725 | if (unlikely(mp->m_finobt_nores)) { |
76d771b4 CH |
1726 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, |
1727 | XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, | |
1728 | &tp); | |
1729 | } else { | |
1730 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp); | |
1731 | } | |
88877d2b | 1732 | if (error) { |
2451337d | 1733 | if (error == -ENOSPC) { |
9d43b180 BF |
1734 | xfs_warn_ratelimited(mp, |
1735 | "Failed to remove inode(s) from unlinked list. " | |
1736 | "Please free space, unmount and run xfs_repair."); | |
1737 | } else { | |
1738 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); | |
1739 | } | |
88877d2b BF |
1740 | return error; |
1741 | } | |
1742 | ||
96355d5a DC |
1743 | /* |
1744 | * We do not hold the inode locked across the entire rolling transaction | |
1745 | * here. We only need to hold it for the first transaction that | |
1746 | * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the | |
1747 | * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode | |
1748 | * here breaks the relationship between cluster buffer invalidation and | |
1749 | * stale inode invalidation on cluster buffer item journal commit | |
1750 | * completion, and can result in leaving dirty stale inodes hanging | |
1751 | * around in memory. | |
1752 | * | |
1753 | * We have no need for serialising this inode operation against other | |
1754 | * operations - we freed the inode and hence reallocation is required | |
1755 | * and that will serialise on reallocating the space the deferops need | |
1756 | * to free. Hence we can unlock the inode on the first commit of | |
1757 | * the transaction rather than roll it right through the deferops. This | |
1758 | * avoids relogging the XFS_ISTALE inode. | |
1759 | * | |
1760 | * We check that xfs_ifree() hasn't grown an internal transaction roll | |
1761 | * by asserting that the inode is still locked when it returns. | |
1762 | */ | |
88877d2b | 1763 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
96355d5a | 1764 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
88877d2b | 1765 | |
0e0417f3 | 1766 | error = xfs_ifree(tp, ip); |
96355d5a | 1767 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
88877d2b BF |
1768 | if (error) { |
1769 | /* | |
1770 | * If we fail to free the inode, shut down. The cancel | |
1771 | * might do that, we need to make sure. Otherwise the | |
1772 | * inode might be lost for a long time or forever. | |
1773 | */ | |
1774 | if (!XFS_FORCED_SHUTDOWN(mp)) { | |
1775 | xfs_notice(mp, "%s: xfs_ifree returned error %d", | |
1776 | __func__, error); | |
1777 | xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); | |
1778 | } | |
4906e215 | 1779 | xfs_trans_cancel(tp); |
88877d2b BF |
1780 | return error; |
1781 | } | |
1782 | ||
1783 | /* | |
1784 | * Credit the quota account(s). The inode is gone. | |
1785 | */ | |
1786 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1); | |
1787 | ||
1788 | /* | |
d4a97a04 BF |
1789 | * Just ignore errors at this point. There is nothing we can do except |
1790 | * to try to keep going. Make sure it's not a silent error. | |
88877d2b | 1791 | */ |
70393313 | 1792 | error = xfs_trans_commit(tp); |
88877d2b BF |
1793 | if (error) |
1794 | xfs_notice(mp, "%s: xfs_trans_commit returned error %d", | |
1795 | __func__, error); | |
1796 | ||
88877d2b BF |
1797 | return 0; |
1798 | } | |
1799 | ||
c24b5dfa DC |
1800 | /* |
1801 | * xfs_inactive | |
1802 | * | |
1803 | * This is called when the vnode reference count for the vnode | |
1804 | * goes to zero. If the file has been unlinked, then it must | |
1805 | * now be truncated. Also, we clear all of the read-ahead state | |
1806 | * kept for the inode here since the file is now closed. | |
1807 | */ | |
74564fb4 | 1808 | void |
c24b5dfa DC |
1809 | xfs_inactive( |
1810 | xfs_inode_t *ip) | |
1811 | { | |
3d3c8b52 | 1812 | struct xfs_mount *mp; |
3d3c8b52 JL |
1813 | int error; |
1814 | int truncate = 0; | |
c24b5dfa DC |
1815 | |
1816 | /* | |
1817 | * If the inode is already free, then there can be nothing | |
1818 | * to clean up here. | |
1819 | */ | |
c19b3b05 | 1820 | if (VFS_I(ip)->i_mode == 0) { |
c24b5dfa | 1821 | ASSERT(ip->i_df.if_broot_bytes == 0); |
74564fb4 | 1822 | return; |
c24b5dfa DC |
1823 | } |
1824 | ||
1825 | mp = ip->i_mount; | |
17c12bcd | 1826 | ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY)); |
c24b5dfa | 1827 | |
c24b5dfa DC |
1828 | /* If this is a read-only mount, don't do this (would generate I/O) */ |
1829 | if (mp->m_flags & XFS_MOUNT_RDONLY) | |
74564fb4 | 1830 | return; |
c24b5dfa | 1831 | |
6231848c | 1832 | /* Try to clean out the cow blocks if there are any. */ |
51d62690 | 1833 | if (xfs_inode_has_cow_data(ip)) |
6231848c DW |
1834 | xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true); |
1835 | ||
54d7b5c1 | 1836 | if (VFS_I(ip)->i_nlink != 0) { |
c24b5dfa DC |
1837 | /* |
1838 | * force is true because we are evicting an inode from the | |
1839 | * cache. Post-eof blocks must be freed, lest we end up with | |
1840 | * broken free space accounting. | |
3b4683c2 BF |
1841 | * |
1842 | * Note: don't bother with iolock here since lockdep complains | |
1843 | * about acquiring it in reclaim context. We have the only | |
1844 | * reference to the inode at this point anyways. | |
c24b5dfa | 1845 | */ |
3b4683c2 | 1846 | if (xfs_can_free_eofblocks(ip, true)) |
a36b9261 | 1847 | xfs_free_eofblocks(ip); |
74564fb4 BF |
1848 | |
1849 | return; | |
c24b5dfa DC |
1850 | } |
1851 | ||
c19b3b05 | 1852 | if (S_ISREG(VFS_I(ip)->i_mode) && |
c24b5dfa | 1853 | (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 || |
daf83964 | 1854 | ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0)) |
c24b5dfa DC |
1855 | truncate = 1; |
1856 | ||
c14cfcca | 1857 | error = xfs_qm_dqattach(ip); |
c24b5dfa | 1858 | if (error) |
74564fb4 | 1859 | return; |
c24b5dfa | 1860 | |
c19b3b05 | 1861 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
36b21dde | 1862 | error = xfs_inactive_symlink(ip); |
f7be2d7f BF |
1863 | else if (truncate) |
1864 | error = xfs_inactive_truncate(ip); | |
1865 | if (error) | |
74564fb4 | 1866 | return; |
c24b5dfa DC |
1867 | |
1868 | /* | |
1869 | * If there are attributes associated with the file then blow them away | |
1870 | * now. The code calls a routine that recursively deconstructs the | |
6dfe5a04 | 1871 | * attribute fork. If also blows away the in-core attribute fork. |
c24b5dfa | 1872 | */ |
6dfe5a04 | 1873 | if (XFS_IFORK_Q(ip)) { |
c24b5dfa DC |
1874 | error = xfs_attr_inactive(ip); |
1875 | if (error) | |
74564fb4 | 1876 | return; |
c24b5dfa DC |
1877 | } |
1878 | ||
6dfe5a04 | 1879 | ASSERT(!ip->i_afp); |
6dfe5a04 | 1880 | ASSERT(ip->i_d.di_forkoff == 0); |
c24b5dfa DC |
1881 | |
1882 | /* | |
1883 | * Free the inode. | |
1884 | */ | |
88877d2b BF |
1885 | error = xfs_inactive_ifree(ip); |
1886 | if (error) | |
74564fb4 | 1887 | return; |
c24b5dfa DC |
1888 | |
1889 | /* | |
1890 | * Release the dquots held by inode, if any. | |
1891 | */ | |
1892 | xfs_qm_dqdetach(ip); | |
c24b5dfa DC |
1893 | } |
1894 | ||
9b247179 DW |
1895 | /* |
1896 | * In-Core Unlinked List Lookups | |
1897 | * ============================= | |
1898 | * | |
1899 | * Every inode is supposed to be reachable from some other piece of metadata | |
1900 | * with the exception of the root directory. Inodes with a connection to a | |
1901 | * file descriptor but not linked from anywhere in the on-disk directory tree | |
1902 | * are collectively known as unlinked inodes, though the filesystem itself | |
1903 | * maintains links to these inodes so that on-disk metadata are consistent. | |
1904 | * | |
1905 | * XFS implements a per-AG on-disk hash table of unlinked inodes. The AGI | |
1906 | * header contains a number of buckets that point to an inode, and each inode | |
1907 | * record has a pointer to the next inode in the hash chain. This | |
1908 | * singly-linked list causes scaling problems in the iunlink remove function | |
1909 | * because we must walk that list to find the inode that points to the inode | |
1910 | * being removed from the unlinked hash bucket list. | |
1911 | * | |
1912 | * What if we modelled the unlinked list as a collection of records capturing | |
1913 | * "X.next_unlinked = Y" relations? If we indexed those records on Y, we'd | |
1914 | * have a fast way to look up unlinked list predecessors, which avoids the | |
1915 | * slow list walk. That's exactly what we do here (in-core) with a per-AG | |
1916 | * rhashtable. | |
1917 | * | |
1918 | * Because this is a backref cache, we ignore operational failures since the | |
1919 | * iunlink code can fall back to the slow bucket walk. The only errors that | |
1920 | * should bubble out are for obviously incorrect situations. | |
1921 | * | |
1922 | * All users of the backref cache MUST hold the AGI buffer lock to serialize | |
1923 | * access or have otherwise provided for concurrency control. | |
1924 | */ | |
1925 | ||
1926 | /* Capture a "X.next_unlinked = Y" relationship. */ | |
1927 | struct xfs_iunlink { | |
1928 | struct rhash_head iu_rhash_head; | |
1929 | xfs_agino_t iu_agino; /* X */ | |
1930 | xfs_agino_t iu_next_unlinked; /* Y */ | |
1931 | }; | |
1932 | ||
1933 | /* Unlinked list predecessor lookup hashtable construction */ | |
1934 | static int | |
1935 | xfs_iunlink_obj_cmpfn( | |
1936 | struct rhashtable_compare_arg *arg, | |
1937 | const void *obj) | |
1938 | { | |
1939 | const xfs_agino_t *key = arg->key; | |
1940 | const struct xfs_iunlink *iu = obj; | |
1941 | ||
1942 | if (iu->iu_next_unlinked != *key) | |
1943 | return 1; | |
1944 | return 0; | |
1945 | } | |
1946 | ||
1947 | static const struct rhashtable_params xfs_iunlink_hash_params = { | |
1948 | .min_size = XFS_AGI_UNLINKED_BUCKETS, | |
1949 | .key_len = sizeof(xfs_agino_t), | |
1950 | .key_offset = offsetof(struct xfs_iunlink, | |
1951 | iu_next_unlinked), | |
1952 | .head_offset = offsetof(struct xfs_iunlink, iu_rhash_head), | |
1953 | .automatic_shrinking = true, | |
1954 | .obj_cmpfn = xfs_iunlink_obj_cmpfn, | |
1955 | }; | |
1956 | ||
1957 | /* | |
1958 | * Return X, where X.next_unlinked == @agino. Returns NULLAGINO if no such | |
1959 | * relation is found. | |
1960 | */ | |
1961 | static xfs_agino_t | |
1962 | xfs_iunlink_lookup_backref( | |
1963 | struct xfs_perag *pag, | |
1964 | xfs_agino_t agino) | |
1965 | { | |
1966 | struct xfs_iunlink *iu; | |
1967 | ||
1968 | iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino, | |
1969 | xfs_iunlink_hash_params); | |
1970 | return iu ? iu->iu_agino : NULLAGINO; | |
1971 | } | |
1972 | ||
1973 | /* | |
1974 | * Take ownership of an iunlink cache entry and insert it into the hash table. | |
1975 | * If successful, the entry will be owned by the cache; if not, it is freed. | |
1976 | * Either way, the caller does not own @iu after this call. | |
1977 | */ | |
1978 | static int | |
1979 | xfs_iunlink_insert_backref( | |
1980 | struct xfs_perag *pag, | |
1981 | struct xfs_iunlink *iu) | |
1982 | { | |
1983 | int error; | |
1984 | ||
1985 | error = rhashtable_insert_fast(&pag->pagi_unlinked_hash, | |
1986 | &iu->iu_rhash_head, xfs_iunlink_hash_params); | |
1987 | /* | |
1988 | * Fail loudly if there already was an entry because that's a sign of | |
1989 | * corruption of in-memory data. Also fail loudly if we see an error | |
1990 | * code we didn't anticipate from the rhashtable code. Currently we | |
1991 | * only anticipate ENOMEM. | |
1992 | */ | |
1993 | if (error) { | |
1994 | WARN(error != -ENOMEM, "iunlink cache insert error %d", error); | |
1995 | kmem_free(iu); | |
1996 | } | |
1997 | /* | |
1998 | * Absorb any runtime errors that aren't a result of corruption because | |
1999 | * this is a cache and we can always fall back to bucket list scanning. | |
2000 | */ | |
2001 | if (error != 0 && error != -EEXIST) | |
2002 | error = 0; | |
2003 | return error; | |
2004 | } | |
2005 | ||
2006 | /* Remember that @prev_agino.next_unlinked = @this_agino. */ | |
2007 | static int | |
2008 | xfs_iunlink_add_backref( | |
2009 | struct xfs_perag *pag, | |
2010 | xfs_agino_t prev_agino, | |
2011 | xfs_agino_t this_agino) | |
2012 | { | |
2013 | struct xfs_iunlink *iu; | |
2014 | ||
2015 | if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK)) | |
2016 | return 0; | |
2017 | ||
707e0dda | 2018 | iu = kmem_zalloc(sizeof(*iu), KM_NOFS); |
9b247179 DW |
2019 | iu->iu_agino = prev_agino; |
2020 | iu->iu_next_unlinked = this_agino; | |
2021 | ||
2022 | return xfs_iunlink_insert_backref(pag, iu); | |
2023 | } | |
2024 | ||
2025 | /* | |
2026 | * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked. | |
2027 | * If @next_unlinked is NULLAGINO, we drop the backref and exit. If there | |
2028 | * wasn't any such entry then we don't bother. | |
2029 | */ | |
2030 | static int | |
2031 | xfs_iunlink_change_backref( | |
2032 | struct xfs_perag *pag, | |
2033 | xfs_agino_t agino, | |
2034 | xfs_agino_t next_unlinked) | |
2035 | { | |
2036 | struct xfs_iunlink *iu; | |
2037 | int error; | |
2038 | ||
2039 | /* Look up the old entry; if there wasn't one then exit. */ | |
2040 | iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino, | |
2041 | xfs_iunlink_hash_params); | |
2042 | if (!iu) | |
2043 | return 0; | |
2044 | ||
2045 | /* | |
2046 | * Remove the entry. This shouldn't ever return an error, but if we | |
2047 | * couldn't remove the old entry we don't want to add it again to the | |
2048 | * hash table, and if the entry disappeared on us then someone's | |
2049 | * violated the locking rules and we need to fail loudly. Either way | |
2050 | * we cannot remove the inode because internal state is or would have | |
2051 | * been corrupt. | |
2052 | */ | |
2053 | error = rhashtable_remove_fast(&pag->pagi_unlinked_hash, | |
2054 | &iu->iu_rhash_head, xfs_iunlink_hash_params); | |
2055 | if (error) | |
2056 | return error; | |
2057 | ||
2058 | /* If there is no new next entry just free our item and return. */ | |
2059 | if (next_unlinked == NULLAGINO) { | |
2060 | kmem_free(iu); | |
2061 | return 0; | |
2062 | } | |
2063 | ||
2064 | /* Update the entry and re-add it to the hash table. */ | |
2065 | iu->iu_next_unlinked = next_unlinked; | |
2066 | return xfs_iunlink_insert_backref(pag, iu); | |
2067 | } | |
2068 | ||
2069 | /* Set up the in-core predecessor structures. */ | |
2070 | int | |
2071 | xfs_iunlink_init( | |
2072 | struct xfs_perag *pag) | |
2073 | { | |
2074 | return rhashtable_init(&pag->pagi_unlinked_hash, | |
2075 | &xfs_iunlink_hash_params); | |
2076 | } | |
2077 | ||
2078 | /* Free the in-core predecessor structures. */ | |
2079 | static void | |
2080 | xfs_iunlink_free_item( | |
2081 | void *ptr, | |
2082 | void *arg) | |
2083 | { | |
2084 | struct xfs_iunlink *iu = ptr; | |
2085 | bool *freed_anything = arg; | |
2086 | ||
2087 | *freed_anything = true; | |
2088 | kmem_free(iu); | |
2089 | } | |
2090 | ||
2091 | void | |
2092 | xfs_iunlink_destroy( | |
2093 | struct xfs_perag *pag) | |
2094 | { | |
2095 | bool freed_anything = false; | |
2096 | ||
2097 | rhashtable_free_and_destroy(&pag->pagi_unlinked_hash, | |
2098 | xfs_iunlink_free_item, &freed_anything); | |
2099 | ||
2100 | ASSERT(freed_anything == false || XFS_FORCED_SHUTDOWN(pag->pag_mount)); | |
2101 | } | |
2102 | ||
9a4a5118 DW |
2103 | /* |
2104 | * Point the AGI unlinked bucket at an inode and log the results. The caller | |
2105 | * is responsible for validating the old value. | |
2106 | */ | |
2107 | STATIC int | |
2108 | xfs_iunlink_update_bucket( | |
2109 | struct xfs_trans *tp, | |
2110 | xfs_agnumber_t agno, | |
2111 | struct xfs_buf *agibp, | |
2112 | unsigned int bucket_index, | |
2113 | xfs_agino_t new_agino) | |
2114 | { | |
370c782b | 2115 | struct xfs_agi *agi = agibp->b_addr; |
9a4a5118 DW |
2116 | xfs_agino_t old_value; |
2117 | int offset; | |
2118 | ||
2119 | ASSERT(xfs_verify_agino_or_null(tp->t_mountp, agno, new_agino)); | |
2120 | ||
2121 | old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]); | |
2122 | trace_xfs_iunlink_update_bucket(tp->t_mountp, agno, bucket_index, | |
2123 | old_value, new_agino); | |
2124 | ||
2125 | /* | |
2126 | * We should never find the head of the list already set to the value | |
2127 | * passed in because either we're adding or removing ourselves from the | |
2128 | * head of the list. | |
2129 | */ | |
a5155b87 | 2130 | if (old_value == new_agino) { |
8d57c216 | 2131 | xfs_buf_mark_corrupt(agibp); |
9a4a5118 | 2132 | return -EFSCORRUPTED; |
a5155b87 | 2133 | } |
9a4a5118 DW |
2134 | |
2135 | agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino); | |
2136 | offset = offsetof(struct xfs_agi, agi_unlinked) + | |
2137 | (sizeof(xfs_agino_t) * bucket_index); | |
2138 | xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1); | |
2139 | return 0; | |
2140 | } | |
2141 | ||
f2fc16a3 DW |
2142 | /* Set an on-disk inode's next_unlinked pointer. */ |
2143 | STATIC void | |
2144 | xfs_iunlink_update_dinode( | |
2145 | struct xfs_trans *tp, | |
2146 | xfs_agnumber_t agno, | |
2147 | xfs_agino_t agino, | |
2148 | struct xfs_buf *ibp, | |
2149 | struct xfs_dinode *dip, | |
2150 | struct xfs_imap *imap, | |
2151 | xfs_agino_t next_agino) | |
2152 | { | |
2153 | struct xfs_mount *mp = tp->t_mountp; | |
2154 | int offset; | |
2155 | ||
2156 | ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino)); | |
2157 | ||
2158 | trace_xfs_iunlink_update_dinode(mp, agno, agino, | |
2159 | be32_to_cpu(dip->di_next_unlinked), next_agino); | |
2160 | ||
2161 | dip->di_next_unlinked = cpu_to_be32(next_agino); | |
2162 | offset = imap->im_boffset + | |
2163 | offsetof(struct xfs_dinode, di_next_unlinked); | |
2164 | ||
2165 | /* need to recalc the inode CRC if appropriate */ | |
2166 | xfs_dinode_calc_crc(mp, dip); | |
2167 | xfs_trans_inode_buf(tp, ibp); | |
2168 | xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1); | |
2169 | xfs_inobp_check(mp, ibp); | |
2170 | } | |
2171 | ||
2172 | /* Set an in-core inode's unlinked pointer and return the old value. */ | |
2173 | STATIC int | |
2174 | xfs_iunlink_update_inode( | |
2175 | struct xfs_trans *tp, | |
2176 | struct xfs_inode *ip, | |
2177 | xfs_agnumber_t agno, | |
2178 | xfs_agino_t next_agino, | |
2179 | xfs_agino_t *old_next_agino) | |
2180 | { | |
2181 | struct xfs_mount *mp = tp->t_mountp; | |
2182 | struct xfs_dinode *dip; | |
2183 | struct xfs_buf *ibp; | |
2184 | xfs_agino_t old_value; | |
2185 | int error; | |
2186 | ||
2187 | ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino)); | |
2188 | ||
c1995079 | 2189 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 0); |
f2fc16a3 DW |
2190 | if (error) |
2191 | return error; | |
2192 | ||
2193 | /* Make sure the old pointer isn't garbage. */ | |
2194 | old_value = be32_to_cpu(dip->di_next_unlinked); | |
2195 | if (!xfs_verify_agino_or_null(mp, agno, old_value)) { | |
a5155b87 DW |
2196 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip, |
2197 | sizeof(*dip), __this_address); | |
f2fc16a3 DW |
2198 | error = -EFSCORRUPTED; |
2199 | goto out; | |
2200 | } | |
2201 | ||
2202 | /* | |
2203 | * Since we're updating a linked list, we should never find that the | |
2204 | * current pointer is the same as the new value, unless we're | |
2205 | * terminating the list. | |
2206 | */ | |
2207 | *old_next_agino = old_value; | |
2208 | if (old_value == next_agino) { | |
a5155b87 DW |
2209 | if (next_agino != NULLAGINO) { |
2210 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, | |
2211 | dip, sizeof(*dip), __this_address); | |
f2fc16a3 | 2212 | error = -EFSCORRUPTED; |
a5155b87 | 2213 | } |
f2fc16a3 DW |
2214 | goto out; |
2215 | } | |
2216 | ||
2217 | /* Ok, update the new pointer. */ | |
2218 | xfs_iunlink_update_dinode(tp, agno, XFS_INO_TO_AGINO(mp, ip->i_ino), | |
2219 | ibp, dip, &ip->i_imap, next_agino); | |
2220 | return 0; | |
2221 | out: | |
2222 | xfs_trans_brelse(tp, ibp); | |
2223 | return error; | |
2224 | } | |
2225 | ||
1da177e4 | 2226 | /* |
c4a6bf7f DW |
2227 | * This is called when the inode's link count has gone to 0 or we are creating |
2228 | * a tmpfile via O_TMPFILE. The inode @ip must have nlink == 0. | |
54d7b5c1 DC |
2229 | * |
2230 | * We place the on-disk inode on a list in the AGI. It will be pulled from this | |
2231 | * list when the inode is freed. | |
1da177e4 | 2232 | */ |
54d7b5c1 | 2233 | STATIC int |
1da177e4 | 2234 | xfs_iunlink( |
5837f625 DW |
2235 | struct xfs_trans *tp, |
2236 | struct xfs_inode *ip) | |
1da177e4 | 2237 | { |
5837f625 DW |
2238 | struct xfs_mount *mp = tp->t_mountp; |
2239 | struct xfs_agi *agi; | |
5837f625 | 2240 | struct xfs_buf *agibp; |
86bfd375 | 2241 | xfs_agino_t next_agino; |
5837f625 DW |
2242 | xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino); |
2243 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino); | |
2244 | short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; | |
5837f625 | 2245 | int error; |
1da177e4 | 2246 | |
c4a6bf7f | 2247 | ASSERT(VFS_I(ip)->i_nlink == 0); |
c19b3b05 | 2248 | ASSERT(VFS_I(ip)->i_mode != 0); |
4664c66c | 2249 | trace_xfs_iunlink(ip); |
1da177e4 | 2250 | |
5837f625 DW |
2251 | /* Get the agi buffer first. It ensures lock ordering on the list. */ |
2252 | error = xfs_read_agi(mp, tp, agno, &agibp); | |
859d7182 | 2253 | if (error) |
1da177e4 | 2254 | return error; |
370c782b | 2255 | agi = agibp->b_addr; |
5e1be0fb | 2256 | |
1da177e4 | 2257 | /* |
86bfd375 DW |
2258 | * Get the index into the agi hash table for the list this inode will |
2259 | * go on. Make sure the pointer isn't garbage and that this inode | |
2260 | * isn't already on the list. | |
1da177e4 | 2261 | */ |
86bfd375 DW |
2262 | next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); |
2263 | if (next_agino == agino || | |
a5155b87 | 2264 | !xfs_verify_agino_or_null(mp, agno, next_agino)) { |
8d57c216 | 2265 | xfs_buf_mark_corrupt(agibp); |
86bfd375 | 2266 | return -EFSCORRUPTED; |
a5155b87 | 2267 | } |
1da177e4 | 2268 | |
86bfd375 | 2269 | if (next_agino != NULLAGINO) { |
9b247179 DW |
2270 | struct xfs_perag *pag; |
2271 | xfs_agino_t old_agino; | |
f2fc16a3 | 2272 | |
1da177e4 | 2273 | /* |
f2fc16a3 DW |
2274 | * There is already another inode in the bucket, so point this |
2275 | * inode to the current head of the list. | |
1da177e4 | 2276 | */ |
f2fc16a3 DW |
2277 | error = xfs_iunlink_update_inode(tp, ip, agno, next_agino, |
2278 | &old_agino); | |
c319b58b VA |
2279 | if (error) |
2280 | return error; | |
f2fc16a3 | 2281 | ASSERT(old_agino == NULLAGINO); |
9b247179 DW |
2282 | |
2283 | /* | |
2284 | * agino has been unlinked, add a backref from the next inode | |
2285 | * back to agino. | |
2286 | */ | |
2287 | pag = xfs_perag_get(mp, agno); | |
2288 | error = xfs_iunlink_add_backref(pag, agino, next_agino); | |
2289 | xfs_perag_put(pag); | |
2290 | if (error) | |
2291 | return error; | |
1da177e4 LT |
2292 | } |
2293 | ||
9a4a5118 DW |
2294 | /* Point the head of the list to point to this inode. */ |
2295 | return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index, agino); | |
1da177e4 LT |
2296 | } |
2297 | ||
23ffa52c DW |
2298 | /* Return the imap, dinode pointer, and buffer for an inode. */ |
2299 | STATIC int | |
2300 | xfs_iunlink_map_ino( | |
2301 | struct xfs_trans *tp, | |
2302 | xfs_agnumber_t agno, | |
2303 | xfs_agino_t agino, | |
2304 | struct xfs_imap *imap, | |
2305 | struct xfs_dinode **dipp, | |
2306 | struct xfs_buf **bpp) | |
2307 | { | |
2308 | struct xfs_mount *mp = tp->t_mountp; | |
2309 | int error; | |
2310 | ||
2311 | imap->im_blkno = 0; | |
2312 | error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0); | |
2313 | if (error) { | |
2314 | xfs_warn(mp, "%s: xfs_imap returned error %d.", | |
2315 | __func__, error); | |
2316 | return error; | |
2317 | } | |
2318 | ||
c1995079 | 2319 | error = xfs_imap_to_bp(mp, tp, imap, dipp, bpp, 0); |
23ffa52c DW |
2320 | if (error) { |
2321 | xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.", | |
2322 | __func__, error); | |
2323 | return error; | |
2324 | } | |
2325 | ||
2326 | return 0; | |
2327 | } | |
2328 | ||
2329 | /* | |
2330 | * Walk the unlinked chain from @head_agino until we find the inode that | |
2331 | * points to @target_agino. Return the inode number, map, dinode pointer, | |
2332 | * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp. | |
2333 | * | |
2334 | * @tp, @pag, @head_agino, and @target_agino are input parameters. | |
2335 | * @agino, @imap, @dipp, and @bpp are all output parameters. | |
2336 | * | |
2337 | * Do not call this function if @target_agino is the head of the list. | |
2338 | */ | |
2339 | STATIC int | |
2340 | xfs_iunlink_map_prev( | |
2341 | struct xfs_trans *tp, | |
2342 | xfs_agnumber_t agno, | |
2343 | xfs_agino_t head_agino, | |
2344 | xfs_agino_t target_agino, | |
2345 | xfs_agino_t *agino, | |
2346 | struct xfs_imap *imap, | |
2347 | struct xfs_dinode **dipp, | |
9b247179 DW |
2348 | struct xfs_buf **bpp, |
2349 | struct xfs_perag *pag) | |
23ffa52c DW |
2350 | { |
2351 | struct xfs_mount *mp = tp->t_mountp; | |
2352 | xfs_agino_t next_agino; | |
2353 | int error; | |
2354 | ||
2355 | ASSERT(head_agino != target_agino); | |
2356 | *bpp = NULL; | |
2357 | ||
9b247179 DW |
2358 | /* See if our backref cache can find it faster. */ |
2359 | *agino = xfs_iunlink_lookup_backref(pag, target_agino); | |
2360 | if (*agino != NULLAGINO) { | |
2361 | error = xfs_iunlink_map_ino(tp, agno, *agino, imap, dipp, bpp); | |
2362 | if (error) | |
2363 | return error; | |
2364 | ||
2365 | if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino) | |
2366 | return 0; | |
2367 | ||
2368 | /* | |
2369 | * If we get here the cache contents were corrupt, so drop the | |
2370 | * buffer and fall back to walking the bucket list. | |
2371 | */ | |
2372 | xfs_trans_brelse(tp, *bpp); | |
2373 | *bpp = NULL; | |
2374 | WARN_ON_ONCE(1); | |
2375 | } | |
2376 | ||
2377 | trace_xfs_iunlink_map_prev_fallback(mp, agno); | |
2378 | ||
2379 | /* Otherwise, walk the entire bucket until we find it. */ | |
23ffa52c DW |
2380 | next_agino = head_agino; |
2381 | while (next_agino != target_agino) { | |
2382 | xfs_agino_t unlinked_agino; | |
2383 | ||
2384 | if (*bpp) | |
2385 | xfs_trans_brelse(tp, *bpp); | |
2386 | ||
2387 | *agino = next_agino; | |
2388 | error = xfs_iunlink_map_ino(tp, agno, next_agino, imap, dipp, | |
2389 | bpp); | |
2390 | if (error) | |
2391 | return error; | |
2392 | ||
2393 | unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked); | |
2394 | /* | |
2395 | * Make sure this pointer is valid and isn't an obvious | |
2396 | * infinite loop. | |
2397 | */ | |
2398 | if (!xfs_verify_agino(mp, agno, unlinked_agino) || | |
2399 | next_agino == unlinked_agino) { | |
2400 | XFS_CORRUPTION_ERROR(__func__, | |
2401 | XFS_ERRLEVEL_LOW, mp, | |
2402 | *dipp, sizeof(**dipp)); | |
2403 | error = -EFSCORRUPTED; | |
2404 | return error; | |
2405 | } | |
2406 | next_agino = unlinked_agino; | |
2407 | } | |
2408 | ||
2409 | return 0; | |
2410 | } | |
2411 | ||
1da177e4 LT |
2412 | /* |
2413 | * Pull the on-disk inode from the AGI unlinked list. | |
2414 | */ | |
2415 | STATIC int | |
2416 | xfs_iunlink_remove( | |
5837f625 DW |
2417 | struct xfs_trans *tp, |
2418 | struct xfs_inode *ip) | |
1da177e4 | 2419 | { |
5837f625 DW |
2420 | struct xfs_mount *mp = tp->t_mountp; |
2421 | struct xfs_agi *agi; | |
5837f625 | 2422 | struct xfs_buf *agibp; |
5837f625 DW |
2423 | struct xfs_buf *last_ibp; |
2424 | struct xfs_dinode *last_dip = NULL; | |
9b247179 | 2425 | struct xfs_perag *pag = NULL; |
5837f625 DW |
2426 | xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino); |
2427 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino); | |
2428 | xfs_agino_t next_agino; | |
b1d2a068 | 2429 | xfs_agino_t head_agino; |
5837f625 | 2430 | short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
5837f625 | 2431 | int error; |
1da177e4 | 2432 | |
4664c66c DW |
2433 | trace_xfs_iunlink_remove(ip); |
2434 | ||
5837f625 | 2435 | /* Get the agi buffer first. It ensures lock ordering on the list. */ |
5e1be0fb CH |
2436 | error = xfs_read_agi(mp, tp, agno, &agibp); |
2437 | if (error) | |
1da177e4 | 2438 | return error; |
370c782b | 2439 | agi = agibp->b_addr; |
5e1be0fb | 2440 | |
1da177e4 | 2441 | /* |
86bfd375 DW |
2442 | * Get the index into the agi hash table for the list this inode will |
2443 | * go on. Make sure the head pointer isn't garbage. | |
1da177e4 | 2444 | */ |
b1d2a068 DW |
2445 | head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); |
2446 | if (!xfs_verify_agino(mp, agno, head_agino)) { | |
d2e73665 DW |
2447 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, |
2448 | agi, sizeof(*agi)); | |
2449 | return -EFSCORRUPTED; | |
2450 | } | |
1da177e4 | 2451 | |
b1d2a068 DW |
2452 | /* |
2453 | * Set our inode's next_unlinked pointer to NULL and then return | |
2454 | * the old pointer value so that we can update whatever was previous | |
2455 | * to us in the list to point to whatever was next in the list. | |
2456 | */ | |
2457 | error = xfs_iunlink_update_inode(tp, ip, agno, NULLAGINO, &next_agino); | |
2458 | if (error) | |
2459 | return error; | |
9a4a5118 | 2460 | |
9b247179 DW |
2461 | /* |
2462 | * If there was a backref pointing from the next inode back to this | |
2463 | * one, remove it because we've removed this inode from the list. | |
2464 | * | |
2465 | * Later, if this inode was in the middle of the list we'll update | |
2466 | * this inode's backref to point from the next inode. | |
2467 | */ | |
2468 | if (next_agino != NULLAGINO) { | |
2469 | pag = xfs_perag_get(mp, agno); | |
2470 | error = xfs_iunlink_change_backref(pag, next_agino, | |
2471 | NULLAGINO); | |
2472 | if (error) | |
2473 | goto out; | |
2474 | } | |
2475 | ||
b1d2a068 | 2476 | if (head_agino == agino) { |
9a4a5118 DW |
2477 | /* Point the head of the list to the next unlinked inode. */ |
2478 | error = xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index, | |
2479 | next_agino); | |
2480 | if (error) | |
9b247179 | 2481 | goto out; |
1da177e4 | 2482 | } else { |
f2fc16a3 DW |
2483 | struct xfs_imap imap; |
2484 | xfs_agino_t prev_agino; | |
2485 | ||
9b247179 DW |
2486 | if (!pag) |
2487 | pag = xfs_perag_get(mp, agno); | |
2488 | ||
23ffa52c | 2489 | /* We need to search the list for the inode being freed. */ |
b1d2a068 | 2490 | error = xfs_iunlink_map_prev(tp, agno, head_agino, agino, |
9b247179 DW |
2491 | &prev_agino, &imap, &last_dip, &last_ibp, |
2492 | pag); | |
23ffa52c | 2493 | if (error) |
9b247179 | 2494 | goto out; |
475ee413 | 2495 | |
f2fc16a3 DW |
2496 | /* Point the previous inode on the list to the next inode. */ |
2497 | xfs_iunlink_update_dinode(tp, agno, prev_agino, last_ibp, | |
2498 | last_dip, &imap, next_agino); | |
9b247179 DW |
2499 | |
2500 | /* | |
2501 | * Now we deal with the backref for this inode. If this inode | |
2502 | * pointed at a real inode, change the backref that pointed to | |
2503 | * us to point to our old next. If this inode was the end of | |
2504 | * the list, delete the backref that pointed to us. Note that | |
2505 | * change_backref takes care of deleting the backref if | |
2506 | * next_agino is NULLAGINO. | |
2507 | */ | |
2508 | error = xfs_iunlink_change_backref(pag, agino, next_agino); | |
2509 | if (error) | |
2510 | goto out; | |
1da177e4 | 2511 | } |
9b247179 DW |
2512 | |
2513 | out: | |
2514 | if (pag) | |
2515 | xfs_perag_put(pag); | |
2516 | return error; | |
1da177e4 LT |
2517 | } |
2518 | ||
5806165a | 2519 | /* |
71e3e356 DC |
2520 | * Look up the inode number specified and if it is not already marked XFS_ISTALE |
2521 | * mark it stale. We should only find clean inodes in this lookup that aren't | |
2522 | * already stale. | |
5806165a | 2523 | */ |
71e3e356 DC |
2524 | static void |
2525 | xfs_ifree_mark_inode_stale( | |
2526 | struct xfs_buf *bp, | |
5806165a | 2527 | struct xfs_inode *free_ip, |
d9fdd0ad | 2528 | xfs_ino_t inum) |
5806165a | 2529 | { |
71e3e356 DC |
2530 | struct xfs_mount *mp = bp->b_mount; |
2531 | struct xfs_perag *pag = bp->b_pag; | |
2532 | struct xfs_inode_log_item *iip; | |
5806165a DC |
2533 | struct xfs_inode *ip; |
2534 | ||
2535 | retry: | |
2536 | rcu_read_lock(); | |
2537 | ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum)); | |
2538 | ||
2539 | /* Inode not in memory, nothing to do */ | |
71e3e356 DC |
2540 | if (!ip) { |
2541 | rcu_read_unlock(); | |
2542 | return; | |
2543 | } | |
5806165a DC |
2544 | |
2545 | /* | |
2546 | * because this is an RCU protected lookup, we could find a recently | |
2547 | * freed or even reallocated inode during the lookup. We need to check | |
2548 | * under the i_flags_lock for a valid inode here. Skip it if it is not | |
2549 | * valid, the wrong inode or stale. | |
2550 | */ | |
2551 | spin_lock(&ip->i_flags_lock); | |
2552 | if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE)) { | |
2553 | spin_unlock(&ip->i_flags_lock); | |
71e3e356 DC |
2554 | rcu_read_unlock(); |
2555 | return; | |
5806165a | 2556 | } |
5806165a DC |
2557 | |
2558 | /* | |
2559 | * Don't try to lock/unlock the current inode, but we _cannot_ skip the | |
2560 | * other inodes that we did not find in the list attached to the buffer | |
2561 | * and are not already marked stale. If we can't lock it, back off and | |
2562 | * retry. | |
2563 | */ | |
2564 | if (ip != free_ip) { | |
2565 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { | |
71e3e356 | 2566 | spin_unlock(&ip->i_flags_lock); |
5806165a DC |
2567 | rcu_read_unlock(); |
2568 | delay(1); | |
2569 | goto retry; | |
2570 | } | |
5806165a | 2571 | } |
71e3e356 DC |
2572 | ip->i_flags |= XFS_ISTALE; |
2573 | spin_unlock(&ip->i_flags_lock); | |
5806165a DC |
2574 | rcu_read_unlock(); |
2575 | ||
71e3e356 DC |
2576 | /* |
2577 | * If we can't get the flush lock, the inode is already attached. All | |
2578 | * we needed to do here is mark the inode stale so buffer IO completion | |
2579 | * will remove it from the AIL. | |
2580 | */ | |
2581 | iip = ip->i_itemp; | |
2582 | if (!xfs_iflock_nowait(ip)) { | |
2583 | ASSERT(!list_empty(&iip->ili_item.li_bio_list)); | |
2584 | ASSERT(iip->ili_last_fields); | |
2585 | goto out_iunlock; | |
2586 | } | |
5806165a DC |
2587 | |
2588 | /* | |
48d55e2a DC |
2589 | * Inodes not attached to the buffer can be released immediately. |
2590 | * Everything else has to go through xfs_iflush_abort() on journal | |
2591 | * commit as the flock synchronises removal of the inode from the | |
2592 | * cluster buffer against inode reclaim. | |
5806165a | 2593 | */ |
48d55e2a | 2594 | if (!iip || list_empty(&iip->ili_item.li_bio_list)) { |
5806165a | 2595 | xfs_ifunlock(ip); |
71e3e356 | 2596 | goto out_iunlock; |
5806165a | 2597 | } |
5806165a | 2598 | |
71e3e356 | 2599 | /* we have a dirty inode in memory that has not yet been flushed. */ |
71e3e356 DC |
2600 | spin_lock(&iip->ili_lock); |
2601 | iip->ili_last_fields = iip->ili_fields; | |
2602 | iip->ili_fields = 0; | |
2603 | iip->ili_fsync_fields = 0; | |
2604 | spin_unlock(&iip->ili_lock); | |
71e3e356 DC |
2605 | ASSERT(iip->ili_last_fields); |
2606 | ||
2607 | out_iunlock: | |
2608 | if (ip != free_ip) | |
2609 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
5806165a DC |
2610 | } |
2611 | ||
5b3eed75 | 2612 | /* |
0b8182db | 2613 | * A big issue when freeing the inode cluster is that we _cannot_ skip any |
5b3eed75 DC |
2614 | * inodes that are in memory - they all must be marked stale and attached to |
2615 | * the cluster buffer. | |
2616 | */ | |
2a30f36d | 2617 | STATIC int |
1da177e4 | 2618 | xfs_ifree_cluster( |
71e3e356 DC |
2619 | struct xfs_inode *free_ip, |
2620 | struct xfs_trans *tp, | |
09b56604 | 2621 | struct xfs_icluster *xic) |
1da177e4 | 2622 | { |
71e3e356 DC |
2623 | struct xfs_mount *mp = free_ip->i_mount; |
2624 | struct xfs_ino_geometry *igeo = M_IGEO(mp); | |
2625 | struct xfs_buf *bp; | |
2626 | xfs_daddr_t blkno; | |
2627 | xfs_ino_t inum = xic->first_ino; | |
1da177e4 | 2628 | int nbufs; |
5b257b4a | 2629 | int i, j; |
3cdaa189 | 2630 | int ioffset; |
ce92464c | 2631 | int error; |
1da177e4 | 2632 | |
ef325959 | 2633 | nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster; |
1da177e4 | 2634 | |
ef325959 | 2635 | for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) { |
09b56604 BF |
2636 | /* |
2637 | * The allocation bitmap tells us which inodes of the chunk were | |
2638 | * physically allocated. Skip the cluster if an inode falls into | |
2639 | * a sparse region. | |
2640 | */ | |
3cdaa189 BF |
2641 | ioffset = inum - xic->first_ino; |
2642 | if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) { | |
ef325959 | 2643 | ASSERT(ioffset % igeo->inodes_per_cluster == 0); |
09b56604 BF |
2644 | continue; |
2645 | } | |
2646 | ||
1da177e4 LT |
2647 | blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), |
2648 | XFS_INO_TO_AGBNO(mp, inum)); | |
2649 | ||
5b257b4a DC |
2650 | /* |
2651 | * We obtain and lock the backing buffer first in the process | |
2652 | * here, as we have to ensure that any dirty inode that we | |
2653 | * can't get the flush lock on is attached to the buffer. | |
2654 | * If we scan the in-memory inodes first, then buffer IO can | |
2655 | * complete before we get a lock on it, and hence we may fail | |
2656 | * to mark all the active inodes on the buffer stale. | |
2657 | */ | |
ce92464c DW |
2658 | error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, |
2659 | mp->m_bsize * igeo->blocks_per_cluster, | |
2660 | XBF_UNMAPPED, &bp); | |
71e3e356 | 2661 | if (error) |
ce92464c | 2662 | return error; |
b0f539de DC |
2663 | |
2664 | /* | |
2665 | * This buffer may not have been correctly initialised as we | |
2666 | * didn't read it from disk. That's not important because we are | |
2667 | * only using to mark the buffer as stale in the log, and to | |
2668 | * attach stale cached inodes on it. That means it will never be | |
2669 | * dispatched for IO. If it is, we want to know about it, and we | |
2670 | * want it to fail. We can acheive this by adding a write | |
2671 | * verifier to the buffer. | |
2672 | */ | |
8c4ce794 | 2673 | bp->b_ops = &xfs_inode_buf_ops; |
b0f539de | 2674 | |
5b257b4a | 2675 | /* |
71e3e356 DC |
2676 | * Now we need to set all the cached clean inodes as XFS_ISTALE, |
2677 | * too. This requires lookups, and will skip inodes that we've | |
2678 | * already marked XFS_ISTALE. | |
1da177e4 | 2679 | */ |
71e3e356 DC |
2680 | for (i = 0; i < igeo->inodes_per_cluster; i++) |
2681 | xfs_ifree_mark_inode_stale(bp, free_ip, inum + i); | |
1da177e4 | 2682 | |
5b3eed75 | 2683 | xfs_trans_stale_inode_buf(tp, bp); |
1da177e4 LT |
2684 | xfs_trans_binval(tp, bp); |
2685 | } | |
2a30f36d | 2686 | return 0; |
1da177e4 LT |
2687 | } |
2688 | ||
2689 | /* | |
2690 | * This is called to return an inode to the inode free list. | |
2691 | * The inode should already be truncated to 0 length and have | |
2692 | * no pages associated with it. This routine also assumes that | |
2693 | * the inode is already a part of the transaction. | |
2694 | * | |
2695 | * The on-disk copy of the inode will have been added to the list | |
2696 | * of unlinked inodes in the AGI. We need to remove the inode from | |
2697 | * that list atomically with respect to freeing it here. | |
2698 | */ | |
2699 | int | |
2700 | xfs_ifree( | |
0e0417f3 BF |
2701 | struct xfs_trans *tp, |
2702 | struct xfs_inode *ip) | |
1da177e4 LT |
2703 | { |
2704 | int error; | |
09b56604 | 2705 | struct xfs_icluster xic = { 0 }; |
1319ebef | 2706 | struct xfs_inode_log_item *iip = ip->i_itemp; |
1da177e4 | 2707 | |
579aa9ca | 2708 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
54d7b5c1 | 2709 | ASSERT(VFS_I(ip)->i_nlink == 0); |
daf83964 | 2710 | ASSERT(ip->i_df.if_nextents == 0); |
c19b3b05 | 2711 | ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode)); |
1da177e4 LT |
2712 | ASSERT(ip->i_d.di_nblocks == 0); |
2713 | ||
2714 | /* | |
2715 | * Pull the on-disk inode from the AGI unlinked list. | |
2716 | */ | |
2717 | error = xfs_iunlink_remove(tp, ip); | |
1baaed8f | 2718 | if (error) |
1da177e4 | 2719 | return error; |
1da177e4 | 2720 | |
0e0417f3 | 2721 | error = xfs_difree(tp, ip->i_ino, &xic); |
1baaed8f | 2722 | if (error) |
1da177e4 | 2723 | return error; |
1baaed8f | 2724 | |
b2c20045 CH |
2725 | /* |
2726 | * Free any local-format data sitting around before we reset the | |
2727 | * data fork to extents format. Note that the attr fork data has | |
2728 | * already been freed by xfs_attr_inactive. | |
2729 | */ | |
f7e67b20 | 2730 | if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) { |
b2c20045 CH |
2731 | kmem_free(ip->i_df.if_u1.if_data); |
2732 | ip->i_df.if_u1.if_data = NULL; | |
2733 | ip->i_df.if_bytes = 0; | |
2734 | } | |
98c4f78d | 2735 | |
c19b3b05 | 2736 | VFS_I(ip)->i_mode = 0; /* mark incore inode as free */ |
1da177e4 | 2737 | ip->i_d.di_flags = 0; |
beaae8cd | 2738 | ip->i_d.di_flags2 = 0; |
1da177e4 LT |
2739 | ip->i_d.di_dmevmask = 0; |
2740 | ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */ | |
f7e67b20 | 2741 | ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS; |
dc1baa71 ES |
2742 | |
2743 | /* Don't attempt to replay owner changes for a deleted inode */ | |
1319ebef DC |
2744 | spin_lock(&iip->ili_lock); |
2745 | iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER); | |
2746 | spin_unlock(&iip->ili_lock); | |
dc1baa71 | 2747 | |
1da177e4 LT |
2748 | /* |
2749 | * Bump the generation count so no one will be confused | |
2750 | * by reincarnations of this inode. | |
2751 | */ | |
9e9a2674 | 2752 | VFS_I(ip)->i_generation++; |
1da177e4 LT |
2753 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
2754 | ||
09b56604 BF |
2755 | if (xic.deleted) |
2756 | error = xfs_ifree_cluster(ip, tp, &xic); | |
1da177e4 | 2757 | |
2a30f36d | 2758 | return error; |
1da177e4 LT |
2759 | } |
2760 | ||
1da177e4 | 2761 | /* |
60ec6783 CH |
2762 | * This is called to unpin an inode. The caller must have the inode locked |
2763 | * in at least shared mode so that the buffer cannot be subsequently pinned | |
2764 | * once someone is waiting for it to be unpinned. | |
1da177e4 | 2765 | */ |
60ec6783 | 2766 | static void |
f392e631 | 2767 | xfs_iunpin( |
60ec6783 | 2768 | struct xfs_inode *ip) |
1da177e4 | 2769 | { |
579aa9ca | 2770 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
1da177e4 | 2771 | |
4aaf15d1 DC |
2772 | trace_xfs_inode_unpin_nowait(ip, _RET_IP_); |
2773 | ||
a3f74ffb | 2774 | /* Give the log a push to start the unpinning I/O */ |
656de4ff | 2775 | xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0, NULL); |
a14a348b | 2776 | |
a3f74ffb | 2777 | } |
1da177e4 | 2778 | |
f392e631 CH |
2779 | static void |
2780 | __xfs_iunpin_wait( | |
2781 | struct xfs_inode *ip) | |
2782 | { | |
2783 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); | |
2784 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); | |
2785 | ||
2786 | xfs_iunpin(ip); | |
2787 | ||
2788 | do { | |
21417136 | 2789 | prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); |
f392e631 CH |
2790 | if (xfs_ipincount(ip)) |
2791 | io_schedule(); | |
2792 | } while (xfs_ipincount(ip)); | |
21417136 | 2793 | finish_wait(wq, &wait.wq_entry); |
f392e631 CH |
2794 | } |
2795 | ||
777df5af | 2796 | void |
a3f74ffb | 2797 | xfs_iunpin_wait( |
60ec6783 | 2798 | struct xfs_inode *ip) |
a3f74ffb | 2799 | { |
f392e631 CH |
2800 | if (xfs_ipincount(ip)) |
2801 | __xfs_iunpin_wait(ip); | |
1da177e4 LT |
2802 | } |
2803 | ||
27320369 DC |
2804 | /* |
2805 | * Removing an inode from the namespace involves removing the directory entry | |
2806 | * and dropping the link count on the inode. Removing the directory entry can | |
2807 | * result in locking an AGF (directory blocks were freed) and removing a link | |
2808 | * count can result in placing the inode on an unlinked list which results in | |
2809 | * locking an AGI. | |
2810 | * | |
2811 | * The big problem here is that we have an ordering constraint on AGF and AGI | |
2812 | * locking - inode allocation locks the AGI, then can allocate a new extent for | |
2813 | * new inodes, locking the AGF after the AGI. Similarly, freeing the inode | |
2814 | * removes the inode from the unlinked list, requiring that we lock the AGI | |
2815 | * first, and then freeing the inode can result in an inode chunk being freed | |
2816 | * and hence freeing disk space requiring that we lock an AGF. | |
2817 | * | |
2818 | * Hence the ordering that is imposed by other parts of the code is AGI before | |
2819 | * AGF. This means we cannot remove the directory entry before we drop the inode | |
2820 | * reference count and put it on the unlinked list as this results in a lock | |
2821 | * order of AGF then AGI, and this can deadlock against inode allocation and | |
2822 | * freeing. Therefore we must drop the link counts before we remove the | |
2823 | * directory entry. | |
2824 | * | |
2825 | * This is still safe from a transactional point of view - it is not until we | |
310a75a3 | 2826 | * get to xfs_defer_finish() that we have the possibility of multiple |
27320369 DC |
2827 | * transactions in this operation. Hence as long as we remove the directory |
2828 | * entry and drop the link count in the first transaction of the remove | |
2829 | * operation, there are no transactional constraints on the ordering here. | |
2830 | */ | |
c24b5dfa DC |
2831 | int |
2832 | xfs_remove( | |
2833 | xfs_inode_t *dp, | |
2834 | struct xfs_name *name, | |
2835 | xfs_inode_t *ip) | |
2836 | { | |
2837 | xfs_mount_t *mp = dp->i_mount; | |
2838 | xfs_trans_t *tp = NULL; | |
c19b3b05 | 2839 | int is_dir = S_ISDIR(VFS_I(ip)->i_mode); |
c24b5dfa | 2840 | int error = 0; |
c24b5dfa | 2841 | uint resblks; |
c24b5dfa DC |
2842 | |
2843 | trace_xfs_remove(dp, name); | |
2844 | ||
2845 | if (XFS_FORCED_SHUTDOWN(mp)) | |
2451337d | 2846 | return -EIO; |
c24b5dfa | 2847 | |
c14cfcca | 2848 | error = xfs_qm_dqattach(dp); |
c24b5dfa DC |
2849 | if (error) |
2850 | goto std_return; | |
2851 | ||
c14cfcca | 2852 | error = xfs_qm_dqattach(ip); |
c24b5dfa DC |
2853 | if (error) |
2854 | goto std_return; | |
2855 | ||
c24b5dfa DC |
2856 | /* |
2857 | * We try to get the real space reservation first, | |
2858 | * allowing for directory btree deletion(s) implying | |
2859 | * possible bmap insert(s). If we can't get the space | |
2860 | * reservation then we use 0 instead, and avoid the bmap | |
2861 | * btree insert(s) in the directory code by, if the bmap | |
2862 | * insert tries to happen, instead trimming the LAST | |
2863 | * block from the directory. | |
2864 | */ | |
2865 | resblks = XFS_REMOVE_SPACE_RES(mp); | |
253f4911 | 2866 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp); |
2451337d | 2867 | if (error == -ENOSPC) { |
c24b5dfa | 2868 | resblks = 0; |
253f4911 CH |
2869 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0, |
2870 | &tp); | |
c24b5dfa DC |
2871 | } |
2872 | if (error) { | |
2451337d | 2873 | ASSERT(error != -ENOSPC); |
253f4911 | 2874 | goto std_return; |
c24b5dfa DC |
2875 | } |
2876 | ||
7c2d238a | 2877 | xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL); |
c24b5dfa | 2878 | |
65523218 | 2879 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
c24b5dfa DC |
2880 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
2881 | ||
2882 | /* | |
2883 | * If we're removing a directory perform some additional validation. | |
2884 | */ | |
2885 | if (is_dir) { | |
54d7b5c1 DC |
2886 | ASSERT(VFS_I(ip)->i_nlink >= 2); |
2887 | if (VFS_I(ip)->i_nlink != 2) { | |
2451337d | 2888 | error = -ENOTEMPTY; |
c24b5dfa DC |
2889 | goto out_trans_cancel; |
2890 | } | |
2891 | if (!xfs_dir_isempty(ip)) { | |
2451337d | 2892 | error = -ENOTEMPTY; |
c24b5dfa DC |
2893 | goto out_trans_cancel; |
2894 | } | |
c24b5dfa | 2895 | |
27320369 | 2896 | /* Drop the link from ip's "..". */ |
c24b5dfa DC |
2897 | error = xfs_droplink(tp, dp); |
2898 | if (error) | |
27320369 | 2899 | goto out_trans_cancel; |
c24b5dfa | 2900 | |
27320369 | 2901 | /* Drop the "." link from ip to self. */ |
c24b5dfa DC |
2902 | error = xfs_droplink(tp, ip); |
2903 | if (error) | |
27320369 | 2904 | goto out_trans_cancel; |
c24b5dfa DC |
2905 | } else { |
2906 | /* | |
2907 | * When removing a non-directory we need to log the parent | |
2908 | * inode here. For a directory this is done implicitly | |
2909 | * by the xfs_droplink call for the ".." entry. | |
2910 | */ | |
2911 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
2912 | } | |
27320369 | 2913 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
c24b5dfa | 2914 | |
27320369 | 2915 | /* Drop the link from dp to ip. */ |
c24b5dfa DC |
2916 | error = xfs_droplink(tp, ip); |
2917 | if (error) | |
27320369 | 2918 | goto out_trans_cancel; |
c24b5dfa | 2919 | |
381eee69 | 2920 | error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks); |
27320369 | 2921 | if (error) { |
2451337d | 2922 | ASSERT(error != -ENOENT); |
c8eac49e | 2923 | goto out_trans_cancel; |
27320369 DC |
2924 | } |
2925 | ||
c24b5dfa DC |
2926 | /* |
2927 | * If this is a synchronous mount, make sure that the | |
2928 | * remove transaction goes to disk before returning to | |
2929 | * the user. | |
2930 | */ | |
2931 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) | |
2932 | xfs_trans_set_sync(tp); | |
2933 | ||
70393313 | 2934 | error = xfs_trans_commit(tp); |
c24b5dfa DC |
2935 | if (error) |
2936 | goto std_return; | |
2937 | ||
2cd2ef6a | 2938 | if (is_dir && xfs_inode_is_filestream(ip)) |
c24b5dfa DC |
2939 | xfs_filestream_deassociate(ip); |
2940 | ||
2941 | return 0; | |
2942 | ||
c24b5dfa | 2943 | out_trans_cancel: |
4906e215 | 2944 | xfs_trans_cancel(tp); |
c24b5dfa DC |
2945 | std_return: |
2946 | return error; | |
2947 | } | |
2948 | ||
f6bba201 DC |
2949 | /* |
2950 | * Enter all inodes for a rename transaction into a sorted array. | |
2951 | */ | |
95afcf5c | 2952 | #define __XFS_SORT_INODES 5 |
f6bba201 DC |
2953 | STATIC void |
2954 | xfs_sort_for_rename( | |
95afcf5c DC |
2955 | struct xfs_inode *dp1, /* in: old (source) directory inode */ |
2956 | struct xfs_inode *dp2, /* in: new (target) directory inode */ | |
2957 | struct xfs_inode *ip1, /* in: inode of old entry */ | |
2958 | struct xfs_inode *ip2, /* in: inode of new entry */ | |
2959 | struct xfs_inode *wip, /* in: whiteout inode */ | |
2960 | struct xfs_inode **i_tab,/* out: sorted array of inodes */ | |
2961 | int *num_inodes) /* in/out: inodes in array */ | |
f6bba201 | 2962 | { |
f6bba201 DC |
2963 | int i, j; |
2964 | ||
95afcf5c DC |
2965 | ASSERT(*num_inodes == __XFS_SORT_INODES); |
2966 | memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *)); | |
2967 | ||
f6bba201 DC |
2968 | /* |
2969 | * i_tab contains a list of pointers to inodes. We initialize | |
2970 | * the table here & we'll sort it. We will then use it to | |
2971 | * order the acquisition of the inode locks. | |
2972 | * | |
2973 | * Note that the table may contain duplicates. e.g., dp1 == dp2. | |
2974 | */ | |
95afcf5c DC |
2975 | i = 0; |
2976 | i_tab[i++] = dp1; | |
2977 | i_tab[i++] = dp2; | |
2978 | i_tab[i++] = ip1; | |
2979 | if (ip2) | |
2980 | i_tab[i++] = ip2; | |
2981 | if (wip) | |
2982 | i_tab[i++] = wip; | |
2983 | *num_inodes = i; | |
f6bba201 DC |
2984 | |
2985 | /* | |
2986 | * Sort the elements via bubble sort. (Remember, there are at | |
95afcf5c | 2987 | * most 5 elements to sort, so this is adequate.) |
f6bba201 DC |
2988 | */ |
2989 | for (i = 0; i < *num_inodes; i++) { | |
2990 | for (j = 1; j < *num_inodes; j++) { | |
2991 | if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) { | |
95afcf5c | 2992 | struct xfs_inode *temp = i_tab[j]; |
f6bba201 DC |
2993 | i_tab[j] = i_tab[j-1]; |
2994 | i_tab[j-1] = temp; | |
2995 | } | |
2996 | } | |
2997 | } | |
2998 | } | |
2999 | ||
310606b0 DC |
3000 | static int |
3001 | xfs_finish_rename( | |
c9cfdb38 | 3002 | struct xfs_trans *tp) |
310606b0 | 3003 | { |
310606b0 DC |
3004 | /* |
3005 | * If this is a synchronous mount, make sure that the rename transaction | |
3006 | * goes to disk before returning to the user. | |
3007 | */ | |
3008 | if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) | |
3009 | xfs_trans_set_sync(tp); | |
3010 | ||
70393313 | 3011 | return xfs_trans_commit(tp); |
310606b0 DC |
3012 | } |
3013 | ||
d31a1825 CM |
3014 | /* |
3015 | * xfs_cross_rename() | |
3016 | * | |
3017 | * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall | |
3018 | */ | |
3019 | STATIC int | |
3020 | xfs_cross_rename( | |
3021 | struct xfs_trans *tp, | |
3022 | struct xfs_inode *dp1, | |
3023 | struct xfs_name *name1, | |
3024 | struct xfs_inode *ip1, | |
3025 | struct xfs_inode *dp2, | |
3026 | struct xfs_name *name2, | |
3027 | struct xfs_inode *ip2, | |
d31a1825 CM |
3028 | int spaceres) |
3029 | { | |
3030 | int error = 0; | |
3031 | int ip1_flags = 0; | |
3032 | int ip2_flags = 0; | |
3033 | int dp2_flags = 0; | |
3034 | ||
3035 | /* Swap inode number for dirent in first parent */ | |
381eee69 | 3036 | error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres); |
d31a1825 | 3037 | if (error) |
eeacd321 | 3038 | goto out_trans_abort; |
d31a1825 CM |
3039 | |
3040 | /* Swap inode number for dirent in second parent */ | |
381eee69 | 3041 | error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres); |
d31a1825 | 3042 | if (error) |
eeacd321 | 3043 | goto out_trans_abort; |
d31a1825 CM |
3044 | |
3045 | /* | |
3046 | * If we're renaming one or more directories across different parents, | |
3047 | * update the respective ".." entries (and link counts) to match the new | |
3048 | * parents. | |
3049 | */ | |
3050 | if (dp1 != dp2) { | |
3051 | dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; | |
3052 | ||
c19b3b05 | 3053 | if (S_ISDIR(VFS_I(ip2)->i_mode)) { |
d31a1825 | 3054 | error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot, |
381eee69 | 3055 | dp1->i_ino, spaceres); |
d31a1825 | 3056 | if (error) |
eeacd321 | 3057 | goto out_trans_abort; |
d31a1825 CM |
3058 | |
3059 | /* transfer ip2 ".." reference to dp1 */ | |
c19b3b05 | 3060 | if (!S_ISDIR(VFS_I(ip1)->i_mode)) { |
d31a1825 CM |
3061 | error = xfs_droplink(tp, dp2); |
3062 | if (error) | |
eeacd321 | 3063 | goto out_trans_abort; |
91083269 | 3064 | xfs_bumplink(tp, dp1); |
d31a1825 CM |
3065 | } |
3066 | ||
3067 | /* | |
3068 | * Although ip1 isn't changed here, userspace needs | |
3069 | * to be warned about the change, so that applications | |
3070 | * relying on it (like backup ones), will properly | |
3071 | * notify the change | |
3072 | */ | |
3073 | ip1_flags |= XFS_ICHGTIME_CHG; | |
3074 | ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; | |
3075 | } | |
3076 | ||
c19b3b05 | 3077 | if (S_ISDIR(VFS_I(ip1)->i_mode)) { |
d31a1825 | 3078 | error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot, |
381eee69 | 3079 | dp2->i_ino, spaceres); |
d31a1825 | 3080 | if (error) |
eeacd321 | 3081 | goto out_trans_abort; |
d31a1825 CM |
3082 | |
3083 | /* transfer ip1 ".." reference to dp2 */ | |
c19b3b05 | 3084 | if (!S_ISDIR(VFS_I(ip2)->i_mode)) { |
d31a1825 CM |
3085 | error = xfs_droplink(tp, dp1); |
3086 | if (error) | |
eeacd321 | 3087 | goto out_trans_abort; |
91083269 | 3088 | xfs_bumplink(tp, dp2); |
d31a1825 CM |
3089 | } |
3090 | ||
3091 | /* | |
3092 | * Although ip2 isn't changed here, userspace needs | |
3093 | * to be warned about the change, so that applications | |
3094 | * relying on it (like backup ones), will properly | |
3095 | * notify the change | |
3096 | */ | |
3097 | ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; | |
3098 | ip2_flags |= XFS_ICHGTIME_CHG; | |
3099 | } | |
3100 | } | |
3101 | ||
3102 | if (ip1_flags) { | |
3103 | xfs_trans_ichgtime(tp, ip1, ip1_flags); | |
3104 | xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE); | |
3105 | } | |
3106 | if (ip2_flags) { | |
3107 | xfs_trans_ichgtime(tp, ip2, ip2_flags); | |
3108 | xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE); | |
3109 | } | |
3110 | if (dp2_flags) { | |
3111 | xfs_trans_ichgtime(tp, dp2, dp2_flags); | |
3112 | xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE); | |
3113 | } | |
3114 | xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
3115 | xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE); | |
c9cfdb38 | 3116 | return xfs_finish_rename(tp); |
eeacd321 DC |
3117 | |
3118 | out_trans_abort: | |
4906e215 | 3119 | xfs_trans_cancel(tp); |
d31a1825 CM |
3120 | return error; |
3121 | } | |
3122 | ||
7dcf5c3e DC |
3123 | /* |
3124 | * xfs_rename_alloc_whiteout() | |
3125 | * | |
3126 | * Return a referenced, unlinked, unlocked inode that that can be used as a | |
3127 | * whiteout in a rename transaction. We use a tmpfile inode here so that if we | |
3128 | * crash between allocating the inode and linking it into the rename transaction | |
3129 | * recovery will free the inode and we won't leak it. | |
3130 | */ | |
3131 | static int | |
3132 | xfs_rename_alloc_whiteout( | |
3133 | struct xfs_inode *dp, | |
3134 | struct xfs_inode **wip) | |
3135 | { | |
3136 | struct xfs_inode *tmpfile; | |
3137 | int error; | |
3138 | ||
a1f69417 | 3139 | error = xfs_create_tmpfile(dp, S_IFCHR | WHITEOUT_MODE, &tmpfile); |
7dcf5c3e DC |
3140 | if (error) |
3141 | return error; | |
3142 | ||
22419ac9 BF |
3143 | /* |
3144 | * Prepare the tmpfile inode as if it were created through the VFS. | |
c4a6bf7f DW |
3145 | * Complete the inode setup and flag it as linkable. nlink is already |
3146 | * zero, so we can skip the drop_nlink. | |
22419ac9 | 3147 | */ |
2b3d1d41 | 3148 | xfs_setup_iops(tmpfile); |
7dcf5c3e DC |
3149 | xfs_finish_inode_setup(tmpfile); |
3150 | VFS_I(tmpfile)->i_state |= I_LINKABLE; | |
3151 | ||
3152 | *wip = tmpfile; | |
3153 | return 0; | |
3154 | } | |
3155 | ||
f6bba201 DC |
3156 | /* |
3157 | * xfs_rename | |
3158 | */ | |
3159 | int | |
3160 | xfs_rename( | |
7dcf5c3e DC |
3161 | struct xfs_inode *src_dp, |
3162 | struct xfs_name *src_name, | |
3163 | struct xfs_inode *src_ip, | |
3164 | struct xfs_inode *target_dp, | |
3165 | struct xfs_name *target_name, | |
3166 | struct xfs_inode *target_ip, | |
3167 | unsigned int flags) | |
f6bba201 | 3168 | { |
7dcf5c3e DC |
3169 | struct xfs_mount *mp = src_dp->i_mount; |
3170 | struct xfs_trans *tp; | |
7dcf5c3e DC |
3171 | struct xfs_inode *wip = NULL; /* whiteout inode */ |
3172 | struct xfs_inode *inodes[__XFS_SORT_INODES]; | |
93597ae8 | 3173 | struct xfs_buf *agibp; |
7dcf5c3e | 3174 | int num_inodes = __XFS_SORT_INODES; |
2b93681f | 3175 | bool new_parent = (src_dp != target_dp); |
c19b3b05 | 3176 | bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode); |
7dcf5c3e DC |
3177 | int spaceres; |
3178 | int error; | |
f6bba201 DC |
3179 | |
3180 | trace_xfs_rename(src_dp, target_dp, src_name, target_name); | |
3181 | ||
eeacd321 DC |
3182 | if ((flags & RENAME_EXCHANGE) && !target_ip) |
3183 | return -EINVAL; | |
3184 | ||
7dcf5c3e DC |
3185 | /* |
3186 | * If we are doing a whiteout operation, allocate the whiteout inode | |
3187 | * we will be placing at the target and ensure the type is set | |
3188 | * appropriately. | |
3189 | */ | |
3190 | if (flags & RENAME_WHITEOUT) { | |
3191 | ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE))); | |
3192 | error = xfs_rename_alloc_whiteout(target_dp, &wip); | |
3193 | if (error) | |
3194 | return error; | |
3195 | ||
3196 | /* setup target dirent info as whiteout */ | |
3197 | src_name->type = XFS_DIR3_FT_CHRDEV; | |
3198 | } | |
f6bba201 | 3199 | |
7dcf5c3e | 3200 | xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip, |
f6bba201 DC |
3201 | inodes, &num_inodes); |
3202 | ||
f6bba201 | 3203 | spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len); |
253f4911 | 3204 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp); |
2451337d | 3205 | if (error == -ENOSPC) { |
f6bba201 | 3206 | spaceres = 0; |
253f4911 CH |
3207 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0, |
3208 | &tp); | |
f6bba201 | 3209 | } |
445883e8 | 3210 | if (error) |
253f4911 | 3211 | goto out_release_wip; |
f6bba201 DC |
3212 | |
3213 | /* | |
3214 | * Attach the dquots to the inodes | |
3215 | */ | |
3216 | error = xfs_qm_vop_rename_dqattach(inodes); | |
445883e8 DC |
3217 | if (error) |
3218 | goto out_trans_cancel; | |
f6bba201 DC |
3219 | |
3220 | /* | |
3221 | * Lock all the participating inodes. Depending upon whether | |
3222 | * the target_name exists in the target directory, and | |
3223 | * whether the target directory is the same as the source | |
3224 | * directory, we can lock from 2 to 4 inodes. | |
3225 | */ | |
3226 | xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL); | |
3227 | ||
3228 | /* | |
3229 | * Join all the inodes to the transaction. From this point on, | |
3230 | * we can rely on either trans_commit or trans_cancel to unlock | |
3231 | * them. | |
3232 | */ | |
65523218 | 3233 | xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL); |
f6bba201 | 3234 | if (new_parent) |
65523218 | 3235 | xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL); |
f6bba201 DC |
3236 | xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL); |
3237 | if (target_ip) | |
3238 | xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL); | |
7dcf5c3e DC |
3239 | if (wip) |
3240 | xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL); | |
f6bba201 DC |
3241 | |
3242 | /* | |
3243 | * If we are using project inheritance, we only allow renames | |
3244 | * into our tree when the project IDs are the same; else the | |
3245 | * tree quota mechanism would be circumvented. | |
3246 | */ | |
3247 | if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && | |
de7a866f | 3248 | target_dp->i_d.di_projid != src_ip->i_d.di_projid)) { |
2451337d | 3249 | error = -EXDEV; |
445883e8 | 3250 | goto out_trans_cancel; |
f6bba201 DC |
3251 | } |
3252 | ||
eeacd321 DC |
3253 | /* RENAME_EXCHANGE is unique from here on. */ |
3254 | if (flags & RENAME_EXCHANGE) | |
3255 | return xfs_cross_rename(tp, src_dp, src_name, src_ip, | |
3256 | target_dp, target_name, target_ip, | |
f16dea54 | 3257 | spaceres); |
d31a1825 | 3258 | |
f6bba201 | 3259 | /* |
bc56ad8c | 3260 | * Check for expected errors before we dirty the transaction |
3261 | * so we can return an error without a transaction abort. | |
f6bba201 DC |
3262 | */ |
3263 | if (target_ip == NULL) { | |
3264 | /* | |
3265 | * If there's no space reservation, check the entry will | |
3266 | * fit before actually inserting it. | |
3267 | */ | |
94f3cad5 ES |
3268 | if (!spaceres) { |
3269 | error = xfs_dir_canenter(tp, target_dp, target_name); | |
3270 | if (error) | |
445883e8 | 3271 | goto out_trans_cancel; |
94f3cad5 | 3272 | } |
bc56ad8c | 3273 | } else { |
3274 | /* | |
3275 | * If target exists and it's a directory, check that whether | |
3276 | * it can be destroyed. | |
3277 | */ | |
3278 | if (S_ISDIR(VFS_I(target_ip)->i_mode) && | |
3279 | (!xfs_dir_isempty(target_ip) || | |
3280 | (VFS_I(target_ip)->i_nlink > 2))) { | |
3281 | error = -EEXIST; | |
3282 | goto out_trans_cancel; | |
3283 | } | |
3284 | } | |
3285 | ||
3286 | /* | |
3287 | * Directory entry creation below may acquire the AGF. Remove | |
3288 | * the whiteout from the unlinked list first to preserve correct | |
3289 | * AGI/AGF locking order. This dirties the transaction so failures | |
3290 | * after this point will abort and log recovery will clean up the | |
3291 | * mess. | |
3292 | * | |
3293 | * For whiteouts, we need to bump the link count on the whiteout | |
3294 | * inode. After this point, we have a real link, clear the tmpfile | |
3295 | * state flag from the inode so it doesn't accidentally get misused | |
3296 | * in future. | |
3297 | */ | |
3298 | if (wip) { | |
3299 | ASSERT(VFS_I(wip)->i_nlink == 0); | |
3300 | error = xfs_iunlink_remove(tp, wip); | |
3301 | if (error) | |
3302 | goto out_trans_cancel; | |
3303 | ||
3304 | xfs_bumplink(tp, wip); | |
bc56ad8c | 3305 | VFS_I(wip)->i_state &= ~I_LINKABLE; |
3306 | } | |
3307 | ||
3308 | /* | |
3309 | * Set up the target. | |
3310 | */ | |
3311 | if (target_ip == NULL) { | |
f6bba201 DC |
3312 | /* |
3313 | * If target does not exist and the rename crosses | |
3314 | * directories, adjust the target directory link count | |
3315 | * to account for the ".." reference from the new entry. | |
3316 | */ | |
3317 | error = xfs_dir_createname(tp, target_dp, target_name, | |
381eee69 | 3318 | src_ip->i_ino, spaceres); |
f6bba201 | 3319 | if (error) |
c8eac49e | 3320 | goto out_trans_cancel; |
f6bba201 DC |
3321 | |
3322 | xfs_trans_ichgtime(tp, target_dp, | |
3323 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
3324 | ||
3325 | if (new_parent && src_is_directory) { | |
91083269 | 3326 | xfs_bumplink(tp, target_dp); |
f6bba201 DC |
3327 | } |
3328 | } else { /* target_ip != NULL */ | |
f6bba201 DC |
3329 | /* |
3330 | * Link the source inode under the target name. | |
3331 | * If the source inode is a directory and we are moving | |
3332 | * it across directories, its ".." entry will be | |
3333 | * inconsistent until we replace that down below. | |
3334 | * | |
3335 | * In case there is already an entry with the same | |
3336 | * name at the destination directory, remove it first. | |
3337 | */ | |
93597ae8 | 3338 | |
3339 | /* | |
3340 | * Check whether the replace operation will need to allocate | |
3341 | * blocks. This happens when the shortform directory lacks | |
3342 | * space and we have to convert it to a block format directory. | |
3343 | * When more blocks are necessary, we must lock the AGI first | |
3344 | * to preserve locking order (AGI -> AGF). | |
3345 | */ | |
3346 | if (xfs_dir2_sf_replace_needblock(target_dp, src_ip->i_ino)) { | |
3347 | error = xfs_read_agi(mp, tp, | |
3348 | XFS_INO_TO_AGNO(mp, target_ip->i_ino), | |
3349 | &agibp); | |
3350 | if (error) | |
3351 | goto out_trans_cancel; | |
3352 | } | |
3353 | ||
f6bba201 | 3354 | error = xfs_dir_replace(tp, target_dp, target_name, |
381eee69 | 3355 | src_ip->i_ino, spaceres); |
f6bba201 | 3356 | if (error) |
c8eac49e | 3357 | goto out_trans_cancel; |
f6bba201 DC |
3358 | |
3359 | xfs_trans_ichgtime(tp, target_dp, | |
3360 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
3361 | ||
3362 | /* | |
3363 | * Decrement the link count on the target since the target | |
3364 | * dir no longer points to it. | |
3365 | */ | |
3366 | error = xfs_droplink(tp, target_ip); | |
3367 | if (error) | |
c8eac49e | 3368 | goto out_trans_cancel; |
f6bba201 DC |
3369 | |
3370 | if (src_is_directory) { | |
3371 | /* | |
3372 | * Drop the link from the old "." entry. | |
3373 | */ | |
3374 | error = xfs_droplink(tp, target_ip); | |
3375 | if (error) | |
c8eac49e | 3376 | goto out_trans_cancel; |
f6bba201 DC |
3377 | } |
3378 | } /* target_ip != NULL */ | |
3379 | ||
3380 | /* | |
3381 | * Remove the source. | |
3382 | */ | |
3383 | if (new_parent && src_is_directory) { | |
3384 | /* | |
3385 | * Rewrite the ".." entry to point to the new | |
3386 | * directory. | |
3387 | */ | |
3388 | error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, | |
381eee69 | 3389 | target_dp->i_ino, spaceres); |
2451337d | 3390 | ASSERT(error != -EEXIST); |
f6bba201 | 3391 | if (error) |
c8eac49e | 3392 | goto out_trans_cancel; |
f6bba201 DC |
3393 | } |
3394 | ||
3395 | /* | |
3396 | * We always want to hit the ctime on the source inode. | |
3397 | * | |
3398 | * This isn't strictly required by the standards since the source | |
3399 | * inode isn't really being changed, but old unix file systems did | |
3400 | * it and some incremental backup programs won't work without it. | |
3401 | */ | |
3402 | xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); | |
3403 | xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); | |
3404 | ||
3405 | /* | |
3406 | * Adjust the link count on src_dp. This is necessary when | |
3407 | * renaming a directory, either within one parent when | |
3408 | * the target existed, or across two parent directories. | |
3409 | */ | |
3410 | if (src_is_directory && (new_parent || target_ip != NULL)) { | |
3411 | ||
3412 | /* | |
3413 | * Decrement link count on src_directory since the | |
3414 | * entry that's moved no longer points to it. | |
3415 | */ | |
3416 | error = xfs_droplink(tp, src_dp); | |
3417 | if (error) | |
c8eac49e | 3418 | goto out_trans_cancel; |
f6bba201 DC |
3419 | } |
3420 | ||
7dcf5c3e DC |
3421 | /* |
3422 | * For whiteouts, we only need to update the source dirent with the | |
3423 | * inode number of the whiteout inode rather than removing it | |
3424 | * altogether. | |
3425 | */ | |
3426 | if (wip) { | |
3427 | error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino, | |
381eee69 | 3428 | spaceres); |
7dcf5c3e DC |
3429 | } else |
3430 | error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, | |
381eee69 | 3431 | spaceres); |
f6bba201 | 3432 | if (error) |
c8eac49e | 3433 | goto out_trans_cancel; |
f6bba201 | 3434 | |
f6bba201 DC |
3435 | xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
3436 | xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); | |
3437 | if (new_parent) | |
3438 | xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); | |
f6bba201 | 3439 | |
c9cfdb38 | 3440 | error = xfs_finish_rename(tp); |
7dcf5c3e | 3441 | if (wip) |
44a8736b | 3442 | xfs_irele(wip); |
7dcf5c3e | 3443 | return error; |
f6bba201 | 3444 | |
445883e8 | 3445 | out_trans_cancel: |
4906e215 | 3446 | xfs_trans_cancel(tp); |
253f4911 | 3447 | out_release_wip: |
7dcf5c3e | 3448 | if (wip) |
44a8736b | 3449 | xfs_irele(wip); |
f6bba201 DC |
3450 | return error; |
3451 | } | |
3452 | ||
5c4d97d0 DC |
3453 | STATIC int |
3454 | xfs_iflush_cluster( | |
19429363 DC |
3455 | struct xfs_inode *ip, |
3456 | struct xfs_buf *bp) | |
1da177e4 | 3457 | { |
19429363 | 3458 | struct xfs_mount *mp = ip->i_mount; |
5c4d97d0 DC |
3459 | struct xfs_perag *pag; |
3460 | unsigned long first_index, mask; | |
19429363 DC |
3461 | int cilist_size; |
3462 | struct xfs_inode **cilist; | |
3463 | struct xfs_inode *cip; | |
ef325959 | 3464 | struct xfs_ino_geometry *igeo = M_IGEO(mp); |
f2019299 | 3465 | int error = 0; |
5c4d97d0 DC |
3466 | int nr_found; |
3467 | int clcount = 0; | |
1da177e4 | 3468 | int i; |
1da177e4 | 3469 | |
5c4d97d0 | 3470 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
1da177e4 | 3471 | |
4b4d98cc | 3472 | cilist_size = igeo->inodes_per_cluster * sizeof(struct xfs_inode *); |
19429363 DC |
3473 | cilist = kmem_alloc(cilist_size, KM_MAYFAIL|KM_NOFS); |
3474 | if (!cilist) | |
5c4d97d0 | 3475 | goto out_put; |
1da177e4 | 3476 | |
4b4d98cc | 3477 | mask = ~(igeo->inodes_per_cluster - 1); |
5c4d97d0 DC |
3478 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask; |
3479 | rcu_read_lock(); | |
3480 | /* really need a gang lookup range call here */ | |
19429363 | 3481 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)cilist, |
4b4d98cc | 3482 | first_index, igeo->inodes_per_cluster); |
5c4d97d0 DC |
3483 | if (nr_found == 0) |
3484 | goto out_free; | |
3485 | ||
3486 | for (i = 0; i < nr_found; i++) { | |
19429363 DC |
3487 | cip = cilist[i]; |
3488 | if (cip == ip) | |
bad55843 | 3489 | continue; |
1a3e8f3d DC |
3490 | |
3491 | /* | |
3492 | * because this is an RCU protected lookup, we could find a | |
3493 | * recently freed or even reallocated inode during the lookup. | |
3494 | * We need to check under the i_flags_lock for a valid inode | |
3495 | * here. Skip it if it is not valid or the wrong inode. | |
3496 | */ | |
19429363 DC |
3497 | spin_lock(&cip->i_flags_lock); |
3498 | if (!cip->i_ino || | |
3499 | __xfs_iflags_test(cip, XFS_ISTALE)) { | |
3500 | spin_unlock(&cip->i_flags_lock); | |
1a3e8f3d DC |
3501 | continue; |
3502 | } | |
5a90e53e DC |
3503 | |
3504 | /* | |
3505 | * Once we fall off the end of the cluster, no point checking | |
3506 | * any more inodes in the list because they will also all be | |
3507 | * outside the cluster. | |
3508 | */ | |
19429363 DC |
3509 | if ((XFS_INO_TO_AGINO(mp, cip->i_ino) & mask) != first_index) { |
3510 | spin_unlock(&cip->i_flags_lock); | |
5a90e53e DC |
3511 | break; |
3512 | } | |
19429363 | 3513 | spin_unlock(&cip->i_flags_lock); |
1a3e8f3d | 3514 | |
bad55843 DC |
3515 | /* |
3516 | * Do an un-protected check to see if the inode is dirty and | |
3517 | * is a candidate for flushing. These checks will be repeated | |
3518 | * later after the appropriate locks are acquired. | |
3519 | */ | |
19429363 | 3520 | if (xfs_inode_clean(cip) && xfs_ipincount(cip) == 0) |
bad55843 | 3521 | continue; |
bad55843 DC |
3522 | |
3523 | /* | |
3524 | * Try to get locks. If any are unavailable or it is pinned, | |
3525 | * then this inode cannot be flushed and is skipped. | |
3526 | */ | |
3527 | ||
19429363 | 3528 | if (!xfs_ilock_nowait(cip, XFS_ILOCK_SHARED)) |
bad55843 | 3529 | continue; |
19429363 DC |
3530 | if (!xfs_iflock_nowait(cip)) { |
3531 | xfs_iunlock(cip, XFS_ILOCK_SHARED); | |
bad55843 DC |
3532 | continue; |
3533 | } | |
19429363 DC |
3534 | if (xfs_ipincount(cip)) { |
3535 | xfs_ifunlock(cip); | |
3536 | xfs_iunlock(cip, XFS_ILOCK_SHARED); | |
bad55843 DC |
3537 | continue; |
3538 | } | |
3539 | ||
8a17d7dd DC |
3540 | |
3541 | /* | |
3542 | * Check the inode number again, just to be certain we are not | |
3543 | * racing with freeing in xfs_reclaim_inode(). See the comments | |
3544 | * in that function for more information as to why the initial | |
3545 | * check is not sufficient. | |
3546 | */ | |
19429363 DC |
3547 | if (!cip->i_ino) { |
3548 | xfs_ifunlock(cip); | |
3549 | xfs_iunlock(cip, XFS_ILOCK_SHARED); | |
bad55843 DC |
3550 | continue; |
3551 | } | |
3552 | ||
3553 | /* | |
3554 | * arriving here means that this inode can be flushed. First | |
3555 | * re-check that it's dirty before flushing. | |
3556 | */ | |
19429363 | 3557 | if (!xfs_inode_clean(cip)) { |
19429363 | 3558 | error = xfs_iflush_int(cip, bp); |
bad55843 | 3559 | if (error) { |
19429363 | 3560 | xfs_iunlock(cip, XFS_ILOCK_SHARED); |
f2019299 | 3561 | goto out_free; |
bad55843 DC |
3562 | } |
3563 | clcount++; | |
3564 | } else { | |
19429363 | 3565 | xfs_ifunlock(cip); |
bad55843 | 3566 | } |
19429363 | 3567 | xfs_iunlock(cip, XFS_ILOCK_SHARED); |
bad55843 DC |
3568 | } |
3569 | ||
3570 | if (clcount) { | |
ff6d6af2 BD |
3571 | XFS_STATS_INC(mp, xs_icluster_flushcnt); |
3572 | XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount); | |
bad55843 DC |
3573 | } |
3574 | ||
3575 | out_free: | |
1a3e8f3d | 3576 | rcu_read_unlock(); |
19429363 | 3577 | kmem_free(cilist); |
44b56e0a DC |
3578 | out_put: |
3579 | xfs_perag_put(pag); | |
f2019299 | 3580 | return error; |
bad55843 DC |
3581 | } |
3582 | ||
1da177e4 | 3583 | /* |
4c46819a CH |
3584 | * Flush dirty inode metadata into the backing buffer. |
3585 | * | |
3586 | * The caller must have the inode lock and the inode flush lock held. The | |
3587 | * inode lock will still be held upon return to the caller, and the inode | |
3588 | * flush lock will be released after the inode has reached the disk. | |
3589 | * | |
3590 | * The caller must write out the buffer returned in *bpp and release it. | |
1da177e4 LT |
3591 | */ |
3592 | int | |
3593 | xfs_iflush( | |
4c46819a CH |
3594 | struct xfs_inode *ip, |
3595 | struct xfs_buf **bpp) | |
1da177e4 | 3596 | { |
4c46819a | 3597 | struct xfs_mount *mp = ip->i_mount; |
b1438f47 | 3598 | struct xfs_buf *bp = NULL; |
4c46819a | 3599 | struct xfs_dinode *dip; |
1da177e4 | 3600 | int error; |
1da177e4 | 3601 | |
ff6d6af2 | 3602 | XFS_STATS_INC(mp, xs_iflush_count); |
1da177e4 | 3603 | |
579aa9ca | 3604 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
474fce06 | 3605 | ASSERT(xfs_isiflocked(ip)); |
f7e67b20 | 3606 | ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE || |
daf83964 | 3607 | ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
1da177e4 | 3608 | |
4c46819a | 3609 | *bpp = NULL; |
1da177e4 | 3610 | |
1da177e4 LT |
3611 | xfs_iunpin_wait(ip); |
3612 | ||
4b6a4688 DC |
3613 | /* |
3614 | * For stale inodes we cannot rely on the backing buffer remaining | |
3615 | * stale in cache for the remaining life of the stale inode and so | |
475ee413 | 3616 | * xfs_imap_to_bp() below may give us a buffer that no longer contains |
4b6a4688 DC |
3617 | * inodes below. We have to check this after ensuring the inode is |
3618 | * unpinned so that it is safe to reclaim the stale inode after the | |
3619 | * flush call. | |
3620 | */ | |
3621 | if (xfs_iflags_test(ip, XFS_ISTALE)) { | |
3622 | xfs_ifunlock(ip); | |
3623 | return 0; | |
3624 | } | |
3625 | ||
a3f74ffb | 3626 | /* |
b1438f47 | 3627 | * Get the buffer containing the on-disk inode. We are doing a try-lock |
f2019299 BF |
3628 | * operation here, so we may get an EAGAIN error. In that case, return |
3629 | * leaving the inode dirty. | |
b1438f47 DC |
3630 | * |
3631 | * If we get any other error, we effectively have a corruption situation | |
f2019299 | 3632 | * and we cannot flush the inode. Abort the flush and shut down. |
a3f74ffb | 3633 | */ |
c1995079 | 3634 | error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK); |
b1438f47 | 3635 | if (error == -EAGAIN) { |
a3f74ffb DC |
3636 | xfs_ifunlock(ip); |
3637 | return error; | |
3638 | } | |
b1438f47 | 3639 | if (error) |
f2019299 | 3640 | goto abort; |
1da177e4 | 3641 | |
a3f74ffb DC |
3642 | /* |
3643 | * If the buffer is pinned then push on the log now so we won't | |
3644 | * get stuck waiting in the write for too long. | |
3645 | */ | |
811e64c7 | 3646 | if (xfs_buf_ispinned(bp)) |
a14a348b | 3647 | xfs_log_force(mp, 0); |
a3f74ffb | 3648 | |
1da177e4 | 3649 | /* |
f2019299 BF |
3650 | * Flush the provided inode then attempt to gather others from the |
3651 | * cluster into the write. | |
e53946db | 3652 | * |
f2019299 BF |
3653 | * Note: Once we attempt to flush an inode, we must run buffer |
3654 | * completion callbacks on any failure. If this fails, simulate an I/O | |
3655 | * failure on the buffer and shut down. | |
1da177e4 | 3656 | */ |
f2019299 BF |
3657 | error = xfs_iflush_int(ip, bp); |
3658 | if (!error) | |
3659 | error = xfs_iflush_cluster(ip, bp); | |
3660 | if (error) { | |
3661 | bp->b_flags |= XBF_ASYNC; | |
3662 | xfs_buf_ioend_fail(bp); | |
3663 | goto shutdown; | |
3664 | } | |
1da177e4 | 3665 | |
4c46819a CH |
3666 | *bpp = bp; |
3667 | return 0; | |
1da177e4 | 3668 | |
f2019299 | 3669 | abort: |
88fc1879 | 3670 | xfs_iflush_abort(ip); |
f2019299 BF |
3671 | shutdown: |
3672 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); | |
32ce90a4 | 3673 | return error; |
1da177e4 LT |
3674 | } |
3675 | ||
1da177e4 LT |
3676 | STATIC int |
3677 | xfs_iflush_int( | |
93848a99 CH |
3678 | struct xfs_inode *ip, |
3679 | struct xfs_buf *bp) | |
1da177e4 | 3680 | { |
93848a99 CH |
3681 | struct xfs_inode_log_item *iip = ip->i_itemp; |
3682 | struct xfs_dinode *dip; | |
3683 | struct xfs_mount *mp = ip->i_mount; | |
f2019299 | 3684 | int error; |
1da177e4 | 3685 | |
579aa9ca | 3686 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
474fce06 | 3687 | ASSERT(xfs_isiflocked(ip)); |
f7e67b20 | 3688 | ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE || |
daf83964 | 3689 | ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
93848a99 | 3690 | ASSERT(iip != NULL && iip->ili_fields != 0); |
1da177e4 | 3691 | |
88ee2df7 | 3692 | dip = xfs_buf_offset(bp, ip->i_imap.im_boffset); |
1da177e4 | 3693 | |
f2019299 BF |
3694 | /* |
3695 | * We don't flush the inode if any of the following checks fail, but we | |
3696 | * do still update the log item and attach to the backing buffer as if | |
3697 | * the flush happened. This is a formality to facilitate predictable | |
3698 | * error handling as the caller will shutdown and fail the buffer. | |
3699 | */ | |
3700 | error = -EFSCORRUPTED; | |
69ef921b | 3701 | if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC), |
9e24cfd0 | 3702 | mp, XFS_ERRTAG_IFLUSH_1)) { |
6a19d939 | 3703 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
c9690043 | 3704 | "%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT, |
6a19d939 | 3705 | __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); |
f2019299 | 3706 | goto flush_out; |
1da177e4 | 3707 | } |
c19b3b05 | 3708 | if (S_ISREG(VFS_I(ip)->i_mode)) { |
1da177e4 | 3709 | if (XFS_TEST_ERROR( |
f7e67b20 CH |
3710 | ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS && |
3711 | ip->i_df.if_format != XFS_DINODE_FMT_BTREE, | |
9e24cfd0 | 3712 | mp, XFS_ERRTAG_IFLUSH_3)) { |
6a19d939 | 3713 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
c9690043 | 3714 | "%s: Bad regular inode %Lu, ptr "PTR_FMT, |
6a19d939 | 3715 | __func__, ip->i_ino, ip); |
f2019299 | 3716 | goto flush_out; |
1da177e4 | 3717 | } |
c19b3b05 | 3718 | } else if (S_ISDIR(VFS_I(ip)->i_mode)) { |
1da177e4 | 3719 | if (XFS_TEST_ERROR( |
f7e67b20 CH |
3720 | ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS && |
3721 | ip->i_df.if_format != XFS_DINODE_FMT_BTREE && | |
3722 | ip->i_df.if_format != XFS_DINODE_FMT_LOCAL, | |
9e24cfd0 | 3723 | mp, XFS_ERRTAG_IFLUSH_4)) { |
6a19d939 | 3724 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
c9690043 | 3725 | "%s: Bad directory inode %Lu, ptr "PTR_FMT, |
6a19d939 | 3726 | __func__, ip->i_ino, ip); |
f2019299 | 3727 | goto flush_out; |
1da177e4 LT |
3728 | } |
3729 | } | |
daf83964 | 3730 | if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) > |
9e24cfd0 | 3731 | ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) { |
6a19d939 DC |
3732 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
3733 | "%s: detected corrupt incore inode %Lu, " | |
c9690043 | 3734 | "total extents = %d, nblocks = %Ld, ptr "PTR_FMT, |
6a19d939 | 3735 | __func__, ip->i_ino, |
daf83964 | 3736 | ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp), |
6a19d939 | 3737 | ip->i_d.di_nblocks, ip); |
f2019299 | 3738 | goto flush_out; |
1da177e4 LT |
3739 | } |
3740 | if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize, | |
9e24cfd0 | 3741 | mp, XFS_ERRTAG_IFLUSH_6)) { |
6a19d939 | 3742 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
c9690043 | 3743 | "%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT, |
6a19d939 | 3744 | __func__, ip->i_ino, ip->i_d.di_forkoff, ip); |
f2019299 | 3745 | goto flush_out; |
1da177e4 | 3746 | } |
e60896d8 | 3747 | |
1da177e4 | 3748 | /* |
263997a6 | 3749 | * Inode item log recovery for v2 inodes are dependent on the |
e60896d8 DC |
3750 | * di_flushiter count for correct sequencing. We bump the flush |
3751 | * iteration count so we can detect flushes which postdate a log record | |
3752 | * during recovery. This is redundant as we now log every change and | |
3753 | * hence this can't happen but we need to still do it to ensure | |
3754 | * backwards compatibility with old kernels that predate logging all | |
3755 | * inode changes. | |
1da177e4 | 3756 | */ |
6471e9c5 | 3757 | if (!xfs_sb_version_has_v3inode(&mp->m_sb)) |
e60896d8 | 3758 | ip->i_d.di_flushiter++; |
1da177e4 | 3759 | |
0f45a1b2 CH |
3760 | /* |
3761 | * If there are inline format data / attr forks attached to this inode, | |
3762 | * make sure they are not corrupt. | |
3763 | */ | |
f7e67b20 | 3764 | if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL && |
0f45a1b2 CH |
3765 | xfs_ifork_verify_local_data(ip)) |
3766 | goto flush_out; | |
f7e67b20 | 3767 | if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL && |
0f45a1b2 | 3768 | xfs_ifork_verify_local_attr(ip)) |
f2019299 | 3769 | goto flush_out; |
005c5db8 | 3770 | |
1da177e4 | 3771 | /* |
3987848c DC |
3772 | * Copy the dirty parts of the inode into the on-disk inode. We always |
3773 | * copy out the core of the inode, because if the inode is dirty at all | |
3774 | * the core must be. | |
1da177e4 | 3775 | */ |
93f958f9 | 3776 | xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn); |
1da177e4 LT |
3777 | |
3778 | /* Wrap, we never let the log put out DI_MAX_FLUSH */ | |
3779 | if (ip->i_d.di_flushiter == DI_MAX_FLUSH) | |
3780 | ip->i_d.di_flushiter = 0; | |
3781 | ||
005c5db8 DW |
3782 | xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK); |
3783 | if (XFS_IFORK_Q(ip)) | |
3784 | xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK); | |
1da177e4 LT |
3785 | xfs_inobp_check(mp, bp); |
3786 | ||
3787 | /* | |
f5d8d5c4 CH |
3788 | * We've recorded everything logged in the inode, so we'd like to clear |
3789 | * the ili_fields bits so we don't log and flush things unnecessarily. | |
3790 | * However, we can't stop logging all this information until the data | |
3791 | * we've copied into the disk buffer is written to disk. If we did we | |
3792 | * might overwrite the copy of the inode in the log with all the data | |
3793 | * after re-logging only part of it, and in the face of a crash we | |
3794 | * wouldn't have all the data we need to recover. | |
1da177e4 | 3795 | * |
f5d8d5c4 CH |
3796 | * What we do is move the bits to the ili_last_fields field. When |
3797 | * logging the inode, these bits are moved back to the ili_fields field. | |
3798 | * In the xfs_iflush_done() routine we clear ili_last_fields, since we | |
3799 | * know that the information those bits represent is permanently on | |
3800 | * disk. As long as the flush completes before the inode is logged | |
3801 | * again, then both ili_fields and ili_last_fields will be cleared. | |
1da177e4 | 3802 | */ |
f2019299 BF |
3803 | error = 0; |
3804 | flush_out: | |
1319ebef | 3805 | spin_lock(&iip->ili_lock); |
93848a99 CH |
3806 | iip->ili_last_fields = iip->ili_fields; |
3807 | iip->ili_fields = 0; | |
fc0561ce | 3808 | iip->ili_fsync_fields = 0; |
1319ebef | 3809 | spin_unlock(&iip->ili_lock); |
1da177e4 | 3810 | |
1319ebef DC |
3811 | /* |
3812 | * Store the current LSN of the inode so that we can tell whether the | |
3813 | * item has moved in the AIL from xfs_iflush_done(). | |
3814 | */ | |
93848a99 CH |
3815 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
3816 | &iip->ili_item.li_lsn); | |
1da177e4 | 3817 | |
93848a99 CH |
3818 | /* generate the checksum. */ |
3819 | xfs_dinode_calc_crc(mp, dip); | |
f2019299 | 3820 | return error; |
1da177e4 | 3821 | } |
44a8736b DW |
3822 | |
3823 | /* Release an inode. */ | |
3824 | void | |
3825 | xfs_irele( | |
3826 | struct xfs_inode *ip) | |
3827 | { | |
3828 | trace_xfs_irele(ip, _RET_IP_); | |
3829 | iput(VFS_I(ip)); | |
3830 | } | |
54fbdd10 CH |
3831 | |
3832 | /* | |
3833 | * Ensure all commited transactions touching the inode are written to the log. | |
3834 | */ | |
3835 | int | |
3836 | xfs_log_force_inode( | |
3837 | struct xfs_inode *ip) | |
3838 | { | |
3839 | xfs_lsn_t lsn = 0; | |
3840 | ||
3841 | xfs_ilock(ip, XFS_ILOCK_SHARED); | |
3842 | if (xfs_ipincount(ip)) | |
3843 | lsn = ip->i_itemp->ili_last_lsn; | |
3844 | xfs_iunlock(ip, XFS_ILOCK_SHARED); | |
3845 | ||
3846 | if (!lsn) | |
3847 | return 0; | |
3848 | return xfs_log_force_lsn(ip->i_mount, lsn, XFS_LOG_SYNC, NULL); | |
3849 | } | |
e2aaee9c DW |
3850 | |
3851 | /* | |
3852 | * Grab the exclusive iolock for a data copy from src to dest, making sure to | |
3853 | * abide vfs locking order (lowest pointer value goes first) and breaking the | |
3854 | * layout leases before proceeding. The loop is needed because we cannot call | |
3855 | * the blocking break_layout() with the iolocks held, and therefore have to | |
3856 | * back out both locks. | |
3857 | */ | |
3858 | static int | |
3859 | xfs_iolock_two_inodes_and_break_layout( | |
3860 | struct inode *src, | |
3861 | struct inode *dest) | |
3862 | { | |
3863 | int error; | |
3864 | ||
3865 | if (src > dest) | |
3866 | swap(src, dest); | |
3867 | ||
3868 | retry: | |
3869 | /* Wait to break both inodes' layouts before we start locking. */ | |
3870 | error = break_layout(src, true); | |
3871 | if (error) | |
3872 | return error; | |
3873 | if (src != dest) { | |
3874 | error = break_layout(dest, true); | |
3875 | if (error) | |
3876 | return error; | |
3877 | } | |
3878 | ||
3879 | /* Lock one inode and make sure nobody got in and leased it. */ | |
3880 | inode_lock(src); | |
3881 | error = break_layout(src, false); | |
3882 | if (error) { | |
3883 | inode_unlock(src); | |
3884 | if (error == -EWOULDBLOCK) | |
3885 | goto retry; | |
3886 | return error; | |
3887 | } | |
3888 | ||
3889 | if (src == dest) | |
3890 | return 0; | |
3891 | ||
3892 | /* Lock the other inode and make sure nobody got in and leased it. */ | |
3893 | inode_lock_nested(dest, I_MUTEX_NONDIR2); | |
3894 | error = break_layout(dest, false); | |
3895 | if (error) { | |
3896 | inode_unlock(src); | |
3897 | inode_unlock(dest); | |
3898 | if (error == -EWOULDBLOCK) | |
3899 | goto retry; | |
3900 | return error; | |
3901 | } | |
3902 | ||
3903 | return 0; | |
3904 | } | |
3905 | ||
3906 | /* | |
3907 | * Lock two inodes so that userspace cannot initiate I/O via file syscalls or | |
3908 | * mmap activity. | |
3909 | */ | |
3910 | int | |
3911 | xfs_ilock2_io_mmap( | |
3912 | struct xfs_inode *ip1, | |
3913 | struct xfs_inode *ip2) | |
3914 | { | |
3915 | int ret; | |
3916 | ||
3917 | ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2)); | |
3918 | if (ret) | |
3919 | return ret; | |
3920 | if (ip1 == ip2) | |
3921 | xfs_ilock(ip1, XFS_MMAPLOCK_EXCL); | |
3922 | else | |
3923 | xfs_lock_two_inodes(ip1, XFS_MMAPLOCK_EXCL, | |
3924 | ip2, XFS_MMAPLOCK_EXCL); | |
3925 | return 0; | |
3926 | } | |
3927 | ||
3928 | /* Unlock both inodes to allow IO and mmap activity. */ | |
3929 | void | |
3930 | xfs_iunlock2_io_mmap( | |
3931 | struct xfs_inode *ip1, | |
3932 | struct xfs_inode *ip2) | |
3933 | { | |
3934 | bool same_inode = (ip1 == ip2); | |
3935 | ||
3936 | xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL); | |
3937 | if (!same_inode) | |
3938 | xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL); | |
3939 | inode_unlock(VFS_I(ip2)); | |
3940 | if (!same_inode) | |
3941 | inode_unlock(VFS_I(ip1)); | |
3942 | } |