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