Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
3e57ecf6 | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
7b718769 | 3 | * All Rights Reserved. |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
40ebd81d RD |
18 | #include <linux/log2.h> |
19 | ||
1da177e4 | 20 | #include "xfs.h" |
a844f451 | 21 | #include "xfs_fs.h" |
70a9883c | 22 | #include "xfs_shared.h" |
239880ef DC |
23 | #include "xfs_format.h" |
24 | #include "xfs_log_format.h" | |
25 | #include "xfs_trans_resv.h" | |
a844f451 | 26 | #include "xfs_inum.h" |
1da177e4 LT |
27 | #include "xfs_sb.h" |
28 | #include "xfs_ag.h" | |
1da177e4 | 29 | #include "xfs_mount.h" |
a4fbe6ab | 30 | #include "xfs_inode.h" |
57062787 | 31 | #include "xfs_da_format.h" |
c24b5dfa | 32 | #include "xfs_da_btree.h" |
c24b5dfa | 33 | #include "xfs_dir2.h" |
a844f451 | 34 | #include "xfs_attr_sf.h" |
c24b5dfa | 35 | #include "xfs_attr.h" |
239880ef DC |
36 | #include "xfs_trans_space.h" |
37 | #include "xfs_trans.h" | |
1da177e4 | 38 | #include "xfs_buf_item.h" |
a844f451 | 39 | #include "xfs_inode_item.h" |
a844f451 NS |
40 | #include "xfs_ialloc.h" |
41 | #include "xfs_bmap.h" | |
68988114 | 42 | #include "xfs_bmap_util.h" |
1da177e4 | 43 | #include "xfs_error.h" |
1da177e4 | 44 | #include "xfs_quota.h" |
2a82b8be | 45 | #include "xfs_filestream.h" |
93848a99 | 46 | #include "xfs_cksum.h" |
0b1b213f | 47 | #include "xfs_trace.h" |
33479e05 | 48 | #include "xfs_icache.h" |
c24b5dfa | 49 | #include "xfs_symlink.h" |
239880ef DC |
50 | #include "xfs_trans_priv.h" |
51 | #include "xfs_log.h" | |
a4fbe6ab | 52 | #include "xfs_bmap_btree.h" |
1da177e4 | 53 | |
1da177e4 | 54 | kmem_zone_t *xfs_inode_zone; |
1da177e4 LT |
55 | |
56 | /* | |
8f04c47a | 57 | * Used in xfs_itruncate_extents(). This is the maximum number of extents |
1da177e4 LT |
58 | * freed from a file in a single transaction. |
59 | */ | |
60 | #define XFS_ITRUNC_MAX_EXTENTS 2 | |
61 | ||
62 | STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *); | |
1da177e4 | 63 | |
2a0ec1d9 DC |
64 | /* |
65 | * helper function to extract extent size hint from inode | |
66 | */ | |
67 | xfs_extlen_t | |
68 | xfs_get_extsz_hint( | |
69 | struct xfs_inode *ip) | |
70 | { | |
71 | if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize) | |
72 | return ip->i_d.di_extsize; | |
73 | if (XFS_IS_REALTIME_INODE(ip)) | |
74 | return ip->i_mount->m_sb.sb_rextsize; | |
75 | return 0; | |
76 | } | |
77 | ||
fa96acad DC |
78 | /* |
79 | * This is a wrapper routine around the xfs_ilock() routine used to centralize | |
80 | * some grungy code. It is used in places that wish to lock the inode solely | |
81 | * for reading the extents. The reason these places can't just call | |
82 | * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the | |
83 | * extents from disk for a file in b-tree format. If the inode is in b-tree | |
84 | * format, then we need to lock the inode exclusively until the extents are read | |
85 | * in. Locking it exclusively all the time would limit our parallelism | |
86 | * unnecessarily, though. What we do instead is check to see if the extents | |
87 | * have been read in yet, and only lock the inode exclusively if they have not. | |
88 | * | |
89 | * The function returns a value which should be given to the corresponding | |
90 | * xfs_iunlock_map_shared(). This value is the mode in which the lock was | |
91 | * actually taken. | |
92 | */ | |
93 | uint | |
94 | xfs_ilock_map_shared( | |
95 | xfs_inode_t *ip) | |
96 | { | |
97 | uint lock_mode; | |
98 | ||
99 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && | |
100 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { | |
101 | lock_mode = XFS_ILOCK_EXCL; | |
102 | } else { | |
103 | lock_mode = XFS_ILOCK_SHARED; | |
104 | } | |
105 | ||
106 | xfs_ilock(ip, lock_mode); | |
107 | ||
108 | return lock_mode; | |
109 | } | |
110 | ||
111 | /* | |
112 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). | |
113 | * All it does is call xfs_iunlock() with the given lock_mode. | |
114 | */ | |
115 | void | |
116 | xfs_iunlock_map_shared( | |
117 | xfs_inode_t *ip, | |
118 | unsigned int lock_mode) | |
119 | { | |
120 | xfs_iunlock(ip, lock_mode); | |
121 | } | |
122 | ||
123 | /* | |
124 | * The xfs inode contains 2 locks: a multi-reader lock called the | |
125 | * i_iolock and a multi-reader lock called the i_lock. This routine | |
126 | * allows either or both of the locks to be obtained. | |
127 | * | |
128 | * The 2 locks should always be ordered so that the IO lock is | |
129 | * obtained first in order to prevent deadlock. | |
130 | * | |
131 | * ip -- the inode being locked | |
132 | * lock_flags -- this parameter indicates the inode's locks | |
133 | * to be locked. It can be: | |
134 | * XFS_IOLOCK_SHARED, | |
135 | * XFS_IOLOCK_EXCL, | |
136 | * XFS_ILOCK_SHARED, | |
137 | * XFS_ILOCK_EXCL, | |
138 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, | |
139 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, | |
140 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, | |
141 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL | |
142 | */ | |
143 | void | |
144 | xfs_ilock( | |
145 | xfs_inode_t *ip, | |
146 | uint lock_flags) | |
147 | { | |
148 | trace_xfs_ilock(ip, lock_flags, _RET_IP_); | |
149 | ||
150 | /* | |
151 | * You can't set both SHARED and EXCL for the same lock, | |
152 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
153 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
154 | */ | |
155 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
156 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
157 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
158 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
159 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); | |
160 | ||
161 | if (lock_flags & XFS_IOLOCK_EXCL) | |
162 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); | |
163 | else if (lock_flags & XFS_IOLOCK_SHARED) | |
164 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); | |
165 | ||
166 | if (lock_flags & XFS_ILOCK_EXCL) | |
167 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); | |
168 | else if (lock_flags & XFS_ILOCK_SHARED) | |
169 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); | |
170 | } | |
171 | ||
172 | /* | |
173 | * This is just like xfs_ilock(), except that the caller | |
174 | * is guaranteed not to sleep. It returns 1 if it gets | |
175 | * the requested locks and 0 otherwise. If the IO lock is | |
176 | * obtained but the inode lock cannot be, then the IO lock | |
177 | * is dropped before returning. | |
178 | * | |
179 | * ip -- the inode being locked | |
180 | * lock_flags -- this parameter indicates the inode's locks to be | |
181 | * to be locked. See the comment for xfs_ilock() for a list | |
182 | * of valid values. | |
183 | */ | |
184 | int | |
185 | xfs_ilock_nowait( | |
186 | xfs_inode_t *ip, | |
187 | uint lock_flags) | |
188 | { | |
189 | trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_); | |
190 | ||
191 | /* | |
192 | * You can't set both SHARED and EXCL for the same lock, | |
193 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
194 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
195 | */ | |
196 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
197 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
198 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
199 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
200 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); | |
201 | ||
202 | if (lock_flags & XFS_IOLOCK_EXCL) { | |
203 | if (!mrtryupdate(&ip->i_iolock)) | |
204 | goto out; | |
205 | } else if (lock_flags & XFS_IOLOCK_SHARED) { | |
206 | if (!mrtryaccess(&ip->i_iolock)) | |
207 | goto out; | |
208 | } | |
209 | if (lock_flags & XFS_ILOCK_EXCL) { | |
210 | if (!mrtryupdate(&ip->i_lock)) | |
211 | goto out_undo_iolock; | |
212 | } else if (lock_flags & XFS_ILOCK_SHARED) { | |
213 | if (!mrtryaccess(&ip->i_lock)) | |
214 | goto out_undo_iolock; | |
215 | } | |
216 | return 1; | |
217 | ||
218 | out_undo_iolock: | |
219 | if (lock_flags & XFS_IOLOCK_EXCL) | |
220 | mrunlock_excl(&ip->i_iolock); | |
221 | else if (lock_flags & XFS_IOLOCK_SHARED) | |
222 | mrunlock_shared(&ip->i_iolock); | |
223 | out: | |
224 | return 0; | |
225 | } | |
226 | ||
227 | /* | |
228 | * xfs_iunlock() is used to drop the inode locks acquired with | |
229 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass | |
230 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so | |
231 | * that we know which locks to drop. | |
232 | * | |
233 | * ip -- the inode being unlocked | |
234 | * lock_flags -- this parameter indicates the inode's locks to be | |
235 | * to be unlocked. See the comment for xfs_ilock() for a list | |
236 | * of valid values for this parameter. | |
237 | * | |
238 | */ | |
239 | void | |
240 | xfs_iunlock( | |
241 | xfs_inode_t *ip, | |
242 | uint lock_flags) | |
243 | { | |
244 | /* | |
245 | * You can't set both SHARED and EXCL for the same lock, | |
246 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
247 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
248 | */ | |
249 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
250 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
251 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
252 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
253 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); | |
254 | ASSERT(lock_flags != 0); | |
255 | ||
256 | if (lock_flags & XFS_IOLOCK_EXCL) | |
257 | mrunlock_excl(&ip->i_iolock); | |
258 | else if (lock_flags & XFS_IOLOCK_SHARED) | |
259 | mrunlock_shared(&ip->i_iolock); | |
260 | ||
261 | if (lock_flags & XFS_ILOCK_EXCL) | |
262 | mrunlock_excl(&ip->i_lock); | |
263 | else if (lock_flags & XFS_ILOCK_SHARED) | |
264 | mrunlock_shared(&ip->i_lock); | |
265 | ||
266 | trace_xfs_iunlock(ip, lock_flags, _RET_IP_); | |
267 | } | |
268 | ||
269 | /* | |
270 | * give up write locks. the i/o lock cannot be held nested | |
271 | * if it is being demoted. | |
272 | */ | |
273 | void | |
274 | xfs_ilock_demote( | |
275 | xfs_inode_t *ip, | |
276 | uint lock_flags) | |
277 | { | |
278 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); | |
279 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); | |
280 | ||
281 | if (lock_flags & XFS_ILOCK_EXCL) | |
282 | mrdemote(&ip->i_lock); | |
283 | if (lock_flags & XFS_IOLOCK_EXCL) | |
284 | mrdemote(&ip->i_iolock); | |
285 | ||
286 | trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_); | |
287 | } | |
288 | ||
742ae1e3 | 289 | #if defined(DEBUG) || defined(XFS_WARN) |
fa96acad DC |
290 | int |
291 | xfs_isilocked( | |
292 | xfs_inode_t *ip, | |
293 | uint lock_flags) | |
294 | { | |
295 | if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { | |
296 | if (!(lock_flags & XFS_ILOCK_SHARED)) | |
297 | return !!ip->i_lock.mr_writer; | |
298 | return rwsem_is_locked(&ip->i_lock.mr_lock); | |
299 | } | |
300 | ||
301 | if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) { | |
302 | if (!(lock_flags & XFS_IOLOCK_SHARED)) | |
303 | return !!ip->i_iolock.mr_writer; | |
304 | return rwsem_is_locked(&ip->i_iolock.mr_lock); | |
305 | } | |
306 | ||
307 | ASSERT(0); | |
308 | return 0; | |
309 | } | |
310 | #endif | |
311 | ||
c24b5dfa DC |
312 | #ifdef DEBUG |
313 | int xfs_locked_n; | |
314 | int xfs_small_retries; | |
315 | int xfs_middle_retries; | |
316 | int xfs_lots_retries; | |
317 | int xfs_lock_delays; | |
318 | #endif | |
319 | ||
320 | /* | |
321 | * Bump the subclass so xfs_lock_inodes() acquires each lock with | |
322 | * a different value | |
323 | */ | |
324 | static inline int | |
325 | xfs_lock_inumorder(int lock_mode, int subclass) | |
326 | { | |
327 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) | |
328 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT; | |
329 | if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) | |
330 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT; | |
331 | ||
332 | return lock_mode; | |
333 | } | |
334 | ||
335 | /* | |
336 | * The following routine will lock n inodes in exclusive mode. | |
337 | * We assume the caller calls us with the inodes in i_ino order. | |
338 | * | |
339 | * We need to detect deadlock where an inode that we lock | |
340 | * is in the AIL and we start waiting for another inode that is locked | |
341 | * by a thread in a long running transaction (such as truncate). This can | |
342 | * result in deadlock since the long running trans might need to wait | |
343 | * for the inode we just locked in order to push the tail and free space | |
344 | * in the log. | |
345 | */ | |
346 | void | |
347 | xfs_lock_inodes( | |
348 | xfs_inode_t **ips, | |
349 | int inodes, | |
350 | uint lock_mode) | |
351 | { | |
352 | int attempts = 0, i, j, try_lock; | |
353 | xfs_log_item_t *lp; | |
354 | ||
355 | ASSERT(ips && (inodes >= 2)); /* we need at least two */ | |
356 | ||
357 | try_lock = 0; | |
358 | i = 0; | |
359 | ||
360 | again: | |
361 | for (; i < inodes; i++) { | |
362 | ASSERT(ips[i]); | |
363 | ||
364 | if (i && (ips[i] == ips[i-1])) /* Already locked */ | |
365 | continue; | |
366 | ||
367 | /* | |
368 | * If try_lock is not set yet, make sure all locked inodes | |
369 | * are not in the AIL. | |
370 | * If any are, set try_lock to be used later. | |
371 | */ | |
372 | ||
373 | if (!try_lock) { | |
374 | for (j = (i - 1); j >= 0 && !try_lock; j--) { | |
375 | lp = (xfs_log_item_t *)ips[j]->i_itemp; | |
376 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { | |
377 | try_lock++; | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | /* | |
383 | * If any of the previous locks we have locked is in the AIL, | |
384 | * we must TRY to get the second and subsequent locks. If | |
385 | * we can't get any, we must release all we have | |
386 | * and try again. | |
387 | */ | |
388 | ||
389 | if (try_lock) { | |
390 | /* try_lock must be 0 if i is 0. */ | |
391 | /* | |
392 | * try_lock means we have an inode locked | |
393 | * that is in the AIL. | |
394 | */ | |
395 | ASSERT(i != 0); | |
396 | if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) { | |
397 | attempts++; | |
398 | ||
399 | /* | |
400 | * Unlock all previous guys and try again. | |
401 | * xfs_iunlock will try to push the tail | |
402 | * if the inode is in the AIL. | |
403 | */ | |
404 | ||
405 | for(j = i - 1; j >= 0; j--) { | |
406 | ||
407 | /* | |
408 | * Check to see if we've already | |
409 | * unlocked this one. | |
410 | * Not the first one going back, | |
411 | * and the inode ptr is the same. | |
412 | */ | |
413 | if ((j != (i - 1)) && ips[j] == | |
414 | ips[j+1]) | |
415 | continue; | |
416 | ||
417 | xfs_iunlock(ips[j], lock_mode); | |
418 | } | |
419 | ||
420 | if ((attempts % 5) == 0) { | |
421 | delay(1); /* Don't just spin the CPU */ | |
422 | #ifdef DEBUG | |
423 | xfs_lock_delays++; | |
424 | #endif | |
425 | } | |
426 | i = 0; | |
427 | try_lock = 0; | |
428 | goto again; | |
429 | } | |
430 | } else { | |
431 | xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); | |
432 | } | |
433 | } | |
434 | ||
435 | #ifdef DEBUG | |
436 | if (attempts) { | |
437 | if (attempts < 5) xfs_small_retries++; | |
438 | else if (attempts < 100) xfs_middle_retries++; | |
439 | else xfs_lots_retries++; | |
440 | } else { | |
441 | xfs_locked_n++; | |
442 | } | |
443 | #endif | |
444 | } | |
445 | ||
446 | /* | |
447 | * xfs_lock_two_inodes() can only be used to lock one type of lock | |
448 | * at a time - the iolock or the ilock, but not both at once. If | |
449 | * we lock both at once, lockdep will report false positives saying | |
450 | * we have violated locking orders. | |
451 | */ | |
452 | void | |
453 | xfs_lock_two_inodes( | |
454 | xfs_inode_t *ip0, | |
455 | xfs_inode_t *ip1, | |
456 | uint lock_mode) | |
457 | { | |
458 | xfs_inode_t *temp; | |
459 | int attempts = 0; | |
460 | xfs_log_item_t *lp; | |
461 | ||
462 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) | |
463 | ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0); | |
464 | ASSERT(ip0->i_ino != ip1->i_ino); | |
465 | ||
466 | if (ip0->i_ino > ip1->i_ino) { | |
467 | temp = ip0; | |
468 | ip0 = ip1; | |
469 | ip1 = temp; | |
470 | } | |
471 | ||
472 | again: | |
473 | xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0)); | |
474 | ||
475 | /* | |
476 | * If the first lock we have locked is in the AIL, we must TRY to get | |
477 | * the second lock. If we can't get it, we must release the first one | |
478 | * and try again. | |
479 | */ | |
480 | lp = (xfs_log_item_t *)ip0->i_itemp; | |
481 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { | |
482 | if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) { | |
483 | xfs_iunlock(ip0, lock_mode); | |
484 | if ((++attempts % 5) == 0) | |
485 | delay(1); /* Don't just spin the CPU */ | |
486 | goto again; | |
487 | } | |
488 | } else { | |
489 | xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1)); | |
490 | } | |
491 | } | |
492 | ||
493 | ||
fa96acad DC |
494 | void |
495 | __xfs_iflock( | |
496 | struct xfs_inode *ip) | |
497 | { | |
498 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT); | |
499 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT); | |
500 | ||
501 | do { | |
502 | prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE); | |
503 | if (xfs_isiflocked(ip)) | |
504 | io_schedule(); | |
505 | } while (!xfs_iflock_nowait(ip)); | |
506 | ||
507 | finish_wait(wq, &wait.wait); | |
508 | } | |
509 | ||
1da177e4 LT |
510 | STATIC uint |
511 | _xfs_dic2xflags( | |
1da177e4 LT |
512 | __uint16_t di_flags) |
513 | { | |
514 | uint flags = 0; | |
515 | ||
516 | if (di_flags & XFS_DIFLAG_ANY) { | |
517 | if (di_flags & XFS_DIFLAG_REALTIME) | |
518 | flags |= XFS_XFLAG_REALTIME; | |
519 | if (di_flags & XFS_DIFLAG_PREALLOC) | |
520 | flags |= XFS_XFLAG_PREALLOC; | |
521 | if (di_flags & XFS_DIFLAG_IMMUTABLE) | |
522 | flags |= XFS_XFLAG_IMMUTABLE; | |
523 | if (di_flags & XFS_DIFLAG_APPEND) | |
524 | flags |= XFS_XFLAG_APPEND; | |
525 | if (di_flags & XFS_DIFLAG_SYNC) | |
526 | flags |= XFS_XFLAG_SYNC; | |
527 | if (di_flags & XFS_DIFLAG_NOATIME) | |
528 | flags |= XFS_XFLAG_NOATIME; | |
529 | if (di_flags & XFS_DIFLAG_NODUMP) | |
530 | flags |= XFS_XFLAG_NODUMP; | |
531 | if (di_flags & XFS_DIFLAG_RTINHERIT) | |
532 | flags |= XFS_XFLAG_RTINHERIT; | |
533 | if (di_flags & XFS_DIFLAG_PROJINHERIT) | |
534 | flags |= XFS_XFLAG_PROJINHERIT; | |
535 | if (di_flags & XFS_DIFLAG_NOSYMLINKS) | |
536 | flags |= XFS_XFLAG_NOSYMLINKS; | |
dd9f438e NS |
537 | if (di_flags & XFS_DIFLAG_EXTSIZE) |
538 | flags |= XFS_XFLAG_EXTSIZE; | |
539 | if (di_flags & XFS_DIFLAG_EXTSZINHERIT) | |
540 | flags |= XFS_XFLAG_EXTSZINHERIT; | |
d3446eac BN |
541 | if (di_flags & XFS_DIFLAG_NODEFRAG) |
542 | flags |= XFS_XFLAG_NODEFRAG; | |
2a82b8be DC |
543 | if (di_flags & XFS_DIFLAG_FILESTREAM) |
544 | flags |= XFS_XFLAG_FILESTREAM; | |
1da177e4 LT |
545 | } |
546 | ||
547 | return flags; | |
548 | } | |
549 | ||
550 | uint | |
551 | xfs_ip2xflags( | |
552 | xfs_inode_t *ip) | |
553 | { | |
347d1c01 | 554 | xfs_icdinode_t *dic = &ip->i_d; |
1da177e4 | 555 | |
a916e2bd | 556 | return _xfs_dic2xflags(dic->di_flags) | |
45ba598e | 557 | (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0); |
1da177e4 LT |
558 | } |
559 | ||
560 | uint | |
561 | xfs_dic2xflags( | |
45ba598e | 562 | xfs_dinode_t *dip) |
1da177e4 | 563 | { |
81591fe2 | 564 | return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) | |
45ba598e | 565 | (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0); |
1da177e4 LT |
566 | } |
567 | ||
c24b5dfa DC |
568 | /* |
569 | * Lookups up an inode from "name". If ci_name is not NULL, then a CI match | |
570 | * is allowed, otherwise it has to be an exact match. If a CI match is found, | |
571 | * ci_name->name will point to a the actual name (caller must free) or | |
572 | * will be set to NULL if an exact match is found. | |
573 | */ | |
574 | int | |
575 | xfs_lookup( | |
576 | xfs_inode_t *dp, | |
577 | struct xfs_name *name, | |
578 | xfs_inode_t **ipp, | |
579 | struct xfs_name *ci_name) | |
580 | { | |
581 | xfs_ino_t inum; | |
582 | int error; | |
583 | uint lock_mode; | |
584 | ||
585 | trace_xfs_lookup(dp, name); | |
586 | ||
587 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) | |
588 | return XFS_ERROR(EIO); | |
589 | ||
590 | lock_mode = xfs_ilock_map_shared(dp); | |
591 | error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); | |
592 | xfs_iunlock_map_shared(dp, lock_mode); | |
593 | ||
594 | if (error) | |
595 | goto out; | |
596 | ||
597 | error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp); | |
598 | if (error) | |
599 | goto out_free_name; | |
600 | ||
601 | return 0; | |
602 | ||
603 | out_free_name: | |
604 | if (ci_name) | |
605 | kmem_free(ci_name->name); | |
606 | out: | |
607 | *ipp = NULL; | |
608 | return error; | |
609 | } | |
610 | ||
1da177e4 LT |
611 | /* |
612 | * Allocate an inode on disk and return a copy of its in-core version. | |
613 | * The in-core inode is locked exclusively. Set mode, nlink, and rdev | |
614 | * appropriately within the inode. The uid and gid for the inode are | |
615 | * set according to the contents of the given cred structure. | |
616 | * | |
617 | * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc() | |
cd856db6 CM |
618 | * has a free inode available, call xfs_iget() to obtain the in-core |
619 | * version of the allocated inode. Finally, fill in the inode and | |
620 | * log its initial contents. In this case, ialloc_context would be | |
621 | * set to NULL. | |
1da177e4 | 622 | * |
cd856db6 CM |
623 | * If xfs_dialloc() does not have an available inode, it will replenish |
624 | * its supply by doing an allocation. Since we can only do one | |
625 | * allocation within a transaction without deadlocks, we must commit | |
626 | * the current transaction before returning the inode itself. | |
627 | * In this case, therefore, we will set ialloc_context and return. | |
1da177e4 LT |
628 | * The caller should then commit the current transaction, start a new |
629 | * transaction, and call xfs_ialloc() again to actually get the inode. | |
630 | * | |
631 | * To ensure that some other process does not grab the inode that | |
632 | * was allocated during the first call to xfs_ialloc(), this routine | |
633 | * also returns the [locked] bp pointing to the head of the freelist | |
634 | * as ialloc_context. The caller should hold this buffer across | |
635 | * the commit and pass it back into this routine on the second call. | |
b11f94d5 DC |
636 | * |
637 | * If we are allocating quota inodes, we do not have a parent inode | |
638 | * to attach to or associate with (i.e. pip == NULL) because they | |
639 | * are not linked into the directory structure - they are attached | |
640 | * directly to the superblock - and so have no parent. | |
1da177e4 LT |
641 | */ |
642 | int | |
643 | xfs_ialloc( | |
644 | xfs_trans_t *tp, | |
645 | xfs_inode_t *pip, | |
576b1d67 | 646 | umode_t mode, |
31b084ae | 647 | xfs_nlink_t nlink, |
1da177e4 | 648 | xfs_dev_t rdev, |
6743099c | 649 | prid_t prid, |
1da177e4 LT |
650 | int okalloc, |
651 | xfs_buf_t **ialloc_context, | |
1da177e4 LT |
652 | xfs_inode_t **ipp) |
653 | { | |
93848a99 | 654 | struct xfs_mount *mp = tp->t_mountp; |
1da177e4 LT |
655 | xfs_ino_t ino; |
656 | xfs_inode_t *ip; | |
1da177e4 LT |
657 | uint flags; |
658 | int error; | |
dff35fd4 | 659 | timespec_t tv; |
bf904248 | 660 | int filestreams = 0; |
1da177e4 LT |
661 | |
662 | /* | |
663 | * Call the space management code to pick | |
664 | * the on-disk inode to be allocated. | |
665 | */ | |
b11f94d5 | 666 | error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc, |
08358906 | 667 | ialloc_context, &ino); |
bf904248 | 668 | if (error) |
1da177e4 | 669 | return error; |
08358906 | 670 | if (*ialloc_context || ino == NULLFSINO) { |
1da177e4 LT |
671 | *ipp = NULL; |
672 | return 0; | |
673 | } | |
674 | ASSERT(*ialloc_context == NULL); | |
675 | ||
676 | /* | |
677 | * Get the in-core inode with the lock held exclusively. | |
678 | * This is because we're setting fields here we need | |
679 | * to prevent others from looking at until we're done. | |
680 | */ | |
93848a99 | 681 | error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, |
ec3ba85f | 682 | XFS_ILOCK_EXCL, &ip); |
bf904248 | 683 | if (error) |
1da177e4 | 684 | return error; |
1da177e4 LT |
685 | ASSERT(ip != NULL); |
686 | ||
576b1d67 | 687 | ip->i_d.di_mode = mode; |
1da177e4 LT |
688 | ip->i_d.di_onlink = 0; |
689 | ip->i_d.di_nlink = nlink; | |
690 | ASSERT(ip->i_d.di_nlink == nlink); | |
7aab1b28 DE |
691 | ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid()); |
692 | ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid()); | |
6743099c | 693 | xfs_set_projid(ip, prid); |
1da177e4 LT |
694 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
695 | ||
696 | /* | |
697 | * If the superblock version is up to where we support new format | |
698 | * inodes and this is currently an old format inode, then change | |
699 | * the inode version number now. This way we only do the conversion | |
700 | * here rather than here and in the flush/logging code. | |
701 | */ | |
93848a99 | 702 | if (xfs_sb_version_hasnlink(&mp->m_sb) && |
51ce16d5 CH |
703 | ip->i_d.di_version == 1) { |
704 | ip->i_d.di_version = 2; | |
1da177e4 LT |
705 | /* |
706 | * We've already zeroed the old link count, the projid field, | |
707 | * and the pad field. | |
708 | */ | |
709 | } | |
710 | ||
711 | /* | |
712 | * Project ids won't be stored on disk if we are using a version 1 inode. | |
713 | */ | |
51ce16d5 | 714 | if ((prid != 0) && (ip->i_d.di_version == 1)) |
1da177e4 LT |
715 | xfs_bump_ino_vers2(tp, ip); |
716 | ||
bd186aa9 | 717 | if (pip && XFS_INHERIT_GID(pip)) { |
1da177e4 | 718 | ip->i_d.di_gid = pip->i_d.di_gid; |
abbede1b | 719 | if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) { |
1da177e4 LT |
720 | ip->i_d.di_mode |= S_ISGID; |
721 | } | |
722 | } | |
723 | ||
724 | /* | |
725 | * If the group ID of the new file does not match the effective group | |
726 | * ID or one of the supplementary group IDs, the S_ISGID bit is cleared | |
727 | * (and only if the irix_sgid_inherit compatibility variable is set). | |
728 | */ | |
729 | if ((irix_sgid_inherit) && | |
730 | (ip->i_d.di_mode & S_ISGID) && | |
7aab1b28 | 731 | (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) { |
1da177e4 LT |
732 | ip->i_d.di_mode &= ~S_ISGID; |
733 | } | |
734 | ||
735 | ip->i_d.di_size = 0; | |
736 | ip->i_d.di_nextents = 0; | |
737 | ASSERT(ip->i_d.di_nblocks == 0); | |
dff35fd4 CH |
738 | |
739 | nanotime(&tv); | |
740 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; | |
741 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; | |
742 | ip->i_d.di_atime = ip->i_d.di_mtime; | |
743 | ip->i_d.di_ctime = ip->i_d.di_mtime; | |
744 | ||
1da177e4 LT |
745 | /* |
746 | * di_gen will have been taken care of in xfs_iread. | |
747 | */ | |
748 | ip->i_d.di_extsize = 0; | |
749 | ip->i_d.di_dmevmask = 0; | |
750 | ip->i_d.di_dmstate = 0; | |
751 | ip->i_d.di_flags = 0; | |
93848a99 CH |
752 | |
753 | if (ip->i_d.di_version == 3) { | |
754 | ASSERT(ip->i_d.di_ino == ino); | |
755 | ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid)); | |
756 | ip->i_d.di_crc = 0; | |
757 | ip->i_d.di_changecount = 1; | |
758 | ip->i_d.di_lsn = 0; | |
759 | ip->i_d.di_flags2 = 0; | |
760 | memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2)); | |
761 | ip->i_d.di_crtime = ip->i_d.di_mtime; | |
762 | } | |
763 | ||
764 | ||
1da177e4 LT |
765 | flags = XFS_ILOG_CORE; |
766 | switch (mode & S_IFMT) { | |
767 | case S_IFIFO: | |
768 | case S_IFCHR: | |
769 | case S_IFBLK: | |
770 | case S_IFSOCK: | |
771 | ip->i_d.di_format = XFS_DINODE_FMT_DEV; | |
772 | ip->i_df.if_u2.if_rdev = rdev; | |
773 | ip->i_df.if_flags = 0; | |
774 | flags |= XFS_ILOG_DEV; | |
775 | break; | |
776 | case S_IFREG: | |
bf904248 DC |
777 | /* |
778 | * we can't set up filestreams until after the VFS inode | |
779 | * is set up properly. | |
780 | */ | |
781 | if (pip && xfs_inode_is_filestream(pip)) | |
782 | filestreams = 1; | |
2a82b8be | 783 | /* fall through */ |
1da177e4 | 784 | case S_IFDIR: |
b11f94d5 | 785 | if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { |
365ca83d NS |
786 | uint di_flags = 0; |
787 | ||
abbede1b | 788 | if (S_ISDIR(mode)) { |
365ca83d NS |
789 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
790 | di_flags |= XFS_DIFLAG_RTINHERIT; | |
dd9f438e NS |
791 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
792 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; | |
793 | ip->i_d.di_extsize = pip->i_d.di_extsize; | |
794 | } | |
abbede1b | 795 | } else if (S_ISREG(mode)) { |
613d7043 | 796 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
365ca83d | 797 | di_flags |= XFS_DIFLAG_REALTIME; |
dd9f438e NS |
798 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
799 | di_flags |= XFS_DIFLAG_EXTSIZE; | |
800 | ip->i_d.di_extsize = pip->i_d.di_extsize; | |
801 | } | |
1da177e4 LT |
802 | } |
803 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) && | |
804 | xfs_inherit_noatime) | |
365ca83d | 805 | di_flags |= XFS_DIFLAG_NOATIME; |
1da177e4 LT |
806 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) && |
807 | xfs_inherit_nodump) | |
365ca83d | 808 | di_flags |= XFS_DIFLAG_NODUMP; |
1da177e4 LT |
809 | if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) && |
810 | xfs_inherit_sync) | |
365ca83d | 811 | di_flags |= XFS_DIFLAG_SYNC; |
1da177e4 LT |
812 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) && |
813 | xfs_inherit_nosymlinks) | |
365ca83d NS |
814 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
815 | if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) | |
816 | di_flags |= XFS_DIFLAG_PROJINHERIT; | |
d3446eac BN |
817 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) && |
818 | xfs_inherit_nodefrag) | |
819 | di_flags |= XFS_DIFLAG_NODEFRAG; | |
2a82b8be DC |
820 | if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM) |
821 | di_flags |= XFS_DIFLAG_FILESTREAM; | |
365ca83d | 822 | ip->i_d.di_flags |= di_flags; |
1da177e4 LT |
823 | } |
824 | /* FALLTHROUGH */ | |
825 | case S_IFLNK: | |
826 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; | |
827 | ip->i_df.if_flags = XFS_IFEXTENTS; | |
828 | ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0; | |
829 | ip->i_df.if_u1.if_extents = NULL; | |
830 | break; | |
831 | default: | |
832 | ASSERT(0); | |
833 | } | |
834 | /* | |
835 | * Attribute fork settings for new inode. | |
836 | */ | |
837 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
838 | ip->i_d.di_anextents = 0; | |
839 | ||
840 | /* | |
841 | * Log the new values stuffed into the inode. | |
842 | */ | |
ddc3415a | 843 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
1da177e4 LT |
844 | xfs_trans_log_inode(tp, ip, flags); |
845 | ||
b83bd138 | 846 | /* now that we have an i_mode we can setup inode ops and unlock */ |
41be8bed | 847 | xfs_setup_inode(ip); |
1da177e4 | 848 | |
bf904248 DC |
849 | /* now we have set up the vfs inode we can associate the filestream */ |
850 | if (filestreams) { | |
851 | error = xfs_filestream_associate(pip, ip); | |
852 | if (error < 0) | |
853 | return -error; | |
854 | if (!error) | |
855 | xfs_iflags_set(ip, XFS_IFILESTREAM); | |
856 | } | |
857 | ||
1da177e4 LT |
858 | *ipp = ip; |
859 | return 0; | |
860 | } | |
861 | ||
e546cb79 DC |
862 | /* |
863 | * Allocates a new inode from disk and return a pointer to the | |
864 | * incore copy. This routine will internally commit the current | |
865 | * transaction and allocate a new one if the Space Manager needed | |
866 | * to do an allocation to replenish the inode free-list. | |
867 | * | |
868 | * This routine is designed to be called from xfs_create and | |
869 | * xfs_create_dir. | |
870 | * | |
871 | */ | |
872 | int | |
873 | xfs_dir_ialloc( | |
874 | xfs_trans_t **tpp, /* input: current transaction; | |
875 | output: may be a new transaction. */ | |
876 | xfs_inode_t *dp, /* directory within whose allocate | |
877 | the inode. */ | |
878 | umode_t mode, | |
879 | xfs_nlink_t nlink, | |
880 | xfs_dev_t rdev, | |
881 | prid_t prid, /* project id */ | |
882 | int okalloc, /* ok to allocate new space */ | |
883 | xfs_inode_t **ipp, /* pointer to inode; it will be | |
884 | locked. */ | |
885 | int *committed) | |
886 | ||
887 | { | |
888 | xfs_trans_t *tp; | |
889 | xfs_trans_t *ntp; | |
890 | xfs_inode_t *ip; | |
891 | xfs_buf_t *ialloc_context = NULL; | |
892 | int code; | |
e546cb79 DC |
893 | void *dqinfo; |
894 | uint tflags; | |
895 | ||
896 | tp = *tpp; | |
897 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); | |
898 | ||
899 | /* | |
900 | * xfs_ialloc will return a pointer to an incore inode if | |
901 | * the Space Manager has an available inode on the free | |
902 | * list. Otherwise, it will do an allocation and replenish | |
903 | * the freelist. Since we can only do one allocation per | |
904 | * transaction without deadlocks, we will need to commit the | |
905 | * current transaction and start a new one. We will then | |
906 | * need to call xfs_ialloc again to get the inode. | |
907 | * | |
908 | * If xfs_ialloc did an allocation to replenish the freelist, | |
909 | * it returns the bp containing the head of the freelist as | |
910 | * ialloc_context. We will hold a lock on it across the | |
911 | * transaction commit so that no other process can steal | |
912 | * the inode(s) that we've just allocated. | |
913 | */ | |
914 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc, | |
915 | &ialloc_context, &ip); | |
916 | ||
917 | /* | |
918 | * Return an error if we were unable to allocate a new inode. | |
919 | * This should only happen if we run out of space on disk or | |
920 | * encounter a disk error. | |
921 | */ | |
922 | if (code) { | |
923 | *ipp = NULL; | |
924 | return code; | |
925 | } | |
926 | if (!ialloc_context && !ip) { | |
927 | *ipp = NULL; | |
928 | return XFS_ERROR(ENOSPC); | |
929 | } | |
930 | ||
931 | /* | |
932 | * If the AGI buffer is non-NULL, then we were unable to get an | |
933 | * inode in one operation. We need to commit the current | |
934 | * transaction and call xfs_ialloc() again. It is guaranteed | |
935 | * to succeed the second time. | |
936 | */ | |
937 | if (ialloc_context) { | |
3d3c8b52 JL |
938 | struct xfs_trans_res tres; |
939 | ||
e546cb79 DC |
940 | /* |
941 | * Normally, xfs_trans_commit releases all the locks. | |
942 | * We call bhold to hang on to the ialloc_context across | |
943 | * the commit. Holding this buffer prevents any other | |
944 | * processes from doing any allocations in this | |
945 | * allocation group. | |
946 | */ | |
947 | xfs_trans_bhold(tp, ialloc_context); | |
948 | /* | |
949 | * Save the log reservation so we can use | |
950 | * them in the next transaction. | |
951 | */ | |
3d3c8b52 JL |
952 | tres.tr_logres = xfs_trans_get_log_res(tp); |
953 | tres.tr_logcount = xfs_trans_get_log_count(tp); | |
e546cb79 DC |
954 | |
955 | /* | |
956 | * We want the quota changes to be associated with the next | |
957 | * transaction, NOT this one. So, detach the dqinfo from this | |
958 | * and attach it to the next transaction. | |
959 | */ | |
960 | dqinfo = NULL; | |
961 | tflags = 0; | |
962 | if (tp->t_dqinfo) { | |
963 | dqinfo = (void *)tp->t_dqinfo; | |
964 | tp->t_dqinfo = NULL; | |
965 | tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY; | |
966 | tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY); | |
967 | } | |
968 | ||
969 | ntp = xfs_trans_dup(tp); | |
970 | code = xfs_trans_commit(tp, 0); | |
971 | tp = ntp; | |
972 | if (committed != NULL) { | |
973 | *committed = 1; | |
974 | } | |
975 | /* | |
976 | * If we get an error during the commit processing, | |
977 | * release the buffer that is still held and return | |
978 | * to the caller. | |
979 | */ | |
980 | if (code) { | |
981 | xfs_buf_relse(ialloc_context); | |
982 | if (dqinfo) { | |
983 | tp->t_dqinfo = dqinfo; | |
984 | xfs_trans_free_dqinfo(tp); | |
985 | } | |
986 | *tpp = ntp; | |
987 | *ipp = NULL; | |
988 | return code; | |
989 | } | |
990 | ||
991 | /* | |
992 | * transaction commit worked ok so we can drop the extra ticket | |
993 | * reference that we gained in xfs_trans_dup() | |
994 | */ | |
995 | xfs_log_ticket_put(tp->t_ticket); | |
3d3c8b52 JL |
996 | tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; |
997 | code = xfs_trans_reserve(tp, &tres, 0, 0); | |
998 | ||
e546cb79 DC |
999 | /* |
1000 | * Re-attach the quota info that we detached from prev trx. | |
1001 | */ | |
1002 | if (dqinfo) { | |
1003 | tp->t_dqinfo = dqinfo; | |
1004 | tp->t_flags |= tflags; | |
1005 | } | |
1006 | ||
1007 | if (code) { | |
1008 | xfs_buf_relse(ialloc_context); | |
1009 | *tpp = ntp; | |
1010 | *ipp = NULL; | |
1011 | return code; | |
1012 | } | |
1013 | xfs_trans_bjoin(tp, ialloc_context); | |
1014 | ||
1015 | /* | |
1016 | * Call ialloc again. Since we've locked out all | |
1017 | * other allocations in this allocation group, | |
1018 | * this call should always succeed. | |
1019 | */ | |
1020 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, | |
1021 | okalloc, &ialloc_context, &ip); | |
1022 | ||
1023 | /* | |
1024 | * If we get an error at this point, return to the caller | |
1025 | * so that the current transaction can be aborted. | |
1026 | */ | |
1027 | if (code) { | |
1028 | *tpp = tp; | |
1029 | *ipp = NULL; | |
1030 | return code; | |
1031 | } | |
1032 | ASSERT(!ialloc_context && ip); | |
1033 | ||
1034 | } else { | |
1035 | if (committed != NULL) | |
1036 | *committed = 0; | |
1037 | } | |
1038 | ||
1039 | *ipp = ip; | |
1040 | *tpp = tp; | |
1041 | ||
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | /* | |
1046 | * Decrement the link count on an inode & log the change. | |
1047 | * If this causes the link count to go to zero, initiate the | |
1048 | * logging activity required to truncate a file. | |
1049 | */ | |
1050 | int /* error */ | |
1051 | xfs_droplink( | |
1052 | xfs_trans_t *tp, | |
1053 | xfs_inode_t *ip) | |
1054 | { | |
1055 | int error; | |
1056 | ||
1057 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); | |
1058 | ||
1059 | ASSERT (ip->i_d.di_nlink > 0); | |
1060 | ip->i_d.di_nlink--; | |
1061 | drop_nlink(VFS_I(ip)); | |
1062 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1063 | ||
1064 | error = 0; | |
1065 | if (ip->i_d.di_nlink == 0) { | |
1066 | /* | |
1067 | * We're dropping the last link to this file. | |
1068 | * Move the on-disk inode to the AGI unlinked list. | |
1069 | * From xfs_inactive() we will pull the inode from | |
1070 | * the list and free it. | |
1071 | */ | |
1072 | error = xfs_iunlink(tp, ip); | |
1073 | } | |
1074 | return error; | |
1075 | } | |
1076 | ||
1077 | /* | |
1078 | * This gets called when the inode's version needs to be changed from 1 to 2. | |
1079 | * Currently this happens when the nlink field overflows the old 16-bit value | |
1080 | * or when chproj is called to change the project for the first time. | |
1081 | * As a side effect the superblock version will also get rev'd | |
1082 | * to contain the NLINK bit. | |
1083 | */ | |
1084 | void | |
1085 | xfs_bump_ino_vers2( | |
1086 | xfs_trans_t *tp, | |
1087 | xfs_inode_t *ip) | |
1088 | { | |
1089 | xfs_mount_t *mp; | |
1090 | ||
1091 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | |
1092 | ASSERT(ip->i_d.di_version == 1); | |
1093 | ||
1094 | ip->i_d.di_version = 2; | |
1095 | ip->i_d.di_onlink = 0; | |
1096 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); | |
1097 | mp = tp->t_mountp; | |
1098 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { | |
1099 | spin_lock(&mp->m_sb_lock); | |
1100 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { | |
1101 | xfs_sb_version_addnlink(&mp->m_sb); | |
1102 | spin_unlock(&mp->m_sb_lock); | |
1103 | xfs_mod_sb(tp, XFS_SB_VERSIONNUM); | |
1104 | } else { | |
1105 | spin_unlock(&mp->m_sb_lock); | |
1106 | } | |
1107 | } | |
1108 | /* Caller must log the inode */ | |
1109 | } | |
1110 | ||
1111 | /* | |
1112 | * Increment the link count on an inode & log the change. | |
1113 | */ | |
1114 | int | |
1115 | xfs_bumplink( | |
1116 | xfs_trans_t *tp, | |
1117 | xfs_inode_t *ip) | |
1118 | { | |
1119 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); | |
1120 | ||
1121 | ASSERT(ip->i_d.di_nlink > 0); | |
1122 | ip->i_d.di_nlink++; | |
1123 | inc_nlink(VFS_I(ip)); | |
1124 | if ((ip->i_d.di_version == 1) && | |
1125 | (ip->i_d.di_nlink > XFS_MAXLINK_1)) { | |
1126 | /* | |
1127 | * The inode has increased its number of links beyond | |
1128 | * what can fit in an old format inode. It now needs | |
1129 | * to be converted to a version 2 inode with a 32 bit | |
1130 | * link count. If this is the first inode in the file | |
1131 | * system to do this, then we need to bump the superblock | |
1132 | * version number as well. | |
1133 | */ | |
1134 | xfs_bump_ino_vers2(tp, ip); | |
1135 | } | |
1136 | ||
1137 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1138 | return 0; | |
1139 | } | |
1140 | ||
c24b5dfa DC |
1141 | int |
1142 | xfs_create( | |
1143 | xfs_inode_t *dp, | |
1144 | struct xfs_name *name, | |
1145 | umode_t mode, | |
1146 | xfs_dev_t rdev, | |
1147 | xfs_inode_t **ipp) | |
1148 | { | |
1149 | int is_dir = S_ISDIR(mode); | |
1150 | struct xfs_mount *mp = dp->i_mount; | |
1151 | struct xfs_inode *ip = NULL; | |
1152 | struct xfs_trans *tp = NULL; | |
1153 | int error; | |
1154 | xfs_bmap_free_t free_list; | |
1155 | xfs_fsblock_t first_block; | |
1156 | bool unlock_dp_on_error = false; | |
1157 | uint cancel_flags; | |
1158 | int committed; | |
1159 | prid_t prid; | |
1160 | struct xfs_dquot *udqp = NULL; | |
1161 | struct xfs_dquot *gdqp = NULL; | |
1162 | struct xfs_dquot *pdqp = NULL; | |
3d3c8b52 | 1163 | struct xfs_trans_res tres; |
c24b5dfa | 1164 | uint resblks; |
c24b5dfa DC |
1165 | |
1166 | trace_xfs_create(dp, name); | |
1167 | ||
1168 | if (XFS_FORCED_SHUTDOWN(mp)) | |
1169 | return XFS_ERROR(EIO); | |
1170 | ||
163467d3 | 1171 | prid = xfs_get_initial_prid(dp); |
c24b5dfa DC |
1172 | |
1173 | /* | |
1174 | * Make sure that we have allocated dquot(s) on disk. | |
1175 | */ | |
7aab1b28 DE |
1176 | error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()), |
1177 | xfs_kgid_to_gid(current_fsgid()), prid, | |
c24b5dfa DC |
1178 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, |
1179 | &udqp, &gdqp, &pdqp); | |
1180 | if (error) | |
1181 | return error; | |
1182 | ||
1183 | if (is_dir) { | |
1184 | rdev = 0; | |
1185 | resblks = XFS_MKDIR_SPACE_RES(mp, name->len); | |
3d3c8b52 JL |
1186 | tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres; |
1187 | tres.tr_logcount = XFS_MKDIR_LOG_COUNT; | |
c24b5dfa DC |
1188 | tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR); |
1189 | } else { | |
1190 | resblks = XFS_CREATE_SPACE_RES(mp, name->len); | |
3d3c8b52 JL |
1191 | tres.tr_logres = M_RES(mp)->tr_create.tr_logres; |
1192 | tres.tr_logcount = XFS_CREATE_LOG_COUNT; | |
c24b5dfa DC |
1193 | tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE); |
1194 | } | |
1195 | ||
1196 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | |
1197 | ||
1198 | /* | |
1199 | * Initially assume that the file does not exist and | |
1200 | * reserve the resources for that case. If that is not | |
1201 | * the case we'll drop the one we have and get a more | |
1202 | * appropriate transaction later. | |
1203 | */ | |
3d3c8b52 JL |
1204 | tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; |
1205 | error = xfs_trans_reserve(tp, &tres, resblks, 0); | |
c24b5dfa DC |
1206 | if (error == ENOSPC) { |
1207 | /* flush outstanding delalloc blocks and retry */ | |
1208 | xfs_flush_inodes(mp); | |
3d3c8b52 | 1209 | error = xfs_trans_reserve(tp, &tres, resblks, 0); |
c24b5dfa DC |
1210 | } |
1211 | if (error == ENOSPC) { | |
1212 | /* No space at all so try a "no-allocation" reservation */ | |
1213 | resblks = 0; | |
3d3c8b52 | 1214 | error = xfs_trans_reserve(tp, &tres, 0, 0); |
c24b5dfa DC |
1215 | } |
1216 | if (error) { | |
1217 | cancel_flags = 0; | |
1218 | goto out_trans_cancel; | |
1219 | } | |
1220 | ||
1221 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); | |
1222 | unlock_dp_on_error = true; | |
1223 | ||
1224 | xfs_bmap_init(&free_list, &first_block); | |
1225 | ||
1226 | /* | |
1227 | * Reserve disk quota and the inode. | |
1228 | */ | |
1229 | error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, | |
1230 | pdqp, resblks, 1, 0); | |
1231 | if (error) | |
1232 | goto out_trans_cancel; | |
1233 | ||
1234 | error = xfs_dir_canenter(tp, dp, name, resblks); | |
1235 | if (error) | |
1236 | goto out_trans_cancel; | |
1237 | ||
1238 | /* | |
1239 | * A newly created regular or special file just has one directory | |
1240 | * entry pointing to them, but a directory also the "." entry | |
1241 | * pointing to itself. | |
1242 | */ | |
1243 | error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, | |
1244 | prid, resblks > 0, &ip, &committed); | |
1245 | if (error) { | |
1246 | if (error == ENOSPC) | |
1247 | goto out_trans_cancel; | |
1248 | goto out_trans_abort; | |
1249 | } | |
1250 | ||
1251 | /* | |
1252 | * Now we join the directory inode to the transaction. We do not do it | |
1253 | * earlier because xfs_dir_ialloc might commit the previous transaction | |
1254 | * (and release all the locks). An error from here on will result in | |
1255 | * the transaction cancel unlocking dp so don't do it explicitly in the | |
1256 | * error path. | |
1257 | */ | |
1258 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); | |
1259 | unlock_dp_on_error = false; | |
1260 | ||
1261 | error = xfs_dir_createname(tp, dp, name, ip->i_ino, | |
1262 | &first_block, &free_list, resblks ? | |
1263 | resblks - XFS_IALLOC_SPACE_RES(mp) : 0); | |
1264 | if (error) { | |
1265 | ASSERT(error != ENOSPC); | |
1266 | goto out_trans_abort; | |
1267 | } | |
1268 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
1269 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
1270 | ||
1271 | if (is_dir) { | |
1272 | error = xfs_dir_init(tp, ip, dp); | |
1273 | if (error) | |
1274 | goto out_bmap_cancel; | |
1275 | ||
1276 | error = xfs_bumplink(tp, dp); | |
1277 | if (error) | |
1278 | goto out_bmap_cancel; | |
1279 | } | |
1280 | ||
1281 | /* | |
1282 | * If this is a synchronous mount, make sure that the | |
1283 | * create transaction goes to disk before returning to | |
1284 | * the user. | |
1285 | */ | |
1286 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) | |
1287 | xfs_trans_set_sync(tp); | |
1288 | ||
1289 | /* | |
1290 | * Attach the dquot(s) to the inodes and modify them incore. | |
1291 | * These ids of the inode couldn't have changed since the new | |
1292 | * inode has been locked ever since it was created. | |
1293 | */ | |
1294 | xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); | |
1295 | ||
1296 | error = xfs_bmap_finish(&tp, &free_list, &committed); | |
1297 | if (error) | |
1298 | goto out_bmap_cancel; | |
1299 | ||
1300 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
1301 | if (error) | |
1302 | goto out_release_inode; | |
1303 | ||
1304 | xfs_qm_dqrele(udqp); | |
1305 | xfs_qm_dqrele(gdqp); | |
1306 | xfs_qm_dqrele(pdqp); | |
1307 | ||
1308 | *ipp = ip; | |
1309 | return 0; | |
1310 | ||
1311 | out_bmap_cancel: | |
1312 | xfs_bmap_cancel(&free_list); | |
1313 | out_trans_abort: | |
1314 | cancel_flags |= XFS_TRANS_ABORT; | |
1315 | out_trans_cancel: | |
1316 | xfs_trans_cancel(tp, cancel_flags); | |
1317 | out_release_inode: | |
1318 | /* | |
1319 | * Wait until after the current transaction is aborted to | |
1320 | * release the inode. This prevents recursive transactions | |
1321 | * and deadlocks from xfs_inactive. | |
1322 | */ | |
1323 | if (ip) | |
1324 | IRELE(ip); | |
1325 | ||
1326 | xfs_qm_dqrele(udqp); | |
1327 | xfs_qm_dqrele(gdqp); | |
1328 | xfs_qm_dqrele(pdqp); | |
1329 | ||
1330 | if (unlock_dp_on_error) | |
1331 | xfs_iunlock(dp, XFS_ILOCK_EXCL); | |
1332 | return error; | |
1333 | } | |
1334 | ||
1335 | int | |
1336 | xfs_link( | |
1337 | xfs_inode_t *tdp, | |
1338 | xfs_inode_t *sip, | |
1339 | struct xfs_name *target_name) | |
1340 | { | |
1341 | xfs_mount_t *mp = tdp->i_mount; | |
1342 | xfs_trans_t *tp; | |
1343 | int error; | |
1344 | xfs_bmap_free_t free_list; | |
1345 | xfs_fsblock_t first_block; | |
1346 | int cancel_flags; | |
1347 | int committed; | |
1348 | int resblks; | |
1349 | ||
1350 | trace_xfs_link(tdp, target_name); | |
1351 | ||
1352 | ASSERT(!S_ISDIR(sip->i_d.di_mode)); | |
1353 | ||
1354 | if (XFS_FORCED_SHUTDOWN(mp)) | |
1355 | return XFS_ERROR(EIO); | |
1356 | ||
1357 | error = xfs_qm_dqattach(sip, 0); | |
1358 | if (error) | |
1359 | goto std_return; | |
1360 | ||
1361 | error = xfs_qm_dqattach(tdp, 0); | |
1362 | if (error) | |
1363 | goto std_return; | |
1364 | ||
1365 | tp = xfs_trans_alloc(mp, XFS_TRANS_LINK); | |
1366 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | |
1367 | resblks = XFS_LINK_SPACE_RES(mp, target_name->len); | |
3d3c8b52 | 1368 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0); |
c24b5dfa DC |
1369 | if (error == ENOSPC) { |
1370 | resblks = 0; | |
3d3c8b52 | 1371 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0); |
c24b5dfa DC |
1372 | } |
1373 | if (error) { | |
1374 | cancel_flags = 0; | |
1375 | goto error_return; | |
1376 | } | |
1377 | ||
1378 | xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL); | |
1379 | ||
1380 | xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL); | |
1381 | xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL); | |
1382 | ||
1383 | /* | |
1384 | * If we are using project inheritance, we only allow hard link | |
1385 | * creation in our tree when the project IDs are the same; else | |
1386 | * the tree quota mechanism could be circumvented. | |
1387 | */ | |
1388 | if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && | |
1389 | (xfs_get_projid(tdp) != xfs_get_projid(sip)))) { | |
1390 | error = XFS_ERROR(EXDEV); | |
1391 | goto error_return; | |
1392 | } | |
1393 | ||
1394 | error = xfs_dir_canenter(tp, tdp, target_name, resblks); | |
1395 | if (error) | |
1396 | goto error_return; | |
1397 | ||
1398 | xfs_bmap_init(&free_list, &first_block); | |
1399 | ||
1400 | error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, | |
1401 | &first_block, &free_list, resblks); | |
1402 | if (error) | |
1403 | goto abort_return; | |
1404 | xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
1405 | xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); | |
1406 | ||
1407 | error = xfs_bumplink(tp, sip); | |
1408 | if (error) | |
1409 | goto abort_return; | |
1410 | ||
1411 | /* | |
1412 | * If this is a synchronous mount, make sure that the | |
1413 | * link transaction goes to disk before returning to | |
1414 | * the user. | |
1415 | */ | |
1416 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { | |
1417 | xfs_trans_set_sync(tp); | |
1418 | } | |
1419 | ||
1420 | error = xfs_bmap_finish (&tp, &free_list, &committed); | |
1421 | if (error) { | |
1422 | xfs_bmap_cancel(&free_list); | |
1423 | goto abort_return; | |
1424 | } | |
1425 | ||
1426 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
1427 | ||
1428 | abort_return: | |
1429 | cancel_flags |= XFS_TRANS_ABORT; | |
1430 | error_return: | |
1431 | xfs_trans_cancel(tp, cancel_flags); | |
1432 | std_return: | |
1433 | return error; | |
1434 | } | |
1435 | ||
1da177e4 | 1436 | /* |
8f04c47a CH |
1437 | * Free up the underlying blocks past new_size. The new size must be smaller |
1438 | * than the current size. This routine can be used both for the attribute and | |
1439 | * data fork, and does not modify the inode size, which is left to the caller. | |
1da177e4 | 1440 | * |
f6485057 DC |
1441 | * The transaction passed to this routine must have made a permanent log |
1442 | * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the | |
1443 | * given transaction and start new ones, so make sure everything involved in | |
1444 | * the transaction is tidy before calling here. Some transaction will be | |
1445 | * returned to the caller to be committed. The incoming transaction must | |
1446 | * already include the inode, and both inode locks must be held exclusively. | |
1447 | * The inode must also be "held" within the transaction. On return the inode | |
1448 | * will be "held" within the returned transaction. This routine does NOT | |
1449 | * require any disk space to be reserved for it within the transaction. | |
1da177e4 | 1450 | * |
f6485057 DC |
1451 | * If we get an error, we must return with the inode locked and linked into the |
1452 | * current transaction. This keeps things simple for the higher level code, | |
1453 | * because it always knows that the inode is locked and held in the transaction | |
1454 | * that returns to it whether errors occur or not. We don't mark the inode | |
1455 | * dirty on error so that transactions can be easily aborted if possible. | |
1da177e4 LT |
1456 | */ |
1457 | int | |
8f04c47a CH |
1458 | xfs_itruncate_extents( |
1459 | struct xfs_trans **tpp, | |
1460 | struct xfs_inode *ip, | |
1461 | int whichfork, | |
1462 | xfs_fsize_t new_size) | |
1da177e4 | 1463 | { |
8f04c47a CH |
1464 | struct xfs_mount *mp = ip->i_mount; |
1465 | struct xfs_trans *tp = *tpp; | |
1466 | struct xfs_trans *ntp; | |
1467 | xfs_bmap_free_t free_list; | |
1468 | xfs_fsblock_t first_block; | |
1469 | xfs_fileoff_t first_unmap_block; | |
1470 | xfs_fileoff_t last_block; | |
1471 | xfs_filblks_t unmap_len; | |
1472 | int committed; | |
1473 | int error = 0; | |
1474 | int done = 0; | |
1da177e4 | 1475 | |
0b56185b CH |
1476 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1477 | ASSERT(!atomic_read(&VFS_I(ip)->i_count) || | |
1478 | xfs_isilocked(ip, XFS_IOLOCK_EXCL)); | |
ce7ae151 | 1479 | ASSERT(new_size <= XFS_ISIZE(ip)); |
8f04c47a | 1480 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
1da177e4 | 1481 | ASSERT(ip->i_itemp != NULL); |
898621d5 | 1482 | ASSERT(ip->i_itemp->ili_lock_flags == 0); |
8f04c47a | 1483 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
1da177e4 | 1484 | |
673e8e59 CH |
1485 | trace_xfs_itruncate_extents_start(ip, new_size); |
1486 | ||
1da177e4 LT |
1487 | /* |
1488 | * Since it is possible for space to become allocated beyond | |
1489 | * the end of the file (in a crash where the space is allocated | |
1490 | * but the inode size is not yet updated), simply remove any | |
1491 | * blocks which show up between the new EOF and the maximum | |
1492 | * possible file size. If the first block to be removed is | |
1493 | * beyond the maximum file size (ie it is the same as last_block), | |
1494 | * then there is nothing to do. | |
1495 | */ | |
8f04c47a | 1496 | first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size); |
32972383 | 1497 | last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); |
8f04c47a CH |
1498 | if (first_unmap_block == last_block) |
1499 | return 0; | |
1500 | ||
1501 | ASSERT(first_unmap_block < last_block); | |
1502 | unmap_len = last_block - first_unmap_block + 1; | |
1da177e4 | 1503 | while (!done) { |
9d87c319 | 1504 | xfs_bmap_init(&free_list, &first_block); |
8f04c47a | 1505 | error = xfs_bunmapi(tp, ip, |
3e57ecf6 | 1506 | first_unmap_block, unmap_len, |
8f04c47a | 1507 | xfs_bmapi_aflag(whichfork), |
1da177e4 | 1508 | XFS_ITRUNC_MAX_EXTENTS, |
3e57ecf6 | 1509 | &first_block, &free_list, |
b4e9181e | 1510 | &done); |
8f04c47a CH |
1511 | if (error) |
1512 | goto out_bmap_cancel; | |
1da177e4 LT |
1513 | |
1514 | /* | |
1515 | * Duplicate the transaction that has the permanent | |
1516 | * reservation and commit the old transaction. | |
1517 | */ | |
8f04c47a | 1518 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
898621d5 | 1519 | if (committed) |
ddc3415a | 1520 | xfs_trans_ijoin(tp, ip, 0); |
8f04c47a CH |
1521 | if (error) |
1522 | goto out_bmap_cancel; | |
1da177e4 LT |
1523 | |
1524 | if (committed) { | |
1525 | /* | |
f6485057 | 1526 | * Mark the inode dirty so it will be logged and |
e5720eec | 1527 | * moved forward in the log as part of every commit. |
1da177e4 | 1528 | */ |
8f04c47a | 1529 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
1da177e4 | 1530 | } |
f6485057 | 1531 | |
8f04c47a CH |
1532 | ntp = xfs_trans_dup(tp); |
1533 | error = xfs_trans_commit(tp, 0); | |
1534 | tp = ntp; | |
e5720eec | 1535 | |
ddc3415a | 1536 | xfs_trans_ijoin(tp, ip, 0); |
f6485057 | 1537 | |
cc09c0dc | 1538 | if (error) |
8f04c47a CH |
1539 | goto out; |
1540 | ||
cc09c0dc | 1541 | /* |
8f04c47a | 1542 | * Transaction commit worked ok so we can drop the extra ticket |
cc09c0dc DC |
1543 | * reference that we gained in xfs_trans_dup() |
1544 | */ | |
8f04c47a | 1545 | xfs_log_ticket_put(tp->t_ticket); |
3d3c8b52 | 1546 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0); |
f6485057 | 1547 | if (error) |
8f04c47a | 1548 | goto out; |
1da177e4 | 1549 | } |
8f04c47a | 1550 | |
673e8e59 CH |
1551 | /* |
1552 | * Always re-log the inode so that our permanent transaction can keep | |
1553 | * on rolling it forward in the log. | |
1554 | */ | |
1555 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1556 | ||
1557 | trace_xfs_itruncate_extents_end(ip, new_size); | |
1558 | ||
8f04c47a CH |
1559 | out: |
1560 | *tpp = tp; | |
1561 | return error; | |
1562 | out_bmap_cancel: | |
1da177e4 | 1563 | /* |
8f04c47a CH |
1564 | * If the bunmapi call encounters an error, return to the caller where |
1565 | * the transaction can be properly aborted. We just need to make sure | |
1566 | * we're not holding any resources that we were not when we came in. | |
1da177e4 | 1567 | */ |
8f04c47a CH |
1568 | xfs_bmap_cancel(&free_list); |
1569 | goto out; | |
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 | ||
1579 | if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0)) | |
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 | ||
1589 | /* | |
1590 | * If we are using filestreams, and we have an unlinked | |
1591 | * file that we are processing the last close on, then nothing | |
1592 | * will be able to reopen and write to this file. Purge this | |
1593 | * inode from the filestreams cache so that it doesn't delay | |
1594 | * teardown of the inode. | |
1595 | */ | |
1596 | if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip)) | |
1597 | xfs_filestream_deassociate(ip); | |
1598 | ||
1599 | /* | |
1600 | * If we previously truncated this file and removed old data | |
1601 | * in the process, we want to initiate "early" writeout on | |
1602 | * the last close. This is an attempt to combat the notorious | |
1603 | * NULL files problem which is particularly noticeable from a | |
1604 | * truncate down, buffered (re-)write (delalloc), followed by | |
1605 | * a crash. What we are effectively doing here is | |
1606 | * significantly reducing the time window where we'd otherwise | |
1607 | * be exposed to that problem. | |
1608 | */ | |
1609 | truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); | |
1610 | if (truncated) { | |
1611 | xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE); | |
1612 | if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) { | |
1613 | error = -filemap_flush(VFS_I(ip)->i_mapping); | |
1614 | if (error) | |
1615 | return error; | |
1616 | } | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | if (ip->i_d.di_nlink == 0) | |
1621 | return 0; | |
1622 | ||
1623 | if (xfs_can_free_eofblocks(ip, false)) { | |
1624 | ||
1625 | /* | |
1626 | * If we can't get the iolock just skip truncating the blocks | |
1627 | * past EOF because we could deadlock with the mmap_sem | |
1628 | * otherwise. We'll get another chance to drop them once the | |
1629 | * last reference to the inode is dropped, so we'll never leak | |
1630 | * blocks permanently. | |
1631 | * | |
1632 | * Further, check if the inode is being opened, written and | |
1633 | * closed frequently and we have delayed allocation blocks | |
1634 | * outstanding (e.g. streaming writes from the NFS server), | |
1635 | * truncating the blocks past EOF will cause fragmentation to | |
1636 | * occur. | |
1637 | * | |
1638 | * In this case don't do the truncation, either, but we have to | |
1639 | * be careful how we detect this case. Blocks beyond EOF show | |
1640 | * up as i_delayed_blks even when the inode is clean, so we | |
1641 | * need to truncate them away first before checking for a dirty | |
1642 | * release. Hence on the first dirty close we will still remove | |
1643 | * the speculative allocation, but after that we will leave it | |
1644 | * in place. | |
1645 | */ | |
1646 | if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE)) | |
1647 | return 0; | |
1648 | ||
1649 | error = xfs_free_eofblocks(mp, ip, true); | |
1650 | if (error && error != EAGAIN) | |
1651 | return error; | |
1652 | ||
1653 | /* delalloc blocks after truncation means it really is dirty */ | |
1654 | if (ip->i_delayed_blks) | |
1655 | xfs_iflags_set(ip, XFS_IDIRTY_RELEASE); | |
1656 | } | |
1657 | return 0; | |
1658 | } | |
1659 | ||
f7be2d7f BF |
1660 | /* |
1661 | * xfs_inactive_truncate | |
1662 | * | |
1663 | * Called to perform a truncate when an inode becomes unlinked. | |
1664 | */ | |
1665 | STATIC int | |
1666 | xfs_inactive_truncate( | |
1667 | struct xfs_inode *ip) | |
1668 | { | |
1669 | struct xfs_mount *mp = ip->i_mount; | |
1670 | struct xfs_trans *tp; | |
1671 | int error; | |
1672 | ||
1673 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); | |
1674 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0); | |
1675 | if (error) { | |
1676 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); | |
1677 | xfs_trans_cancel(tp, 0); | |
1678 | return error; | |
1679 | } | |
1680 | ||
1681 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
1682 | xfs_trans_ijoin(tp, ip, 0); | |
1683 | ||
1684 | /* | |
1685 | * Log the inode size first to prevent stale data exposure in the event | |
1686 | * of a system crash before the truncate completes. See the related | |
1687 | * comment in xfs_setattr_size() for details. | |
1688 | */ | |
1689 | ip->i_d.di_size = 0; | |
1690 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1691 | ||
1692 | error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0); | |
1693 | if (error) | |
1694 | goto error_trans_cancel; | |
1695 | ||
1696 | ASSERT(ip->i_d.di_nextents == 0); | |
1697 | ||
1698 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
1699 | if (error) | |
1700 | goto error_unlock; | |
1701 | ||
1702 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1703 | return 0; | |
1704 | ||
1705 | error_trans_cancel: | |
1706 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); | |
1707 | error_unlock: | |
1708 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1709 | return error; | |
1710 | } | |
1711 | ||
88877d2b BF |
1712 | /* |
1713 | * xfs_inactive_ifree() | |
1714 | * | |
1715 | * Perform the inode free when an inode is unlinked. | |
1716 | */ | |
1717 | STATIC int | |
1718 | xfs_inactive_ifree( | |
1719 | struct xfs_inode *ip) | |
1720 | { | |
1721 | xfs_bmap_free_t free_list; | |
1722 | xfs_fsblock_t first_block; | |
1723 | int committed; | |
1724 | struct xfs_mount *mp = ip->i_mount; | |
1725 | struct xfs_trans *tp; | |
1726 | int error; | |
1727 | ||
1728 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); | |
1729 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree, 0, 0); | |
1730 | if (error) { | |
1731 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); | |
1732 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); | |
1733 | return error; | |
1734 | } | |
1735 | ||
1736 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
1737 | xfs_trans_ijoin(tp, ip, 0); | |
1738 | ||
1739 | xfs_bmap_init(&free_list, &first_block); | |
1740 | error = xfs_ifree(tp, ip, &free_list); | |
1741 | if (error) { | |
1742 | /* | |
1743 | * If we fail to free the inode, shut down. The cancel | |
1744 | * might do that, we need to make sure. Otherwise the | |
1745 | * inode might be lost for a long time or forever. | |
1746 | */ | |
1747 | if (!XFS_FORCED_SHUTDOWN(mp)) { | |
1748 | xfs_notice(mp, "%s: xfs_ifree returned error %d", | |
1749 | __func__, error); | |
1750 | xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); | |
1751 | } | |
1752 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); | |
1753 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1754 | return error; | |
1755 | } | |
1756 | ||
1757 | /* | |
1758 | * Credit the quota account(s). The inode is gone. | |
1759 | */ | |
1760 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1); | |
1761 | ||
1762 | /* | |
1763 | * Just ignore errors at this point. There is nothing we can | |
1764 | * do except to try to keep going. Make sure it's not a silent | |
1765 | * error. | |
1766 | */ | |
1767 | error = xfs_bmap_finish(&tp, &free_list, &committed); | |
1768 | if (error) | |
1769 | xfs_notice(mp, "%s: xfs_bmap_finish returned error %d", | |
1770 | __func__, error); | |
1771 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
1772 | if (error) | |
1773 | xfs_notice(mp, "%s: xfs_trans_commit returned error %d", | |
1774 | __func__, error); | |
1775 | ||
1776 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1777 | return 0; | |
1778 | } | |
1779 | ||
c24b5dfa DC |
1780 | /* |
1781 | * xfs_inactive | |
1782 | * | |
1783 | * This is called when the vnode reference count for the vnode | |
1784 | * goes to zero. If the file has been unlinked, then it must | |
1785 | * now be truncated. Also, we clear all of the read-ahead state | |
1786 | * kept for the inode here since the file is now closed. | |
1787 | */ | |
74564fb4 | 1788 | void |
c24b5dfa DC |
1789 | xfs_inactive( |
1790 | xfs_inode_t *ip) | |
1791 | { | |
3d3c8b52 | 1792 | struct xfs_mount *mp; |
3d3c8b52 JL |
1793 | int error; |
1794 | int truncate = 0; | |
c24b5dfa DC |
1795 | |
1796 | /* | |
1797 | * If the inode is already free, then there can be nothing | |
1798 | * to clean up here. | |
1799 | */ | |
d948709b | 1800 | if (ip->i_d.di_mode == 0) { |
c24b5dfa DC |
1801 | ASSERT(ip->i_df.if_real_bytes == 0); |
1802 | ASSERT(ip->i_df.if_broot_bytes == 0); | |
74564fb4 | 1803 | return; |
c24b5dfa DC |
1804 | } |
1805 | ||
1806 | mp = ip->i_mount; | |
1807 | ||
c24b5dfa DC |
1808 | /* If this is a read-only mount, don't do this (would generate I/O) */ |
1809 | if (mp->m_flags & XFS_MOUNT_RDONLY) | |
74564fb4 | 1810 | return; |
c24b5dfa DC |
1811 | |
1812 | if (ip->i_d.di_nlink != 0) { | |
1813 | /* | |
1814 | * force is true because we are evicting an inode from the | |
1815 | * cache. Post-eof blocks must be freed, lest we end up with | |
1816 | * broken free space accounting. | |
1817 | */ | |
74564fb4 BF |
1818 | if (xfs_can_free_eofblocks(ip, true)) |
1819 | xfs_free_eofblocks(mp, ip, false); | |
1820 | ||
1821 | return; | |
c24b5dfa DC |
1822 | } |
1823 | ||
1824 | if (S_ISREG(ip->i_d.di_mode) && | |
1825 | (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 || | |
1826 | ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0)) | |
1827 | truncate = 1; | |
1828 | ||
1829 | error = xfs_qm_dqattach(ip, 0); | |
1830 | if (error) | |
74564fb4 | 1831 | return; |
c24b5dfa | 1832 | |
f7be2d7f | 1833 | if (S_ISLNK(ip->i_d.di_mode)) |
36b21dde | 1834 | error = xfs_inactive_symlink(ip); |
f7be2d7f BF |
1835 | else if (truncate) |
1836 | error = xfs_inactive_truncate(ip); | |
1837 | if (error) | |
74564fb4 | 1838 | return; |
c24b5dfa DC |
1839 | |
1840 | /* | |
1841 | * If there are attributes associated with the file then blow them away | |
1842 | * now. The code calls a routine that recursively deconstructs the | |
1843 | * attribute fork. We need to just commit the current transaction | |
1844 | * because we can't use it for xfs_attr_inactive(). | |
1845 | */ | |
1846 | if (ip->i_d.di_anextents > 0) { | |
1847 | ASSERT(ip->i_d.di_forkoff != 0); | |
1848 | ||
c24b5dfa DC |
1849 | error = xfs_attr_inactive(ip); |
1850 | if (error) | |
74564fb4 | 1851 | return; |
c24b5dfa DC |
1852 | } |
1853 | ||
1854 | if (ip->i_afp) | |
1855 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); | |
1856 | ||
1857 | ASSERT(ip->i_d.di_anextents == 0); | |
1858 | ||
1859 | /* | |
1860 | * Free the inode. | |
1861 | */ | |
88877d2b BF |
1862 | error = xfs_inactive_ifree(ip); |
1863 | if (error) | |
74564fb4 | 1864 | return; |
c24b5dfa DC |
1865 | |
1866 | /* | |
1867 | * Release the dquots held by inode, if any. | |
1868 | */ | |
1869 | xfs_qm_dqdetach(ip); | |
c24b5dfa DC |
1870 | } |
1871 | ||
1da177e4 LT |
1872 | /* |
1873 | * This is called when the inode's link count goes to 0. | |
1874 | * We place the on-disk inode on a list in the AGI. It | |
1875 | * will be pulled from this list when the inode is freed. | |
1876 | */ | |
1877 | int | |
1878 | xfs_iunlink( | |
1879 | xfs_trans_t *tp, | |
1880 | xfs_inode_t *ip) | |
1881 | { | |
1882 | xfs_mount_t *mp; | |
1883 | xfs_agi_t *agi; | |
1884 | xfs_dinode_t *dip; | |
1885 | xfs_buf_t *agibp; | |
1886 | xfs_buf_t *ibp; | |
1da177e4 LT |
1887 | xfs_agino_t agino; |
1888 | short bucket_index; | |
1889 | int offset; | |
1890 | int error; | |
1da177e4 LT |
1891 | |
1892 | ASSERT(ip->i_d.di_nlink == 0); | |
1893 | ASSERT(ip->i_d.di_mode != 0); | |
1da177e4 LT |
1894 | |
1895 | mp = tp->t_mountp; | |
1896 | ||
1da177e4 LT |
1897 | /* |
1898 | * Get the agi buffer first. It ensures lock ordering | |
1899 | * on the list. | |
1900 | */ | |
5e1be0fb | 1901 | error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp); |
859d7182 | 1902 | if (error) |
1da177e4 | 1903 | return error; |
1da177e4 | 1904 | agi = XFS_BUF_TO_AGI(agibp); |
5e1be0fb | 1905 | |
1da177e4 LT |
1906 | /* |
1907 | * Get the index into the agi hash table for the | |
1908 | * list this inode will go on. | |
1909 | */ | |
1910 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); | |
1911 | ASSERT(agino != 0); | |
1912 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; | |
1913 | ASSERT(agi->agi_unlinked[bucket_index]); | |
16259e7d | 1914 | ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino); |
1da177e4 | 1915 | |
69ef921b | 1916 | if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) { |
1da177e4 LT |
1917 | /* |
1918 | * There is already another inode in the bucket we need | |
1919 | * to add ourselves to. Add us at the front of the list. | |
1920 | * Here we put the head pointer into our next pointer, | |
1921 | * and then we fall through to point the head at us. | |
1922 | */ | |
475ee413 CH |
1923 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
1924 | 0, 0); | |
c319b58b VA |
1925 | if (error) |
1926 | return error; | |
1927 | ||
69ef921b | 1928 | ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO)); |
1da177e4 | 1929 | dip->di_next_unlinked = agi->agi_unlinked[bucket_index]; |
92bfc6e7 | 1930 | offset = ip->i_imap.im_boffset + |
1da177e4 | 1931 | offsetof(xfs_dinode_t, di_next_unlinked); |
0a32c26e DC |
1932 | |
1933 | /* need to recalc the inode CRC if appropriate */ | |
1934 | xfs_dinode_calc_crc(mp, dip); | |
1935 | ||
1da177e4 LT |
1936 | xfs_trans_inode_buf(tp, ibp); |
1937 | xfs_trans_log_buf(tp, ibp, offset, | |
1938 | (offset + sizeof(xfs_agino_t) - 1)); | |
1939 | xfs_inobp_check(mp, ibp); | |
1940 | } | |
1941 | ||
1942 | /* | |
1943 | * Point the bucket head pointer at the inode being inserted. | |
1944 | */ | |
1945 | ASSERT(agino != 0); | |
16259e7d | 1946 | agi->agi_unlinked[bucket_index] = cpu_to_be32(agino); |
1da177e4 LT |
1947 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
1948 | (sizeof(xfs_agino_t) * bucket_index); | |
1949 | xfs_trans_log_buf(tp, agibp, offset, | |
1950 | (offset + sizeof(xfs_agino_t) - 1)); | |
1951 | return 0; | |
1952 | } | |
1953 | ||
1954 | /* | |
1955 | * Pull the on-disk inode from the AGI unlinked list. | |
1956 | */ | |
1957 | STATIC int | |
1958 | xfs_iunlink_remove( | |
1959 | xfs_trans_t *tp, | |
1960 | xfs_inode_t *ip) | |
1961 | { | |
1962 | xfs_ino_t next_ino; | |
1963 | xfs_mount_t *mp; | |
1964 | xfs_agi_t *agi; | |
1965 | xfs_dinode_t *dip; | |
1966 | xfs_buf_t *agibp; | |
1967 | xfs_buf_t *ibp; | |
1968 | xfs_agnumber_t agno; | |
1da177e4 LT |
1969 | xfs_agino_t agino; |
1970 | xfs_agino_t next_agino; | |
1971 | xfs_buf_t *last_ibp; | |
6fdf8ccc | 1972 | xfs_dinode_t *last_dip = NULL; |
1da177e4 | 1973 | short bucket_index; |
6fdf8ccc | 1974 | int offset, last_offset = 0; |
1da177e4 | 1975 | int error; |
1da177e4 | 1976 | |
1da177e4 | 1977 | mp = tp->t_mountp; |
1da177e4 | 1978 | agno = XFS_INO_TO_AGNO(mp, ip->i_ino); |
1da177e4 LT |
1979 | |
1980 | /* | |
1981 | * Get the agi buffer first. It ensures lock ordering | |
1982 | * on the list. | |
1983 | */ | |
5e1be0fb CH |
1984 | error = xfs_read_agi(mp, tp, agno, &agibp); |
1985 | if (error) | |
1da177e4 | 1986 | return error; |
5e1be0fb | 1987 | |
1da177e4 | 1988 | agi = XFS_BUF_TO_AGI(agibp); |
5e1be0fb | 1989 | |
1da177e4 LT |
1990 | /* |
1991 | * Get the index into the agi hash table for the | |
1992 | * list this inode will go on. | |
1993 | */ | |
1994 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); | |
1995 | ASSERT(agino != 0); | |
1996 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; | |
69ef921b | 1997 | ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)); |
1da177e4 LT |
1998 | ASSERT(agi->agi_unlinked[bucket_index]); |
1999 | ||
16259e7d | 2000 | if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) { |
1da177e4 | 2001 | /* |
475ee413 CH |
2002 | * We're at the head of the list. Get the inode's on-disk |
2003 | * buffer to see if there is anyone after us on the list. | |
2004 | * Only modify our next pointer if it is not already NULLAGINO. | |
2005 | * This saves us the overhead of dealing with the buffer when | |
2006 | * there is no need to change it. | |
1da177e4 | 2007 | */ |
475ee413 CH |
2008 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
2009 | 0, 0); | |
1da177e4 | 2010 | if (error) { |
475ee413 | 2011 | xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.", |
0b932ccc | 2012 | __func__, error); |
1da177e4 LT |
2013 | return error; |
2014 | } | |
347d1c01 | 2015 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
1da177e4 LT |
2016 | ASSERT(next_agino != 0); |
2017 | if (next_agino != NULLAGINO) { | |
347d1c01 | 2018 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
92bfc6e7 | 2019 | offset = ip->i_imap.im_boffset + |
1da177e4 | 2020 | offsetof(xfs_dinode_t, di_next_unlinked); |
0a32c26e DC |
2021 | |
2022 | /* need to recalc the inode CRC if appropriate */ | |
2023 | xfs_dinode_calc_crc(mp, dip); | |
2024 | ||
1da177e4 LT |
2025 | xfs_trans_inode_buf(tp, ibp); |
2026 | xfs_trans_log_buf(tp, ibp, offset, | |
2027 | (offset + sizeof(xfs_agino_t) - 1)); | |
2028 | xfs_inobp_check(mp, ibp); | |
2029 | } else { | |
2030 | xfs_trans_brelse(tp, ibp); | |
2031 | } | |
2032 | /* | |
2033 | * Point the bucket head pointer at the next inode. | |
2034 | */ | |
2035 | ASSERT(next_agino != 0); | |
2036 | ASSERT(next_agino != agino); | |
16259e7d | 2037 | agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino); |
1da177e4 LT |
2038 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
2039 | (sizeof(xfs_agino_t) * bucket_index); | |
2040 | xfs_trans_log_buf(tp, agibp, offset, | |
2041 | (offset + sizeof(xfs_agino_t) - 1)); | |
2042 | } else { | |
2043 | /* | |
2044 | * We need to search the list for the inode being freed. | |
2045 | */ | |
16259e7d | 2046 | next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); |
1da177e4 LT |
2047 | last_ibp = NULL; |
2048 | while (next_agino != agino) { | |
129dbc9a CH |
2049 | struct xfs_imap imap; |
2050 | ||
2051 | if (last_ibp) | |
1da177e4 | 2052 | xfs_trans_brelse(tp, last_ibp); |
129dbc9a CH |
2053 | |
2054 | imap.im_blkno = 0; | |
1da177e4 | 2055 | next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino); |
129dbc9a CH |
2056 | |
2057 | error = xfs_imap(mp, tp, next_ino, &imap, 0); | |
2058 | if (error) { | |
2059 | xfs_warn(mp, | |
2060 | "%s: xfs_imap returned error %d.", | |
2061 | __func__, error); | |
2062 | return error; | |
2063 | } | |
2064 | ||
2065 | error = xfs_imap_to_bp(mp, tp, &imap, &last_dip, | |
2066 | &last_ibp, 0, 0); | |
1da177e4 | 2067 | if (error) { |
0b932ccc | 2068 | xfs_warn(mp, |
129dbc9a | 2069 | "%s: xfs_imap_to_bp returned error %d.", |
0b932ccc | 2070 | __func__, error); |
1da177e4 LT |
2071 | return error; |
2072 | } | |
129dbc9a CH |
2073 | |
2074 | last_offset = imap.im_boffset; | |
347d1c01 | 2075 | next_agino = be32_to_cpu(last_dip->di_next_unlinked); |
1da177e4 LT |
2076 | ASSERT(next_agino != NULLAGINO); |
2077 | ASSERT(next_agino != 0); | |
2078 | } | |
475ee413 | 2079 | |
1da177e4 | 2080 | /* |
475ee413 CH |
2081 | * Now last_ibp points to the buffer previous to us on the |
2082 | * unlinked list. Pull us from the list. | |
1da177e4 | 2083 | */ |
475ee413 CH |
2084 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
2085 | 0, 0); | |
1da177e4 | 2086 | if (error) { |
475ee413 | 2087 | xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.", |
0b932ccc | 2088 | __func__, error); |
1da177e4 LT |
2089 | return error; |
2090 | } | |
347d1c01 | 2091 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
1da177e4 LT |
2092 | ASSERT(next_agino != 0); |
2093 | ASSERT(next_agino != agino); | |
2094 | if (next_agino != NULLAGINO) { | |
347d1c01 | 2095 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
92bfc6e7 | 2096 | offset = ip->i_imap.im_boffset + |
1da177e4 | 2097 | offsetof(xfs_dinode_t, di_next_unlinked); |
0a32c26e DC |
2098 | |
2099 | /* need to recalc the inode CRC if appropriate */ | |
2100 | xfs_dinode_calc_crc(mp, dip); | |
2101 | ||
1da177e4 LT |
2102 | xfs_trans_inode_buf(tp, ibp); |
2103 | xfs_trans_log_buf(tp, ibp, offset, | |
2104 | (offset + sizeof(xfs_agino_t) - 1)); | |
2105 | xfs_inobp_check(mp, ibp); | |
2106 | } else { | |
2107 | xfs_trans_brelse(tp, ibp); | |
2108 | } | |
2109 | /* | |
2110 | * Point the previous inode on the list to the next inode. | |
2111 | */ | |
347d1c01 | 2112 | last_dip->di_next_unlinked = cpu_to_be32(next_agino); |
1da177e4 LT |
2113 | ASSERT(next_agino != 0); |
2114 | offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked); | |
0a32c26e DC |
2115 | |
2116 | /* need to recalc the inode CRC if appropriate */ | |
2117 | xfs_dinode_calc_crc(mp, last_dip); | |
2118 | ||
1da177e4 LT |
2119 | xfs_trans_inode_buf(tp, last_ibp); |
2120 | xfs_trans_log_buf(tp, last_ibp, offset, | |
2121 | (offset + sizeof(xfs_agino_t) - 1)); | |
2122 | xfs_inobp_check(mp, last_ibp); | |
2123 | } | |
2124 | return 0; | |
2125 | } | |
2126 | ||
5b3eed75 | 2127 | /* |
0b8182db | 2128 | * A big issue when freeing the inode cluster is that we _cannot_ skip any |
5b3eed75 DC |
2129 | * inodes that are in memory - they all must be marked stale and attached to |
2130 | * the cluster buffer. | |
2131 | */ | |
2a30f36d | 2132 | STATIC int |
1da177e4 LT |
2133 | xfs_ifree_cluster( |
2134 | xfs_inode_t *free_ip, | |
2135 | xfs_trans_t *tp, | |
2136 | xfs_ino_t inum) | |
2137 | { | |
2138 | xfs_mount_t *mp = free_ip->i_mount; | |
2139 | int blks_per_cluster; | |
2140 | int nbufs; | |
2141 | int ninodes; | |
5b257b4a | 2142 | int i, j; |
1da177e4 LT |
2143 | xfs_daddr_t blkno; |
2144 | xfs_buf_t *bp; | |
5b257b4a | 2145 | xfs_inode_t *ip; |
1da177e4 LT |
2146 | xfs_inode_log_item_t *iip; |
2147 | xfs_log_item_t *lip; | |
5017e97d | 2148 | struct xfs_perag *pag; |
1da177e4 | 2149 | |
5017e97d | 2150 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum)); |
1da177e4 LT |
2151 | if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) { |
2152 | blks_per_cluster = 1; | |
2153 | ninodes = mp->m_sb.sb_inopblock; | |
2154 | nbufs = XFS_IALLOC_BLOCKS(mp); | |
2155 | } else { | |
2156 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) / | |
2157 | mp->m_sb.sb_blocksize; | |
2158 | ninodes = blks_per_cluster * mp->m_sb.sb_inopblock; | |
2159 | nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster; | |
2160 | } | |
2161 | ||
1da177e4 LT |
2162 | for (j = 0; j < nbufs; j++, inum += ninodes) { |
2163 | blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), | |
2164 | XFS_INO_TO_AGBNO(mp, inum)); | |
2165 | ||
5b257b4a DC |
2166 | /* |
2167 | * We obtain and lock the backing buffer first in the process | |
2168 | * here, as we have to ensure that any dirty inode that we | |
2169 | * can't get the flush lock on is attached to the buffer. | |
2170 | * If we scan the in-memory inodes first, then buffer IO can | |
2171 | * complete before we get a lock on it, and hence we may fail | |
2172 | * to mark all the active inodes on the buffer stale. | |
2173 | */ | |
2174 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, | |
b6aff29f DC |
2175 | mp->m_bsize * blks_per_cluster, |
2176 | XBF_UNMAPPED); | |
5b257b4a | 2177 | |
2a30f36d CS |
2178 | if (!bp) |
2179 | return ENOMEM; | |
b0f539de DC |
2180 | |
2181 | /* | |
2182 | * This buffer may not have been correctly initialised as we | |
2183 | * didn't read it from disk. That's not important because we are | |
2184 | * only using to mark the buffer as stale in the log, and to | |
2185 | * attach stale cached inodes on it. That means it will never be | |
2186 | * dispatched for IO. If it is, we want to know about it, and we | |
2187 | * want it to fail. We can acheive this by adding a write | |
2188 | * verifier to the buffer. | |
2189 | */ | |
1813dd64 | 2190 | bp->b_ops = &xfs_inode_buf_ops; |
b0f539de | 2191 | |
5b257b4a DC |
2192 | /* |
2193 | * Walk the inodes already attached to the buffer and mark them | |
2194 | * stale. These will all have the flush locks held, so an | |
5b3eed75 DC |
2195 | * in-memory inode walk can't lock them. By marking them all |
2196 | * stale first, we will not attempt to lock them in the loop | |
2197 | * below as the XFS_ISTALE flag will be set. | |
5b257b4a | 2198 | */ |
adadbeef | 2199 | lip = bp->b_fspriv; |
5b257b4a DC |
2200 | while (lip) { |
2201 | if (lip->li_type == XFS_LI_INODE) { | |
2202 | iip = (xfs_inode_log_item_t *)lip; | |
2203 | ASSERT(iip->ili_logged == 1); | |
ca30b2a7 | 2204 | lip->li_cb = xfs_istale_done; |
5b257b4a DC |
2205 | xfs_trans_ail_copy_lsn(mp->m_ail, |
2206 | &iip->ili_flush_lsn, | |
2207 | &iip->ili_item.li_lsn); | |
2208 | xfs_iflags_set(iip->ili_inode, XFS_ISTALE); | |
5b257b4a DC |
2209 | } |
2210 | lip = lip->li_bio_list; | |
2211 | } | |
1da177e4 | 2212 | |
5b3eed75 | 2213 | |
1da177e4 | 2214 | /* |
5b257b4a DC |
2215 | * For each inode in memory attempt to add it to the inode |
2216 | * buffer and set it up for being staled on buffer IO | |
2217 | * completion. This is safe as we've locked out tail pushing | |
2218 | * and flushing by locking the buffer. | |
1da177e4 | 2219 | * |
5b257b4a DC |
2220 | * We have already marked every inode that was part of a |
2221 | * transaction stale above, which means there is no point in | |
2222 | * even trying to lock them. | |
1da177e4 | 2223 | */ |
1da177e4 | 2224 | for (i = 0; i < ninodes; i++) { |
5b3eed75 | 2225 | retry: |
1a3e8f3d | 2226 | rcu_read_lock(); |
da353b0d DC |
2227 | ip = radix_tree_lookup(&pag->pag_ici_root, |
2228 | XFS_INO_TO_AGINO(mp, (inum + i))); | |
1da177e4 | 2229 | |
1a3e8f3d DC |
2230 | /* Inode not in memory, nothing to do */ |
2231 | if (!ip) { | |
2232 | rcu_read_unlock(); | |
1da177e4 LT |
2233 | continue; |
2234 | } | |
2235 | ||
1a3e8f3d DC |
2236 | /* |
2237 | * because this is an RCU protected lookup, we could | |
2238 | * find a recently freed or even reallocated inode | |
2239 | * during the lookup. We need to check under the | |
2240 | * i_flags_lock for a valid inode here. Skip it if it | |
2241 | * is not valid, the wrong inode or stale. | |
2242 | */ | |
2243 | spin_lock(&ip->i_flags_lock); | |
2244 | if (ip->i_ino != inum + i || | |
2245 | __xfs_iflags_test(ip, XFS_ISTALE)) { | |
2246 | spin_unlock(&ip->i_flags_lock); | |
2247 | rcu_read_unlock(); | |
2248 | continue; | |
2249 | } | |
2250 | spin_unlock(&ip->i_flags_lock); | |
2251 | ||
5b3eed75 DC |
2252 | /* |
2253 | * Don't try to lock/unlock the current inode, but we | |
2254 | * _cannot_ skip the other inodes that we did not find | |
2255 | * in the list attached to the buffer and are not | |
2256 | * already marked stale. If we can't lock it, back off | |
2257 | * and retry. | |
2258 | */ | |
5b257b4a DC |
2259 | if (ip != free_ip && |
2260 | !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { | |
1a3e8f3d | 2261 | rcu_read_unlock(); |
5b3eed75 DC |
2262 | delay(1); |
2263 | goto retry; | |
1da177e4 | 2264 | } |
1a3e8f3d | 2265 | rcu_read_unlock(); |
1da177e4 | 2266 | |
5b3eed75 | 2267 | xfs_iflock(ip); |
5b257b4a | 2268 | xfs_iflags_set(ip, XFS_ISTALE); |
1da177e4 | 2269 | |
5b3eed75 DC |
2270 | /* |
2271 | * we don't need to attach clean inodes or those only | |
2272 | * with unlogged changes (which we throw away, anyway). | |
2273 | */ | |
1da177e4 | 2274 | iip = ip->i_itemp; |
5b3eed75 | 2275 | if (!iip || xfs_inode_clean(ip)) { |
5b257b4a | 2276 | ASSERT(ip != free_ip); |
1da177e4 LT |
2277 | xfs_ifunlock(ip); |
2278 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
2279 | continue; | |
2280 | } | |
2281 | ||
f5d8d5c4 CH |
2282 | iip->ili_last_fields = iip->ili_fields; |
2283 | iip->ili_fields = 0; | |
1da177e4 | 2284 | iip->ili_logged = 1; |
7b2e2a31 DC |
2285 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
2286 | &iip->ili_item.li_lsn); | |
1da177e4 | 2287 | |
ca30b2a7 CH |
2288 | xfs_buf_attach_iodone(bp, xfs_istale_done, |
2289 | &iip->ili_item); | |
5b257b4a DC |
2290 | |
2291 | if (ip != free_ip) | |
1da177e4 | 2292 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
1da177e4 LT |
2293 | } |
2294 | ||
5b3eed75 | 2295 | xfs_trans_stale_inode_buf(tp, bp); |
1da177e4 LT |
2296 | xfs_trans_binval(tp, bp); |
2297 | } | |
2298 | ||
5017e97d | 2299 | xfs_perag_put(pag); |
2a30f36d | 2300 | return 0; |
1da177e4 LT |
2301 | } |
2302 | ||
2303 | /* | |
2304 | * This is called to return an inode to the inode free list. | |
2305 | * The inode should already be truncated to 0 length and have | |
2306 | * no pages associated with it. This routine also assumes that | |
2307 | * the inode is already a part of the transaction. | |
2308 | * | |
2309 | * The on-disk copy of the inode will have been added to the list | |
2310 | * of unlinked inodes in the AGI. We need to remove the inode from | |
2311 | * that list atomically with respect to freeing it here. | |
2312 | */ | |
2313 | int | |
2314 | xfs_ifree( | |
2315 | xfs_trans_t *tp, | |
2316 | xfs_inode_t *ip, | |
2317 | xfs_bmap_free_t *flist) | |
2318 | { | |
2319 | int error; | |
2320 | int delete; | |
2321 | xfs_ino_t first_ino; | |
2322 | ||
579aa9ca | 2323 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 LT |
2324 | ASSERT(ip->i_d.di_nlink == 0); |
2325 | ASSERT(ip->i_d.di_nextents == 0); | |
2326 | ASSERT(ip->i_d.di_anextents == 0); | |
ce7ae151 | 2327 | ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode)); |
1da177e4 LT |
2328 | ASSERT(ip->i_d.di_nblocks == 0); |
2329 | ||
2330 | /* | |
2331 | * Pull the on-disk inode from the AGI unlinked list. | |
2332 | */ | |
2333 | error = xfs_iunlink_remove(tp, ip); | |
1baaed8f | 2334 | if (error) |
1da177e4 | 2335 | return error; |
1da177e4 LT |
2336 | |
2337 | error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino); | |
1baaed8f | 2338 | if (error) |
1da177e4 | 2339 | return error; |
1baaed8f | 2340 | |
1da177e4 LT |
2341 | ip->i_d.di_mode = 0; /* mark incore inode as free */ |
2342 | ip->i_d.di_flags = 0; | |
2343 | ip->i_d.di_dmevmask = 0; | |
2344 | ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */ | |
1da177e4 LT |
2345 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
2346 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
2347 | /* | |
2348 | * Bump the generation count so no one will be confused | |
2349 | * by reincarnations of this inode. | |
2350 | */ | |
2351 | ip->i_d.di_gen++; | |
2352 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
2353 | ||
1baaed8f | 2354 | if (delete) |
2a30f36d | 2355 | error = xfs_ifree_cluster(ip, tp, first_ino); |
1da177e4 | 2356 | |
2a30f36d | 2357 | return error; |
1da177e4 LT |
2358 | } |
2359 | ||
1da177e4 | 2360 | /* |
60ec6783 CH |
2361 | * This is called to unpin an inode. The caller must have the inode locked |
2362 | * in at least shared mode so that the buffer cannot be subsequently pinned | |
2363 | * once someone is waiting for it to be unpinned. | |
1da177e4 | 2364 | */ |
60ec6783 | 2365 | static void |
f392e631 | 2366 | xfs_iunpin( |
60ec6783 | 2367 | struct xfs_inode *ip) |
1da177e4 | 2368 | { |
579aa9ca | 2369 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
1da177e4 | 2370 | |
4aaf15d1 DC |
2371 | trace_xfs_inode_unpin_nowait(ip, _RET_IP_); |
2372 | ||
a3f74ffb | 2373 | /* Give the log a push to start the unpinning I/O */ |
60ec6783 | 2374 | xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0); |
a14a348b | 2375 | |
a3f74ffb | 2376 | } |
1da177e4 | 2377 | |
f392e631 CH |
2378 | static void |
2379 | __xfs_iunpin_wait( | |
2380 | struct xfs_inode *ip) | |
2381 | { | |
2382 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); | |
2383 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); | |
2384 | ||
2385 | xfs_iunpin(ip); | |
2386 | ||
2387 | do { | |
2388 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); | |
2389 | if (xfs_ipincount(ip)) | |
2390 | io_schedule(); | |
2391 | } while (xfs_ipincount(ip)); | |
2392 | finish_wait(wq, &wait.wait); | |
2393 | } | |
2394 | ||
777df5af | 2395 | void |
a3f74ffb | 2396 | xfs_iunpin_wait( |
60ec6783 | 2397 | struct xfs_inode *ip) |
a3f74ffb | 2398 | { |
f392e631 CH |
2399 | if (xfs_ipincount(ip)) |
2400 | __xfs_iunpin_wait(ip); | |
1da177e4 LT |
2401 | } |
2402 | ||
27320369 DC |
2403 | /* |
2404 | * Removing an inode from the namespace involves removing the directory entry | |
2405 | * and dropping the link count on the inode. Removing the directory entry can | |
2406 | * result in locking an AGF (directory blocks were freed) and removing a link | |
2407 | * count can result in placing the inode on an unlinked list which results in | |
2408 | * locking an AGI. | |
2409 | * | |
2410 | * The big problem here is that we have an ordering constraint on AGF and AGI | |
2411 | * locking - inode allocation locks the AGI, then can allocate a new extent for | |
2412 | * new inodes, locking the AGF after the AGI. Similarly, freeing the inode | |
2413 | * removes the inode from the unlinked list, requiring that we lock the AGI | |
2414 | * first, and then freeing the inode can result in an inode chunk being freed | |
2415 | * and hence freeing disk space requiring that we lock an AGF. | |
2416 | * | |
2417 | * Hence the ordering that is imposed by other parts of the code is AGI before | |
2418 | * AGF. This means we cannot remove the directory entry before we drop the inode | |
2419 | * reference count and put it on the unlinked list as this results in a lock | |
2420 | * order of AGF then AGI, and this can deadlock against inode allocation and | |
2421 | * freeing. Therefore we must drop the link counts before we remove the | |
2422 | * directory entry. | |
2423 | * | |
2424 | * This is still safe from a transactional point of view - it is not until we | |
2425 | * get to xfs_bmap_finish() that we have the possibility of multiple | |
2426 | * transactions in this operation. Hence as long as we remove the directory | |
2427 | * entry and drop the link count in the first transaction of the remove | |
2428 | * operation, there are no transactional constraints on the ordering here. | |
2429 | */ | |
c24b5dfa DC |
2430 | int |
2431 | xfs_remove( | |
2432 | xfs_inode_t *dp, | |
2433 | struct xfs_name *name, | |
2434 | xfs_inode_t *ip) | |
2435 | { | |
2436 | xfs_mount_t *mp = dp->i_mount; | |
2437 | xfs_trans_t *tp = NULL; | |
2438 | int is_dir = S_ISDIR(ip->i_d.di_mode); | |
2439 | int error = 0; | |
2440 | xfs_bmap_free_t free_list; | |
2441 | xfs_fsblock_t first_block; | |
2442 | int cancel_flags; | |
2443 | int committed; | |
2444 | int link_zero; | |
2445 | uint resblks; | |
2446 | uint log_count; | |
2447 | ||
2448 | trace_xfs_remove(dp, name); | |
2449 | ||
2450 | if (XFS_FORCED_SHUTDOWN(mp)) | |
2451 | return XFS_ERROR(EIO); | |
2452 | ||
2453 | error = xfs_qm_dqattach(dp, 0); | |
2454 | if (error) | |
2455 | goto std_return; | |
2456 | ||
2457 | error = xfs_qm_dqattach(ip, 0); | |
2458 | if (error) | |
2459 | goto std_return; | |
2460 | ||
2461 | if (is_dir) { | |
2462 | tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR); | |
2463 | log_count = XFS_DEFAULT_LOG_COUNT; | |
2464 | } else { | |
2465 | tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE); | |
2466 | log_count = XFS_REMOVE_LOG_COUNT; | |
2467 | } | |
2468 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | |
2469 | ||
2470 | /* | |
2471 | * We try to get the real space reservation first, | |
2472 | * allowing for directory btree deletion(s) implying | |
2473 | * possible bmap insert(s). If we can't get the space | |
2474 | * reservation then we use 0 instead, and avoid the bmap | |
2475 | * btree insert(s) in the directory code by, if the bmap | |
2476 | * insert tries to happen, instead trimming the LAST | |
2477 | * block from the directory. | |
2478 | */ | |
2479 | resblks = XFS_REMOVE_SPACE_RES(mp); | |
3d3c8b52 | 2480 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0); |
c24b5dfa DC |
2481 | if (error == ENOSPC) { |
2482 | resblks = 0; | |
3d3c8b52 | 2483 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0); |
c24b5dfa DC |
2484 | } |
2485 | if (error) { | |
2486 | ASSERT(error != ENOSPC); | |
2487 | cancel_flags = 0; | |
2488 | goto out_trans_cancel; | |
2489 | } | |
2490 | ||
2491 | xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL); | |
2492 | ||
2493 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); | |
2494 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
2495 | ||
2496 | /* | |
2497 | * If we're removing a directory perform some additional validation. | |
2498 | */ | |
27320369 | 2499 | cancel_flags |= XFS_TRANS_ABORT; |
c24b5dfa DC |
2500 | if (is_dir) { |
2501 | ASSERT(ip->i_d.di_nlink >= 2); | |
2502 | if (ip->i_d.di_nlink != 2) { | |
2503 | error = XFS_ERROR(ENOTEMPTY); | |
2504 | goto out_trans_cancel; | |
2505 | } | |
2506 | if (!xfs_dir_isempty(ip)) { | |
2507 | error = XFS_ERROR(ENOTEMPTY); | |
2508 | goto out_trans_cancel; | |
2509 | } | |
c24b5dfa | 2510 | |
27320369 | 2511 | /* Drop the link from ip's "..". */ |
c24b5dfa DC |
2512 | error = xfs_droplink(tp, dp); |
2513 | if (error) | |
27320369 | 2514 | goto out_trans_cancel; |
c24b5dfa | 2515 | |
27320369 | 2516 | /* Drop the "." link from ip to self. */ |
c24b5dfa DC |
2517 | error = xfs_droplink(tp, ip); |
2518 | if (error) | |
27320369 | 2519 | goto out_trans_cancel; |
c24b5dfa DC |
2520 | } else { |
2521 | /* | |
2522 | * When removing a non-directory we need to log the parent | |
2523 | * inode here. For a directory this is done implicitly | |
2524 | * by the xfs_droplink call for the ".." entry. | |
2525 | */ | |
2526 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
2527 | } | |
27320369 | 2528 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
c24b5dfa | 2529 | |
27320369 | 2530 | /* Drop the link from dp to ip. */ |
c24b5dfa DC |
2531 | error = xfs_droplink(tp, ip); |
2532 | if (error) | |
27320369 | 2533 | goto out_trans_cancel; |
c24b5dfa | 2534 | |
27320369 | 2535 | /* Determine if this is the last link while the inode is locked */ |
c24b5dfa DC |
2536 | link_zero = (ip->i_d.di_nlink == 0); |
2537 | ||
27320369 DC |
2538 | xfs_bmap_init(&free_list, &first_block); |
2539 | error = xfs_dir_removename(tp, dp, name, ip->i_ino, | |
2540 | &first_block, &free_list, resblks); | |
2541 | if (error) { | |
2542 | ASSERT(error != ENOENT); | |
2543 | goto out_bmap_cancel; | |
2544 | } | |
2545 | ||
c24b5dfa DC |
2546 | /* |
2547 | * If this is a synchronous mount, make sure that the | |
2548 | * remove transaction goes to disk before returning to | |
2549 | * the user. | |
2550 | */ | |
2551 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) | |
2552 | xfs_trans_set_sync(tp); | |
2553 | ||
2554 | error = xfs_bmap_finish(&tp, &free_list, &committed); | |
2555 | if (error) | |
2556 | goto out_bmap_cancel; | |
2557 | ||
2558 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
2559 | if (error) | |
2560 | goto std_return; | |
2561 | ||
2562 | /* | |
2563 | * If we are using filestreams, kill the stream association. | |
2564 | * If the file is still open it may get a new one but that | |
2565 | * will get killed on last close in xfs_close() so we don't | |
2566 | * have to worry about that. | |
2567 | */ | |
2568 | if (!is_dir && link_zero && xfs_inode_is_filestream(ip)) | |
2569 | xfs_filestream_deassociate(ip); | |
2570 | ||
2571 | return 0; | |
2572 | ||
2573 | out_bmap_cancel: | |
2574 | xfs_bmap_cancel(&free_list); | |
c24b5dfa DC |
2575 | out_trans_cancel: |
2576 | xfs_trans_cancel(tp, cancel_flags); | |
2577 | std_return: | |
2578 | return error; | |
2579 | } | |
2580 | ||
f6bba201 DC |
2581 | /* |
2582 | * Enter all inodes for a rename transaction into a sorted array. | |
2583 | */ | |
2584 | STATIC void | |
2585 | xfs_sort_for_rename( | |
2586 | xfs_inode_t *dp1, /* in: old (source) directory inode */ | |
2587 | xfs_inode_t *dp2, /* in: new (target) directory inode */ | |
2588 | xfs_inode_t *ip1, /* in: inode of old entry */ | |
2589 | xfs_inode_t *ip2, /* in: inode of new entry, if it | |
2590 | already exists, NULL otherwise. */ | |
2591 | xfs_inode_t **i_tab,/* out: array of inode returned, sorted */ | |
2592 | int *num_inodes) /* out: number of inodes in array */ | |
2593 | { | |
2594 | xfs_inode_t *temp; | |
2595 | int i, j; | |
2596 | ||
2597 | /* | |
2598 | * i_tab contains a list of pointers to inodes. We initialize | |
2599 | * the table here & we'll sort it. We will then use it to | |
2600 | * order the acquisition of the inode locks. | |
2601 | * | |
2602 | * Note that the table may contain duplicates. e.g., dp1 == dp2. | |
2603 | */ | |
2604 | i_tab[0] = dp1; | |
2605 | i_tab[1] = dp2; | |
2606 | i_tab[2] = ip1; | |
2607 | if (ip2) { | |
2608 | *num_inodes = 4; | |
2609 | i_tab[3] = ip2; | |
2610 | } else { | |
2611 | *num_inodes = 3; | |
2612 | i_tab[3] = NULL; | |
2613 | } | |
2614 | ||
2615 | /* | |
2616 | * Sort the elements via bubble sort. (Remember, there are at | |
2617 | * most 4 elements to sort, so this is adequate.) | |
2618 | */ | |
2619 | for (i = 0; i < *num_inodes; i++) { | |
2620 | for (j = 1; j < *num_inodes; j++) { | |
2621 | if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) { | |
2622 | temp = i_tab[j]; | |
2623 | i_tab[j] = i_tab[j-1]; | |
2624 | i_tab[j-1] = temp; | |
2625 | } | |
2626 | } | |
2627 | } | |
2628 | } | |
2629 | ||
2630 | /* | |
2631 | * xfs_rename | |
2632 | */ | |
2633 | int | |
2634 | xfs_rename( | |
2635 | xfs_inode_t *src_dp, | |
2636 | struct xfs_name *src_name, | |
2637 | xfs_inode_t *src_ip, | |
2638 | xfs_inode_t *target_dp, | |
2639 | struct xfs_name *target_name, | |
2640 | xfs_inode_t *target_ip) | |
2641 | { | |
2642 | xfs_trans_t *tp = NULL; | |
2643 | xfs_mount_t *mp = src_dp->i_mount; | |
2644 | int new_parent; /* moving to a new dir */ | |
2645 | int src_is_directory; /* src_name is a directory */ | |
2646 | int error; | |
2647 | xfs_bmap_free_t free_list; | |
2648 | xfs_fsblock_t first_block; | |
2649 | int cancel_flags; | |
2650 | int committed; | |
2651 | xfs_inode_t *inodes[4]; | |
2652 | int spaceres; | |
2653 | int num_inodes; | |
2654 | ||
2655 | trace_xfs_rename(src_dp, target_dp, src_name, target_name); | |
2656 | ||
2657 | new_parent = (src_dp != target_dp); | |
2658 | src_is_directory = S_ISDIR(src_ip->i_d.di_mode); | |
2659 | ||
2660 | xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, | |
2661 | inodes, &num_inodes); | |
2662 | ||
2663 | xfs_bmap_init(&free_list, &first_block); | |
2664 | tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME); | |
2665 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | |
2666 | spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len); | |
3d3c8b52 | 2667 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0); |
f6bba201 DC |
2668 | if (error == ENOSPC) { |
2669 | spaceres = 0; | |
3d3c8b52 | 2670 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0); |
f6bba201 DC |
2671 | } |
2672 | if (error) { | |
2673 | xfs_trans_cancel(tp, 0); | |
2674 | goto std_return; | |
2675 | } | |
2676 | ||
2677 | /* | |
2678 | * Attach the dquots to the inodes | |
2679 | */ | |
2680 | error = xfs_qm_vop_rename_dqattach(inodes); | |
2681 | if (error) { | |
2682 | xfs_trans_cancel(tp, cancel_flags); | |
2683 | goto std_return; | |
2684 | } | |
2685 | ||
2686 | /* | |
2687 | * Lock all the participating inodes. Depending upon whether | |
2688 | * the target_name exists in the target directory, and | |
2689 | * whether the target directory is the same as the source | |
2690 | * directory, we can lock from 2 to 4 inodes. | |
2691 | */ | |
2692 | xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL); | |
2693 | ||
2694 | /* | |
2695 | * Join all the inodes to the transaction. From this point on, | |
2696 | * we can rely on either trans_commit or trans_cancel to unlock | |
2697 | * them. | |
2698 | */ | |
2699 | xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL); | |
2700 | if (new_parent) | |
2701 | xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL); | |
2702 | xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL); | |
2703 | if (target_ip) | |
2704 | xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL); | |
2705 | ||
2706 | /* | |
2707 | * If we are using project inheritance, we only allow renames | |
2708 | * into our tree when the project IDs are the same; else the | |
2709 | * tree quota mechanism would be circumvented. | |
2710 | */ | |
2711 | if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && | |
2712 | (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) { | |
2713 | error = XFS_ERROR(EXDEV); | |
2714 | goto error_return; | |
2715 | } | |
2716 | ||
2717 | /* | |
2718 | * Set up the target. | |
2719 | */ | |
2720 | if (target_ip == NULL) { | |
2721 | /* | |
2722 | * If there's no space reservation, check the entry will | |
2723 | * fit before actually inserting it. | |
2724 | */ | |
2725 | error = xfs_dir_canenter(tp, target_dp, target_name, spaceres); | |
2726 | if (error) | |
2727 | goto error_return; | |
2728 | /* | |
2729 | * If target does not exist and the rename crosses | |
2730 | * directories, adjust the target directory link count | |
2731 | * to account for the ".." reference from the new entry. | |
2732 | */ | |
2733 | error = xfs_dir_createname(tp, target_dp, target_name, | |
2734 | src_ip->i_ino, &first_block, | |
2735 | &free_list, spaceres); | |
2736 | if (error == ENOSPC) | |
2737 | goto error_return; | |
2738 | if (error) | |
2739 | goto abort_return; | |
2740 | ||
2741 | xfs_trans_ichgtime(tp, target_dp, | |
2742 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
2743 | ||
2744 | if (new_parent && src_is_directory) { | |
2745 | error = xfs_bumplink(tp, target_dp); | |
2746 | if (error) | |
2747 | goto abort_return; | |
2748 | } | |
2749 | } else { /* target_ip != NULL */ | |
2750 | /* | |
2751 | * If target exists and it's a directory, check that both | |
2752 | * target and source are directories and that target can be | |
2753 | * destroyed, or that neither is a directory. | |
2754 | */ | |
2755 | if (S_ISDIR(target_ip->i_d.di_mode)) { | |
2756 | /* | |
2757 | * Make sure target dir is empty. | |
2758 | */ | |
2759 | if (!(xfs_dir_isempty(target_ip)) || | |
2760 | (target_ip->i_d.di_nlink > 2)) { | |
2761 | error = XFS_ERROR(EEXIST); | |
2762 | goto error_return; | |
2763 | } | |
2764 | } | |
2765 | ||
2766 | /* | |
2767 | * Link the source inode under the target name. | |
2768 | * If the source inode is a directory and we are moving | |
2769 | * it across directories, its ".." entry will be | |
2770 | * inconsistent until we replace that down below. | |
2771 | * | |
2772 | * In case there is already an entry with the same | |
2773 | * name at the destination directory, remove it first. | |
2774 | */ | |
2775 | error = xfs_dir_replace(tp, target_dp, target_name, | |
2776 | src_ip->i_ino, | |
2777 | &first_block, &free_list, spaceres); | |
2778 | if (error) | |
2779 | goto abort_return; | |
2780 | ||
2781 | xfs_trans_ichgtime(tp, target_dp, | |
2782 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
2783 | ||
2784 | /* | |
2785 | * Decrement the link count on the target since the target | |
2786 | * dir no longer points to it. | |
2787 | */ | |
2788 | error = xfs_droplink(tp, target_ip); | |
2789 | if (error) | |
2790 | goto abort_return; | |
2791 | ||
2792 | if (src_is_directory) { | |
2793 | /* | |
2794 | * Drop the link from the old "." entry. | |
2795 | */ | |
2796 | error = xfs_droplink(tp, target_ip); | |
2797 | if (error) | |
2798 | goto abort_return; | |
2799 | } | |
2800 | } /* target_ip != NULL */ | |
2801 | ||
2802 | /* | |
2803 | * Remove the source. | |
2804 | */ | |
2805 | if (new_parent && src_is_directory) { | |
2806 | /* | |
2807 | * Rewrite the ".." entry to point to the new | |
2808 | * directory. | |
2809 | */ | |
2810 | error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, | |
2811 | target_dp->i_ino, | |
2812 | &first_block, &free_list, spaceres); | |
2813 | ASSERT(error != EEXIST); | |
2814 | if (error) | |
2815 | goto abort_return; | |
2816 | } | |
2817 | ||
2818 | /* | |
2819 | * We always want to hit the ctime on the source inode. | |
2820 | * | |
2821 | * This isn't strictly required by the standards since the source | |
2822 | * inode isn't really being changed, but old unix file systems did | |
2823 | * it and some incremental backup programs won't work without it. | |
2824 | */ | |
2825 | xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); | |
2826 | xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); | |
2827 | ||
2828 | /* | |
2829 | * Adjust the link count on src_dp. This is necessary when | |
2830 | * renaming a directory, either within one parent when | |
2831 | * the target existed, or across two parent directories. | |
2832 | */ | |
2833 | if (src_is_directory && (new_parent || target_ip != NULL)) { | |
2834 | ||
2835 | /* | |
2836 | * Decrement link count on src_directory since the | |
2837 | * entry that's moved no longer points to it. | |
2838 | */ | |
2839 | error = xfs_droplink(tp, src_dp); | |
2840 | if (error) | |
2841 | goto abort_return; | |
2842 | } | |
2843 | ||
2844 | error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, | |
2845 | &first_block, &free_list, spaceres); | |
2846 | if (error) | |
2847 | goto abort_return; | |
2848 | ||
2849 | xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
2850 | xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); | |
2851 | if (new_parent) | |
2852 | xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); | |
2853 | ||
2854 | /* | |
2855 | * If this is a synchronous mount, make sure that the | |
2856 | * rename transaction goes to disk before returning to | |
2857 | * the user. | |
2858 | */ | |
2859 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { | |
2860 | xfs_trans_set_sync(tp); | |
2861 | } | |
2862 | ||
2863 | error = xfs_bmap_finish(&tp, &free_list, &committed); | |
2864 | if (error) { | |
2865 | xfs_bmap_cancel(&free_list); | |
2866 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | | |
2867 | XFS_TRANS_ABORT)); | |
2868 | goto std_return; | |
2869 | } | |
2870 | ||
2871 | /* | |
2872 | * trans_commit will unlock src_ip, target_ip & decrement | |
2873 | * the vnode references. | |
2874 | */ | |
2875 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
2876 | ||
2877 | abort_return: | |
2878 | cancel_flags |= XFS_TRANS_ABORT; | |
2879 | error_return: | |
2880 | xfs_bmap_cancel(&free_list); | |
2881 | xfs_trans_cancel(tp, cancel_flags); | |
2882 | std_return: | |
2883 | return error; | |
2884 | } | |
2885 | ||
5c4d97d0 DC |
2886 | STATIC int |
2887 | xfs_iflush_cluster( | |
2888 | xfs_inode_t *ip, | |
2889 | xfs_buf_t *bp) | |
1da177e4 | 2890 | { |
5c4d97d0 DC |
2891 | xfs_mount_t *mp = ip->i_mount; |
2892 | struct xfs_perag *pag; | |
2893 | unsigned long first_index, mask; | |
2894 | unsigned long inodes_per_cluster; | |
2895 | int ilist_size; | |
2896 | xfs_inode_t **ilist; | |
2897 | xfs_inode_t *iq; | |
2898 | int nr_found; | |
2899 | int clcount = 0; | |
2900 | int bufwasdelwri; | |
1da177e4 | 2901 | int i; |
1da177e4 | 2902 | |
5c4d97d0 | 2903 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
1da177e4 | 2904 | |
5c4d97d0 DC |
2905 | inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog; |
2906 | ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *); | |
2907 | ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS); | |
2908 | if (!ilist) | |
2909 | goto out_put; | |
1da177e4 | 2910 | |
5c4d97d0 DC |
2911 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
2912 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask; | |
2913 | rcu_read_lock(); | |
2914 | /* really need a gang lookup range call here */ | |
2915 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist, | |
2916 | first_index, inodes_per_cluster); | |
2917 | if (nr_found == 0) | |
2918 | goto out_free; | |
2919 | ||
2920 | for (i = 0; i < nr_found; i++) { | |
2921 | iq = ilist[i]; | |
2922 | if (iq == ip) | |
bad55843 | 2923 | continue; |
1a3e8f3d DC |
2924 | |
2925 | /* | |
2926 | * because this is an RCU protected lookup, we could find a | |
2927 | * recently freed or even reallocated inode during the lookup. | |
2928 | * We need to check under the i_flags_lock for a valid inode | |
2929 | * here. Skip it if it is not valid or the wrong inode. | |
2930 | */ | |
2931 | spin_lock(&ip->i_flags_lock); | |
2932 | if (!ip->i_ino || | |
2933 | (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) { | |
2934 | spin_unlock(&ip->i_flags_lock); | |
2935 | continue; | |
2936 | } | |
2937 | spin_unlock(&ip->i_flags_lock); | |
2938 | ||
bad55843 DC |
2939 | /* |
2940 | * Do an un-protected check to see if the inode is dirty and | |
2941 | * is a candidate for flushing. These checks will be repeated | |
2942 | * later after the appropriate locks are acquired. | |
2943 | */ | |
33540408 | 2944 | if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0) |
bad55843 | 2945 | continue; |
bad55843 DC |
2946 | |
2947 | /* | |
2948 | * Try to get locks. If any are unavailable or it is pinned, | |
2949 | * then this inode cannot be flushed and is skipped. | |
2950 | */ | |
2951 | ||
2952 | if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) | |
2953 | continue; | |
2954 | if (!xfs_iflock_nowait(iq)) { | |
2955 | xfs_iunlock(iq, XFS_ILOCK_SHARED); | |
2956 | continue; | |
2957 | } | |
2958 | if (xfs_ipincount(iq)) { | |
2959 | xfs_ifunlock(iq); | |
2960 | xfs_iunlock(iq, XFS_ILOCK_SHARED); | |
2961 | continue; | |
2962 | } | |
2963 | ||
2964 | /* | |
2965 | * arriving here means that this inode can be flushed. First | |
2966 | * re-check that it's dirty before flushing. | |
2967 | */ | |
33540408 DC |
2968 | if (!xfs_inode_clean(iq)) { |
2969 | int error; | |
bad55843 DC |
2970 | error = xfs_iflush_int(iq, bp); |
2971 | if (error) { | |
2972 | xfs_iunlock(iq, XFS_ILOCK_SHARED); | |
2973 | goto cluster_corrupt_out; | |
2974 | } | |
2975 | clcount++; | |
2976 | } else { | |
2977 | xfs_ifunlock(iq); | |
2978 | } | |
2979 | xfs_iunlock(iq, XFS_ILOCK_SHARED); | |
2980 | } | |
2981 | ||
2982 | if (clcount) { | |
2983 | XFS_STATS_INC(xs_icluster_flushcnt); | |
2984 | XFS_STATS_ADD(xs_icluster_flushinode, clcount); | |
2985 | } | |
2986 | ||
2987 | out_free: | |
1a3e8f3d | 2988 | rcu_read_unlock(); |
f0e2d93c | 2989 | kmem_free(ilist); |
44b56e0a DC |
2990 | out_put: |
2991 | xfs_perag_put(pag); | |
bad55843 DC |
2992 | return 0; |
2993 | ||
2994 | ||
2995 | cluster_corrupt_out: | |
2996 | /* | |
2997 | * Corruption detected in the clustering loop. Invalidate the | |
2998 | * inode buffer and shut down the filesystem. | |
2999 | */ | |
1a3e8f3d | 3000 | rcu_read_unlock(); |
bad55843 | 3001 | /* |
43ff2122 | 3002 | * Clean up the buffer. If it was delwri, just release it -- |
bad55843 DC |
3003 | * brelse can handle it with no problems. If not, shut down the |
3004 | * filesystem before releasing the buffer. | |
3005 | */ | |
43ff2122 | 3006 | bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q); |
bad55843 DC |
3007 | if (bufwasdelwri) |
3008 | xfs_buf_relse(bp); | |
3009 | ||
3010 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); | |
3011 | ||
3012 | if (!bufwasdelwri) { | |
3013 | /* | |
3014 | * Just like incore_relse: if we have b_iodone functions, | |
3015 | * mark the buffer as an error and call them. Otherwise | |
3016 | * mark it as stale and brelse. | |
3017 | */ | |
cb669ca5 | 3018 | if (bp->b_iodone) { |
bad55843 | 3019 | XFS_BUF_UNDONE(bp); |
c867cb61 | 3020 | xfs_buf_stale(bp); |
5a52c2a5 | 3021 | xfs_buf_ioerror(bp, EIO); |
1a1a3e97 | 3022 | xfs_buf_ioend(bp, 0); |
bad55843 | 3023 | } else { |
c867cb61 | 3024 | xfs_buf_stale(bp); |
bad55843 DC |
3025 | xfs_buf_relse(bp); |
3026 | } | |
3027 | } | |
3028 | ||
3029 | /* | |
3030 | * Unlocks the flush lock | |
3031 | */ | |
04913fdd | 3032 | xfs_iflush_abort(iq, false); |
f0e2d93c | 3033 | kmem_free(ilist); |
44b56e0a | 3034 | xfs_perag_put(pag); |
bad55843 DC |
3035 | return XFS_ERROR(EFSCORRUPTED); |
3036 | } | |
3037 | ||
1da177e4 | 3038 | /* |
4c46819a CH |
3039 | * Flush dirty inode metadata into the backing buffer. |
3040 | * | |
3041 | * The caller must have the inode lock and the inode flush lock held. The | |
3042 | * inode lock will still be held upon return to the caller, and the inode | |
3043 | * flush lock will be released after the inode has reached the disk. | |
3044 | * | |
3045 | * The caller must write out the buffer returned in *bpp and release it. | |
1da177e4 LT |
3046 | */ |
3047 | int | |
3048 | xfs_iflush( | |
4c46819a CH |
3049 | struct xfs_inode *ip, |
3050 | struct xfs_buf **bpp) | |
1da177e4 | 3051 | { |
4c46819a CH |
3052 | struct xfs_mount *mp = ip->i_mount; |
3053 | struct xfs_buf *bp; | |
3054 | struct xfs_dinode *dip; | |
1da177e4 | 3055 | int error; |
1da177e4 LT |
3056 | |
3057 | XFS_STATS_INC(xs_iflush_count); | |
3058 | ||
579aa9ca | 3059 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
474fce06 | 3060 | ASSERT(xfs_isiflocked(ip)); |
1da177e4 | 3061 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
8096b1eb | 3062 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
1da177e4 | 3063 | |
4c46819a | 3064 | *bpp = NULL; |
1da177e4 | 3065 | |
1da177e4 LT |
3066 | xfs_iunpin_wait(ip); |
3067 | ||
4b6a4688 DC |
3068 | /* |
3069 | * For stale inodes we cannot rely on the backing buffer remaining | |
3070 | * stale in cache for the remaining life of the stale inode and so | |
475ee413 | 3071 | * xfs_imap_to_bp() below may give us a buffer that no longer contains |
4b6a4688 DC |
3072 | * inodes below. We have to check this after ensuring the inode is |
3073 | * unpinned so that it is safe to reclaim the stale inode after the | |
3074 | * flush call. | |
3075 | */ | |
3076 | if (xfs_iflags_test(ip, XFS_ISTALE)) { | |
3077 | xfs_ifunlock(ip); | |
3078 | return 0; | |
3079 | } | |
3080 | ||
1da177e4 LT |
3081 | /* |
3082 | * This may have been unpinned because the filesystem is shutting | |
3083 | * down forcibly. If that's the case we must not write this inode | |
32ce90a4 CH |
3084 | * to disk, because the log record didn't make it to disk. |
3085 | * | |
3086 | * We also have to remove the log item from the AIL in this case, | |
3087 | * as we wait for an empty AIL as part of the unmount process. | |
1da177e4 LT |
3088 | */ |
3089 | if (XFS_FORCED_SHUTDOWN(mp)) { | |
32ce90a4 CH |
3090 | error = XFS_ERROR(EIO); |
3091 | goto abort_out; | |
1da177e4 LT |
3092 | } |
3093 | ||
a3f74ffb DC |
3094 | /* |
3095 | * Get the buffer containing the on-disk inode. | |
3096 | */ | |
475ee413 CH |
3097 | error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK, |
3098 | 0); | |
a3f74ffb DC |
3099 | if (error || !bp) { |
3100 | xfs_ifunlock(ip); | |
3101 | return error; | |
3102 | } | |
3103 | ||
1da177e4 LT |
3104 | /* |
3105 | * First flush out the inode that xfs_iflush was called with. | |
3106 | */ | |
3107 | error = xfs_iflush_int(ip, bp); | |
bad55843 | 3108 | if (error) |
1da177e4 | 3109 | goto corrupt_out; |
1da177e4 | 3110 | |
a3f74ffb DC |
3111 | /* |
3112 | * If the buffer is pinned then push on the log now so we won't | |
3113 | * get stuck waiting in the write for too long. | |
3114 | */ | |
811e64c7 | 3115 | if (xfs_buf_ispinned(bp)) |
a14a348b | 3116 | xfs_log_force(mp, 0); |
a3f74ffb | 3117 | |
1da177e4 LT |
3118 | /* |
3119 | * inode clustering: | |
3120 | * see if other inodes can be gathered into this write | |
3121 | */ | |
bad55843 DC |
3122 | error = xfs_iflush_cluster(ip, bp); |
3123 | if (error) | |
3124 | goto cluster_corrupt_out; | |
1da177e4 | 3125 | |
4c46819a CH |
3126 | *bpp = bp; |
3127 | return 0; | |
1da177e4 LT |
3128 | |
3129 | corrupt_out: | |
3130 | xfs_buf_relse(bp); | |
7d04a335 | 3131 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
1da177e4 | 3132 | cluster_corrupt_out: |
32ce90a4 CH |
3133 | error = XFS_ERROR(EFSCORRUPTED); |
3134 | abort_out: | |
1da177e4 LT |
3135 | /* |
3136 | * Unlocks the flush lock | |
3137 | */ | |
04913fdd | 3138 | xfs_iflush_abort(ip, false); |
32ce90a4 | 3139 | return error; |
1da177e4 LT |
3140 | } |
3141 | ||
1da177e4 LT |
3142 | STATIC int |
3143 | xfs_iflush_int( | |
93848a99 CH |
3144 | struct xfs_inode *ip, |
3145 | struct xfs_buf *bp) | |
1da177e4 | 3146 | { |
93848a99 CH |
3147 | struct xfs_inode_log_item *iip = ip->i_itemp; |
3148 | struct xfs_dinode *dip; | |
3149 | struct xfs_mount *mp = ip->i_mount; | |
1da177e4 | 3150 | |
579aa9ca | 3151 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
474fce06 | 3152 | ASSERT(xfs_isiflocked(ip)); |
1da177e4 | 3153 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
8096b1eb | 3154 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
93848a99 | 3155 | ASSERT(iip != NULL && iip->ili_fields != 0); |
1da177e4 | 3156 | |
1da177e4 | 3157 | /* set *dip = inode's place in the buffer */ |
92bfc6e7 | 3158 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset); |
1da177e4 | 3159 | |
69ef921b | 3160 | if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC), |
1da177e4 | 3161 | mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) { |
6a19d939 DC |
3162 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
3163 | "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p", | |
3164 | __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); | |
1da177e4 LT |
3165 | goto corrupt_out; |
3166 | } | |
3167 | if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC, | |
3168 | mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) { | |
6a19d939 DC |
3169 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
3170 | "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x", | |
3171 | __func__, ip->i_ino, ip, ip->i_d.di_magic); | |
1da177e4 LT |
3172 | goto corrupt_out; |
3173 | } | |
abbede1b | 3174 | if (S_ISREG(ip->i_d.di_mode)) { |
1da177e4 LT |
3175 | if (XFS_TEST_ERROR( |
3176 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && | |
3177 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE), | |
3178 | mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) { | |
6a19d939 DC |
3179 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
3180 | "%s: Bad regular inode %Lu, ptr 0x%p", | |
3181 | __func__, ip->i_ino, ip); | |
1da177e4 LT |
3182 | goto corrupt_out; |
3183 | } | |
abbede1b | 3184 | } else if (S_ISDIR(ip->i_d.di_mode)) { |
1da177e4 LT |
3185 | if (XFS_TEST_ERROR( |
3186 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && | |
3187 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && | |
3188 | (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL), | |
3189 | mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) { | |
6a19d939 DC |
3190 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
3191 | "%s: Bad directory inode %Lu, ptr 0x%p", | |
3192 | __func__, ip->i_ino, ip); | |
1da177e4 LT |
3193 | goto corrupt_out; |
3194 | } | |
3195 | } | |
3196 | if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents > | |
3197 | ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5, | |
3198 | XFS_RANDOM_IFLUSH_5)) { | |
6a19d939 DC |
3199 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
3200 | "%s: detected corrupt incore inode %Lu, " | |
3201 | "total extents = %d, nblocks = %Ld, ptr 0x%p", | |
3202 | __func__, ip->i_ino, | |
1da177e4 | 3203 | ip->i_d.di_nextents + ip->i_d.di_anextents, |
6a19d939 | 3204 | ip->i_d.di_nblocks, ip); |
1da177e4 LT |
3205 | goto corrupt_out; |
3206 | } | |
3207 | if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize, | |
3208 | mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) { | |
6a19d939 DC |
3209 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
3210 | "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p", | |
3211 | __func__, ip->i_ino, ip->i_d.di_forkoff, ip); | |
1da177e4 LT |
3212 | goto corrupt_out; |
3213 | } | |
e60896d8 | 3214 | |
1da177e4 | 3215 | /* |
e60896d8 DC |
3216 | * Inode item log recovery for v1/v2 inodes are dependent on the |
3217 | * di_flushiter count for correct sequencing. We bump the flush | |
3218 | * iteration count so we can detect flushes which postdate a log record | |
3219 | * during recovery. This is redundant as we now log every change and | |
3220 | * hence this can't happen but we need to still do it to ensure | |
3221 | * backwards compatibility with old kernels that predate logging all | |
3222 | * inode changes. | |
1da177e4 | 3223 | */ |
e60896d8 DC |
3224 | if (ip->i_d.di_version < 3) |
3225 | ip->i_d.di_flushiter++; | |
1da177e4 LT |
3226 | |
3227 | /* | |
3228 | * Copy the dirty parts of the inode into the on-disk | |
3229 | * inode. We always copy out the core of the inode, | |
3230 | * because if the inode is dirty at all the core must | |
3231 | * be. | |
3232 | */ | |
81591fe2 | 3233 | xfs_dinode_to_disk(dip, &ip->i_d); |
1da177e4 LT |
3234 | |
3235 | /* Wrap, we never let the log put out DI_MAX_FLUSH */ | |
3236 | if (ip->i_d.di_flushiter == DI_MAX_FLUSH) | |
3237 | ip->i_d.di_flushiter = 0; | |
3238 | ||
3239 | /* | |
3240 | * If this is really an old format inode and the superblock version | |
3241 | * has not been updated to support only new format inodes, then | |
3242 | * convert back to the old inode format. If the superblock version | |
3243 | * has been updated, then make the conversion permanent. | |
3244 | */ | |
51ce16d5 CH |
3245 | ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb)); |
3246 | if (ip->i_d.di_version == 1) { | |
62118709 | 3247 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { |
1da177e4 LT |
3248 | /* |
3249 | * Convert it back. | |
3250 | */ | |
3251 | ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1); | |
81591fe2 | 3252 | dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink); |
1da177e4 LT |
3253 | } else { |
3254 | /* | |
3255 | * The superblock version has already been bumped, | |
3256 | * so just make the conversion to the new inode | |
3257 | * format permanent. | |
3258 | */ | |
51ce16d5 CH |
3259 | ip->i_d.di_version = 2; |
3260 | dip->di_version = 2; | |
1da177e4 | 3261 | ip->i_d.di_onlink = 0; |
81591fe2 | 3262 | dip->di_onlink = 0; |
1da177e4 | 3263 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
81591fe2 CH |
3264 | memset(&(dip->di_pad[0]), 0, |
3265 | sizeof(dip->di_pad)); | |
6743099c | 3266 | ASSERT(xfs_get_projid(ip) == 0); |
1da177e4 LT |
3267 | } |
3268 | } | |
3269 | ||
e4ac967b DC |
3270 | xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp); |
3271 | if (XFS_IFORK_Q(ip)) | |
3272 | xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp); | |
1da177e4 LT |
3273 | xfs_inobp_check(mp, bp); |
3274 | ||
3275 | /* | |
f5d8d5c4 CH |
3276 | * We've recorded everything logged in the inode, so we'd like to clear |
3277 | * the ili_fields bits so we don't log and flush things unnecessarily. | |
3278 | * However, we can't stop logging all this information until the data | |
3279 | * we've copied into the disk buffer is written to disk. If we did we | |
3280 | * might overwrite the copy of the inode in the log with all the data | |
3281 | * after re-logging only part of it, and in the face of a crash we | |
3282 | * wouldn't have all the data we need to recover. | |
1da177e4 | 3283 | * |
f5d8d5c4 CH |
3284 | * What we do is move the bits to the ili_last_fields field. When |
3285 | * logging the inode, these bits are moved back to the ili_fields field. | |
3286 | * In the xfs_iflush_done() routine we clear ili_last_fields, since we | |
3287 | * know that the information those bits represent is permanently on | |
3288 | * disk. As long as the flush completes before the inode is logged | |
3289 | * again, then both ili_fields and ili_last_fields will be cleared. | |
1da177e4 | 3290 | * |
f5d8d5c4 CH |
3291 | * We can play with the ili_fields bits here, because the inode lock |
3292 | * must be held exclusively in order to set bits there and the flush | |
3293 | * lock protects the ili_last_fields bits. Set ili_logged so the flush | |
3294 | * done routine can tell whether or not to look in the AIL. Also, store | |
3295 | * the current LSN of the inode so that we can tell whether the item has | |
3296 | * moved in the AIL from xfs_iflush_done(). In order to read the lsn we | |
3297 | * need the AIL lock, because it is a 64 bit value that cannot be read | |
3298 | * atomically. | |
1da177e4 | 3299 | */ |
93848a99 CH |
3300 | iip->ili_last_fields = iip->ili_fields; |
3301 | iip->ili_fields = 0; | |
3302 | iip->ili_logged = 1; | |
1da177e4 | 3303 | |
93848a99 CH |
3304 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
3305 | &iip->ili_item.li_lsn); | |
1da177e4 | 3306 | |
93848a99 CH |
3307 | /* |
3308 | * Attach the function xfs_iflush_done to the inode's | |
3309 | * buffer. This will remove the inode from the AIL | |
3310 | * and unlock the inode's flush lock when the inode is | |
3311 | * completely written to disk. | |
3312 | */ | |
3313 | xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item); | |
1da177e4 | 3314 | |
93848a99 CH |
3315 | /* update the lsn in the on disk inode if required */ |
3316 | if (ip->i_d.di_version == 3) | |
3317 | dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn); | |
3318 | ||
3319 | /* generate the checksum. */ | |
3320 | xfs_dinode_calc_crc(mp, dip); | |
1da177e4 | 3321 | |
93848a99 CH |
3322 | ASSERT(bp->b_fspriv != NULL); |
3323 | ASSERT(bp->b_iodone != NULL); | |
1da177e4 LT |
3324 | return 0; |
3325 | ||
3326 | corrupt_out: | |
3327 | return XFS_ERROR(EFSCORRUPTED); | |
3328 | } |