]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
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 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 LT |
24 | #include "xfs_trans.h" |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
1da177e4 LT |
27 | #include "xfs_dir2.h" |
28 | #include "xfs_dmapi.h" | |
29 | #include "xfs_mount.h" | |
1da177e4 | 30 | #include "xfs_bmap_btree.h" |
a844f451 | 31 | #include "xfs_alloc_btree.h" |
1da177e4 | 32 | #include "xfs_ialloc_btree.h" |
1da177e4 | 33 | #include "xfs_dir2_sf.h" |
a844f451 | 34 | #include "xfs_attr_sf.h" |
1da177e4 LT |
35 | #include "xfs_dinode.h" |
36 | #include "xfs_inode.h" | |
a844f451 NS |
37 | #include "xfs_btree.h" |
38 | #include "xfs_ialloc.h" | |
1da177e4 LT |
39 | #include "xfs_quota.h" |
40 | #include "xfs_utils.h" | |
783a2f65 DC |
41 | #include "xfs_trans_priv.h" |
42 | #include "xfs_inode_item.h" | |
1da177e4 | 43 | |
1da177e4 | 44 | /* |
6441e549 | 45 | * Check the validity of the inode we just found it the cache |
1da177e4 | 46 | */ |
6441e549 DC |
47 | static int |
48 | xfs_iget_cache_hit( | |
6441e549 DC |
49 | struct xfs_perag *pag, |
50 | struct xfs_inode *ip, | |
51 | int flags, | |
52 | int lock_flags) __releases(pag->pag_ici_lock) | |
1da177e4 | 53 | { |
6441e549 | 54 | struct xfs_mount *mp = ip->i_mount; |
6441e549 | 55 | int error = 0; |
da353b0d | 56 | |
6441e549 DC |
57 | /* |
58 | * If INEW is set this inode is being set up | |
bf904248 | 59 | * If IRECLAIM is set this inode is being torn down |
6441e549 DC |
60 | * Pause and try again. |
61 | */ | |
bf904248 | 62 | if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) { |
6441e549 DC |
63 | error = EAGAIN; |
64 | XFS_STATS_INC(xs_ig_frecycle); | |
65 | goto out_error; | |
66 | } | |
da353b0d | 67 | |
bf904248 DC |
68 | /* If IRECLAIMABLE is set, we've torn down the vfs inode part */ |
69 | if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) { | |
70 | ||
da353b0d | 71 | /* |
bf904248 DC |
72 | * If lookup is racing with unlink, then we should return an |
73 | * error immediately so we don't remove it from the reclaim | |
74 | * list and potentially leak the inode. | |
da353b0d | 75 | */ |
bf904248 DC |
76 | |
77 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { | |
78 | error = ENOENT; | |
6441e549 DC |
79 | goto out_error; |
80 | } | |
bf904248 DC |
81 | |
82 | xfs_itrace_exit_tag(ip, "xfs_iget.alloc"); | |
da353b0d | 83 | |
6441e549 | 84 | /* |
bf904248 DC |
85 | * We need to re-initialise the VFS inode as it has been |
86 | * 'freed' by the VFS. Do this here so we can deal with | |
87 | * errors cleanly, then tag it so it can be set up correctly | |
88 | * later. | |
6441e549 | 89 | */ |
bf904248 DC |
90 | if (!inode_init_always(mp->m_super, VFS_I(ip))) { |
91 | error = ENOMEM; | |
6441e549 | 92 | goto out_error; |
da353b0d | 93 | } |
bf904248 | 94 | xfs_iflags_set(ip, XFS_INEW); |
6441e549 | 95 | xfs_iflags_clear(ip, XFS_IRECLAIMABLE); |
396beb85 DC |
96 | |
97 | /* clear the radix tree reclaim flag as well. */ | |
98 | __xfs_inode_clear_reclaim_tag(mp, pag, ip); | |
6441e549 | 99 | read_unlock(&pag->pag_ici_lock); |
bf904248 DC |
100 | } else if (!igrab(VFS_I(ip))) { |
101 | /* If the VFS inode is being torn down, pause and try again. */ | |
102 | error = EAGAIN; | |
103 | XFS_STATS_INC(xs_ig_frecycle); | |
104 | goto out_error; | |
6441e549 | 105 | } else { |
bf904248 | 106 | /* we've got a live one */ |
da353b0d | 107 | read_unlock(&pag->pag_ici_lock); |
6441e549 | 108 | } |
1da177e4 | 109 | |
6441e549 DC |
110 | if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) { |
111 | error = ENOENT; | |
112 | goto out; | |
113 | } | |
da353b0d | 114 | |
6441e549 DC |
115 | if (lock_flags != 0) |
116 | xfs_ilock(ip, lock_flags); | |
da353b0d | 117 | |
6441e549 DC |
118 | xfs_iflags_clear(ip, XFS_ISTALE); |
119 | xfs_itrace_exit_tag(ip, "xfs_iget.found"); | |
120 | XFS_STATS_INC(xs_ig_found); | |
121 | return 0; | |
1da177e4 | 122 | |
6441e549 | 123 | out_error: |
da353b0d | 124 | read_unlock(&pag->pag_ici_lock); |
6441e549 DC |
125 | out: |
126 | return error; | |
127 | } | |
128 | ||
129 | ||
130 | static int | |
131 | xfs_iget_cache_miss( | |
132 | struct xfs_mount *mp, | |
133 | struct xfs_perag *pag, | |
134 | xfs_trans_t *tp, | |
135 | xfs_ino_t ino, | |
136 | struct xfs_inode **ipp, | |
137 | xfs_daddr_t bno, | |
138 | int flags, | |
139 | int lock_flags) __releases(pag->pag_ici_lock) | |
140 | { | |
141 | struct xfs_inode *ip; | |
142 | int error; | |
143 | unsigned long first_index, mask; | |
144 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); | |
1da177e4 | 145 | |
1da177e4 LT |
146 | /* |
147 | * Read the disk inode attributes into a new inode structure and get | |
148 | * a new vnode for it. This should also initialize i_ino and i_mount. | |
149 | */ | |
745b1f47 NS |
150 | error = xfs_iread(mp, tp, ino, &ip, bno, |
151 | (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0); | |
6441e549 | 152 | if (error) |
1da177e4 | 153 | return error; |
1da177e4 | 154 | |
15947f2d | 155 | xfs_itrace_exit_tag(ip, "xfs_iget.alloc"); |
1da177e4 | 156 | |
745b1f47 | 157 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
6441e549 DC |
158 | error = ENOENT; |
159 | goto out_destroy; | |
1da177e4 LT |
160 | } |
161 | ||
56e73ec4 DC |
162 | if (lock_flags) |
163 | xfs_ilock(ip, lock_flags); | |
164 | ||
1da177e4 | 165 | /* |
bad55843 | 166 | * Preload the radix tree so we can insert safely under the |
56e73ec4 DC |
167 | * write spinlock. Note that we cannot sleep inside the preload |
168 | * region. | |
1da177e4 | 169 | */ |
da353b0d | 170 | if (radix_tree_preload(GFP_KERNEL)) { |
6441e549 | 171 | error = EAGAIN; |
56e73ec4 | 172 | goto out_unlock; |
da353b0d | 173 | } |
f338f903 | 174 | |
da353b0d DC |
175 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
176 | first_index = agino & mask; | |
177 | write_lock(&pag->pag_ici_lock); | |
6441e549 DC |
178 | |
179 | /* insert the new inode */ | |
da353b0d DC |
180 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); |
181 | if (unlikely(error)) { | |
6441e549 | 182 | WARN_ON(error != -EEXIST); |
da353b0d | 183 | XFS_STATS_INC(xs_ig_dup); |
6441e549 | 184 | error = EAGAIN; |
56e73ec4 | 185 | goto out_preload_end; |
1da177e4 LT |
186 | } |
187 | ||
6441e549 | 188 | /* These values _must_ be set before releasing the radix tree lock! */ |
1da177e4 | 189 | ip->i_udquot = ip->i_gdquot = NULL; |
7a18c386 | 190 | xfs_iflags_set(ip, XFS_INEW); |
1da177e4 | 191 | |
da353b0d DC |
192 | write_unlock(&pag->pag_ici_lock); |
193 | radix_tree_preload_end(); | |
6441e549 DC |
194 | *ipp = ip; |
195 | return 0; | |
196 | ||
56e73ec4 | 197 | out_preload_end: |
6441e549 DC |
198 | write_unlock(&pag->pag_ici_lock); |
199 | radix_tree_preload_end(); | |
56e73ec4 DC |
200 | out_unlock: |
201 | if (lock_flags) | |
202 | xfs_iunlock(ip, lock_flags); | |
6441e549 | 203 | out_destroy: |
9ed0451e | 204 | xfs_destroy_inode(ip); |
6441e549 DC |
205 | return error; |
206 | } | |
207 | ||
208 | /* | |
209 | * Look up an inode by number in the given file system. | |
210 | * The inode is looked up in the cache held in each AG. | |
bf904248 DC |
211 | * If the inode is found in the cache, initialise the vfs inode |
212 | * if necessary. | |
6441e549 DC |
213 | * |
214 | * If it is not in core, read it in from the file system's device, | |
bf904248 | 215 | * add it to the cache and initialise the vfs inode. |
6441e549 DC |
216 | * |
217 | * The inode is locked according to the value of the lock_flags parameter. | |
218 | * This flag parameter indicates how and if the inode's IO lock and inode lock | |
219 | * should be taken. | |
220 | * | |
221 | * mp -- the mount point structure for the current file system. It points | |
222 | * to the inode hash table. | |
223 | * tp -- a pointer to the current transaction if there is one. This is | |
224 | * simply passed through to the xfs_iread() call. | |
225 | * ino -- the number of the inode desired. This is the unique identifier | |
226 | * within the file system for the inode being requested. | |
227 | * lock_flags -- flags indicating how to lock the inode. See the comment | |
228 | * for xfs_ilock() for a list of valid values. | |
229 | * bno -- the block number starting the buffer containing the inode, | |
230 | * if known (as by bulkstat), else 0. | |
231 | */ | |
bf904248 DC |
232 | int |
233 | xfs_iget( | |
6441e549 DC |
234 | xfs_mount_t *mp, |
235 | xfs_trans_t *tp, | |
236 | xfs_ino_t ino, | |
237 | uint flags, | |
238 | uint lock_flags, | |
239 | xfs_inode_t **ipp, | |
240 | xfs_daddr_t bno) | |
241 | { | |
242 | xfs_inode_t *ip; | |
243 | int error; | |
244 | xfs_perag_t *pag; | |
245 | xfs_agino_t agino; | |
246 | ||
247 | /* the radix tree exists only in inode capable AGs */ | |
248 | if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi) | |
249 | return EINVAL; | |
250 | ||
251 | /* get the perag structure and ensure that it's inode capable */ | |
252 | pag = xfs_get_perag(mp, ino); | |
253 | if (!pag->pagi_inodeok) | |
254 | return EINVAL; | |
255 | ASSERT(pag->pag_ici_init); | |
256 | agino = XFS_INO_TO_AGINO(mp, ino); | |
257 | ||
258 | again: | |
259 | error = 0; | |
260 | read_lock(&pag->pag_ici_lock); | |
261 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); | |
262 | ||
263 | if (ip) { | |
bf904248 | 264 | error = xfs_iget_cache_hit(pag, ip, flags, lock_flags); |
6441e549 DC |
265 | if (error) |
266 | goto out_error_or_again; | |
267 | } else { | |
268 | read_unlock(&pag->pag_ici_lock); | |
269 | XFS_STATS_INC(xs_ig_missed); | |
270 | ||
271 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno, | |
272 | flags, lock_flags); | |
273 | if (error) | |
274 | goto out_error_or_again; | |
275 | } | |
da353b0d | 276 | xfs_put_perag(mp, pag); |
1da177e4 | 277 | |
b3aea4ed | 278 | xfs_iflags_set(ip, XFS_IMODIFIED); |
1da177e4 LT |
279 | *ipp = ip; |
280 | ||
bf904248 DC |
281 | ASSERT(ip->i_df.if_ext_max == |
282 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t)); | |
1da177e4 LT |
283 | /* |
284 | * If we have a real type for an on-disk inode, we can set ops(&unlock) | |
285 | * now. If it's a new inode being created, xfs_ialloc will handle it. | |
286 | */ | |
bf904248 | 287 | if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0) |
41be8bed | 288 | xfs_setup_inode(ip); |
1da177e4 | 289 | return 0; |
6441e549 DC |
290 | |
291 | out_error_or_again: | |
292 | if (error == EAGAIN) { | |
293 | delay(1); | |
294 | goto again; | |
295 | } | |
296 | xfs_put_perag(mp, pag); | |
297 | return error; | |
1da177e4 LT |
298 | } |
299 | ||
300 | ||
1da177e4 LT |
301 | /* |
302 | * Look for the inode corresponding to the given ino in the hash table. | |
303 | * If it is there and its i_transp pointer matches tp, return it. | |
304 | * Otherwise, return NULL. | |
305 | */ | |
306 | xfs_inode_t * | |
307 | xfs_inode_incore(xfs_mount_t *mp, | |
308 | xfs_ino_t ino, | |
309 | xfs_trans_t *tp) | |
310 | { | |
1da177e4 | 311 | xfs_inode_t *ip; |
da353b0d DC |
312 | xfs_perag_t *pag; |
313 | ||
314 | pag = xfs_get_perag(mp, ino); | |
315 | read_lock(&pag->pag_ici_lock); | |
316 | ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino)); | |
317 | read_unlock(&pag->pag_ici_lock); | |
318 | xfs_put_perag(mp, pag); | |
319 | ||
320 | /* the returned inode must match the transaction */ | |
321 | if (ip && (ip->i_transp != tp)) | |
322 | return NULL; | |
323 | return ip; | |
1da177e4 LT |
324 | } |
325 | ||
326 | /* | |
327 | * Decrement reference count of an inode structure and unlock it. | |
328 | * | |
329 | * ip -- the inode being released | |
330 | * lock_flags -- this parameter indicates the inode's locks to be | |
331 | * to be released. See the comment on xfs_iunlock() for a list | |
332 | * of valid values. | |
333 | */ | |
334 | void | |
335 | xfs_iput(xfs_inode_t *ip, | |
336 | uint lock_flags) | |
337 | { | |
cf441eeb | 338 | xfs_itrace_entry(ip); |
1da177e4 | 339 | xfs_iunlock(ip, lock_flags); |
10090be2 | 340 | IRELE(ip); |
1da177e4 LT |
341 | } |
342 | ||
343 | /* | |
344 | * Special iput for brand-new inodes that are still locked | |
345 | */ | |
346 | void | |
01651646 DC |
347 | xfs_iput_new( |
348 | xfs_inode_t *ip, | |
349 | uint lock_flags) | |
1da177e4 | 350 | { |
01651646 | 351 | struct inode *inode = VFS_I(ip); |
1da177e4 | 352 | |
cf441eeb | 353 | xfs_itrace_entry(ip); |
1da177e4 LT |
354 | |
355 | if ((ip->i_d.di_mode == 0)) { | |
7a18c386 | 356 | ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE)); |
10090be2 | 357 | make_bad_inode(inode); |
1da177e4 LT |
358 | } |
359 | if (inode->i_state & I_NEW) | |
360 | unlock_new_inode(inode); | |
361 | if (lock_flags) | |
362 | xfs_iunlock(ip, lock_flags); | |
10090be2 | 363 | IRELE(ip); |
1da177e4 LT |
364 | } |
365 | ||
366 | ||
367 | /* | |
368 | * This routine embodies the part of the reclaim code that pulls | |
369 | * the inode from the inode hash table and the mount structure's | |
370 | * inode list. | |
371 | * This should only be called from xfs_reclaim(). | |
372 | */ | |
373 | void | |
374 | xfs_ireclaim(xfs_inode_t *ip) | |
375 | { | |
1da177e4 LT |
376 | /* |
377 | * Remove from old hash list and mount list. | |
378 | */ | |
379 | XFS_STATS_INC(xs_ig_reclaims); | |
380 | ||
381 | xfs_iextract(ip); | |
382 | ||
383 | /* | |
a4e4c4f4 DC |
384 | * Here we do a spurious inode lock in order to coordinate with inode |
385 | * cache radix tree lookups. This is because the lookup can reference | |
386 | * the inodes in the cache without taking references. We make that OK | |
387 | * here by ensuring that we wait until the inode is unlocked after the | |
388 | * lookup before we go ahead and free it. We get both the ilock and | |
389 | * the iolock because the code may need to drop the ilock one but will | |
390 | * still hold the iolock. | |
1da177e4 LT |
391 | */ |
392 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | |
393 | ||
394 | /* | |
395 | * Release dquots (and their references) if any. An inode may escape | |
396 | * xfs_inactive and get here via vn_alloc->vn_reclaim path. | |
397 | */ | |
398 | XFS_QM_DQDETACH(ip->i_mount, ip); | |
399 | ||
1da177e4 LT |
400 | /* |
401 | * Free all memory associated with the inode. | |
402 | */ | |
439b8434 | 403 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
1da177e4 LT |
404 | xfs_idestroy(ip); |
405 | } | |
406 | ||
407 | /* | |
408 | * This routine removes an about-to-be-destroyed inode from | |
409 | * all of the lists in which it is located with the exception | |
410 | * of the behavior chain. | |
411 | */ | |
412 | void | |
413 | xfs_iextract( | |
414 | xfs_inode_t *ip) | |
415 | { | |
da353b0d DC |
416 | xfs_mount_t *mp = ip->i_mount; |
417 | xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino); | |
da353b0d DC |
418 | |
419 | write_lock(&pag->pag_ici_lock); | |
420 | radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino)); | |
421 | write_unlock(&pag->pag_ici_lock); | |
422 | xfs_put_perag(mp, pag); | |
1da177e4 | 423 | |
1da177e4 | 424 | mp->m_ireclaims++; |
1da177e4 LT |
425 | } |
426 | ||
427 | /* | |
428 | * This is a wrapper routine around the xfs_ilock() routine | |
429 | * used to centralize some grungy code. It is used in places | |
430 | * that wish to lock the inode solely for reading the extents. | |
431 | * The reason these places can't just call xfs_ilock(SHARED) | |
432 | * is that the inode lock also guards to bringing in of the | |
433 | * extents from disk for a file in b-tree format. If the inode | |
434 | * is in b-tree format, then we need to lock the inode exclusively | |
435 | * until the extents are read in. Locking it exclusively all | |
436 | * the time would limit our parallelism unnecessarily, though. | |
437 | * What we do instead is check to see if the extents have been | |
438 | * read in yet, and only lock the inode exclusively if they | |
439 | * have not. | |
440 | * | |
441 | * The function returns a value which should be given to the | |
442 | * corresponding xfs_iunlock_map_shared(). This value is | |
443 | * the mode in which the lock was actually taken. | |
444 | */ | |
445 | uint | |
446 | xfs_ilock_map_shared( | |
447 | xfs_inode_t *ip) | |
448 | { | |
449 | uint lock_mode; | |
450 | ||
451 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && | |
452 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { | |
453 | lock_mode = XFS_ILOCK_EXCL; | |
454 | } else { | |
455 | lock_mode = XFS_ILOCK_SHARED; | |
456 | } | |
457 | ||
458 | xfs_ilock(ip, lock_mode); | |
459 | ||
460 | return lock_mode; | |
461 | } | |
462 | ||
463 | /* | |
464 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). | |
465 | * All it does is call xfs_iunlock() with the given lock_mode. | |
466 | */ | |
467 | void | |
468 | xfs_iunlock_map_shared( | |
469 | xfs_inode_t *ip, | |
470 | unsigned int lock_mode) | |
471 | { | |
472 | xfs_iunlock(ip, lock_mode); | |
473 | } | |
474 | ||
475 | /* | |
476 | * The xfs inode contains 2 locks: a multi-reader lock called the | |
477 | * i_iolock and a multi-reader lock called the i_lock. This routine | |
478 | * allows either or both of the locks to be obtained. | |
479 | * | |
480 | * The 2 locks should always be ordered so that the IO lock is | |
481 | * obtained first in order to prevent deadlock. | |
482 | * | |
483 | * ip -- the inode being locked | |
484 | * lock_flags -- this parameter indicates the inode's locks | |
485 | * to be locked. It can be: | |
486 | * XFS_IOLOCK_SHARED, | |
487 | * XFS_IOLOCK_EXCL, | |
488 | * XFS_ILOCK_SHARED, | |
489 | * XFS_ILOCK_EXCL, | |
490 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, | |
491 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, | |
492 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, | |
493 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL | |
494 | */ | |
495 | void | |
579aa9ca CH |
496 | xfs_ilock( |
497 | xfs_inode_t *ip, | |
498 | uint lock_flags) | |
1da177e4 LT |
499 | { |
500 | /* | |
501 | * You can't set both SHARED and EXCL for the same lock, | |
502 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
503 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
504 | */ | |
505 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
506 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
507 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
508 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
f7c66ce3 | 509 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
1da177e4 | 510 | |
579aa9ca | 511 | if (lock_flags & XFS_IOLOCK_EXCL) |
f7c66ce3 | 512 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
579aa9ca | 513 | else if (lock_flags & XFS_IOLOCK_SHARED) |
f7c66ce3 | 514 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
579aa9ca CH |
515 | |
516 | if (lock_flags & XFS_ILOCK_EXCL) | |
f7c66ce3 | 517 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
579aa9ca | 518 | else if (lock_flags & XFS_ILOCK_SHARED) |
f7c66ce3 | 519 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
579aa9ca | 520 | |
1da177e4 LT |
521 | xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address); |
522 | } | |
523 | ||
524 | /* | |
525 | * This is just like xfs_ilock(), except that the caller | |
526 | * is guaranteed not to sleep. It returns 1 if it gets | |
527 | * the requested locks and 0 otherwise. If the IO lock is | |
528 | * obtained but the inode lock cannot be, then the IO lock | |
529 | * is dropped before returning. | |
530 | * | |
531 | * ip -- the inode being locked | |
532 | * lock_flags -- this parameter indicates the inode's locks to be | |
533 | * to be locked. See the comment for xfs_ilock() for a list | |
534 | * of valid values. | |
1da177e4 LT |
535 | */ |
536 | int | |
579aa9ca CH |
537 | xfs_ilock_nowait( |
538 | xfs_inode_t *ip, | |
539 | uint lock_flags) | |
1da177e4 | 540 | { |
1da177e4 LT |
541 | /* |
542 | * You can't set both SHARED and EXCL for the same lock, | |
543 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
544 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
545 | */ | |
546 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
547 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
548 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
549 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
f7c66ce3 | 550 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
1da177e4 | 551 | |
1da177e4 | 552 | if (lock_flags & XFS_IOLOCK_EXCL) { |
579aa9ca CH |
553 | if (!mrtryupdate(&ip->i_iolock)) |
554 | goto out; | |
1da177e4 | 555 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
579aa9ca CH |
556 | if (!mrtryaccess(&ip->i_iolock)) |
557 | goto out; | |
1da177e4 LT |
558 | } |
559 | if (lock_flags & XFS_ILOCK_EXCL) { | |
579aa9ca CH |
560 | if (!mrtryupdate(&ip->i_lock)) |
561 | goto out_undo_iolock; | |
1da177e4 | 562 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
579aa9ca CH |
563 | if (!mrtryaccess(&ip->i_lock)) |
564 | goto out_undo_iolock; | |
1da177e4 LT |
565 | } |
566 | xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address); | |
567 | return 1; | |
579aa9ca CH |
568 | |
569 | out_undo_iolock: | |
570 | if (lock_flags & XFS_IOLOCK_EXCL) | |
571 | mrunlock_excl(&ip->i_iolock); | |
572 | else if (lock_flags & XFS_IOLOCK_SHARED) | |
573 | mrunlock_shared(&ip->i_iolock); | |
574 | out: | |
575 | return 0; | |
1da177e4 LT |
576 | } |
577 | ||
578 | /* | |
579 | * xfs_iunlock() is used to drop the inode locks acquired with | |
580 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass | |
581 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so | |
582 | * that we know which locks to drop. | |
583 | * | |
584 | * ip -- the inode being unlocked | |
585 | * lock_flags -- this parameter indicates the inode's locks to be | |
586 | * to be unlocked. See the comment for xfs_ilock() for a list | |
587 | * of valid values for this parameter. | |
588 | * | |
589 | */ | |
590 | void | |
579aa9ca CH |
591 | xfs_iunlock( |
592 | xfs_inode_t *ip, | |
593 | uint lock_flags) | |
1da177e4 LT |
594 | { |
595 | /* | |
596 | * You can't set both SHARED and EXCL for the same lock, | |
597 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
598 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
599 | */ | |
600 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
601 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
602 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
603 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
f7c66ce3 LM |
604 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY | |
605 | XFS_LOCK_DEP_MASK)) == 0); | |
1da177e4 LT |
606 | ASSERT(lock_flags != 0); |
607 | ||
579aa9ca CH |
608 | if (lock_flags & XFS_IOLOCK_EXCL) |
609 | mrunlock_excl(&ip->i_iolock); | |
610 | else if (lock_flags & XFS_IOLOCK_SHARED) | |
611 | mrunlock_shared(&ip->i_iolock); | |
1da177e4 | 612 | |
579aa9ca CH |
613 | if (lock_flags & XFS_ILOCK_EXCL) |
614 | mrunlock_excl(&ip->i_lock); | |
615 | else if (lock_flags & XFS_ILOCK_SHARED) | |
616 | mrunlock_shared(&ip->i_lock); | |
1da177e4 | 617 | |
579aa9ca CH |
618 | if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) && |
619 | !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) { | |
1da177e4 LT |
620 | /* |
621 | * Let the AIL know that this item has been unlocked in case | |
622 | * it is in the AIL and anyone is waiting on it. Don't do | |
623 | * this if the caller has asked us not to. | |
624 | */ | |
783a2f65 | 625 | xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp, |
579aa9ca | 626 | (xfs_log_item_t*)(ip->i_itemp)); |
1da177e4 LT |
627 | } |
628 | xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address); | |
629 | } | |
630 | ||
631 | /* | |
632 | * give up write locks. the i/o lock cannot be held nested | |
633 | * if it is being demoted. | |
634 | */ | |
635 | void | |
579aa9ca CH |
636 | xfs_ilock_demote( |
637 | xfs_inode_t *ip, | |
638 | uint lock_flags) | |
1da177e4 LT |
639 | { |
640 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); | |
641 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); | |
642 | ||
579aa9ca | 643 | if (lock_flags & XFS_ILOCK_EXCL) |
1da177e4 | 644 | mrdemote(&ip->i_lock); |
579aa9ca | 645 | if (lock_flags & XFS_IOLOCK_EXCL) |
1da177e4 | 646 | mrdemote(&ip->i_iolock); |
579aa9ca CH |
647 | } |
648 | ||
649 | #ifdef DEBUG | |
650 | /* | |
651 | * Debug-only routine, without additional rw_semaphore APIs, we can | |
652 | * now only answer requests regarding whether we hold the lock for write | |
653 | * (reader state is outside our visibility, we only track writer state). | |
654 | * | |
655 | * Note: this means !xfs_isilocked would give false positives, so don't do that. | |
656 | */ | |
657 | int | |
658 | xfs_isilocked( | |
659 | xfs_inode_t *ip, | |
660 | uint lock_flags) | |
661 | { | |
662 | if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) == | |
663 | XFS_ILOCK_EXCL) { | |
664 | if (!ip->i_lock.mr_writer) | |
665 | return 0; | |
1da177e4 | 666 | } |
579aa9ca CH |
667 | |
668 | if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) == | |
669 | XFS_IOLOCK_EXCL) { | |
670 | if (!ip->i_iolock.mr_writer) | |
671 | return 0; | |
672 | } | |
673 | ||
674 | return 1; | |
1da177e4 | 675 | } |
579aa9ca | 676 | #endif |
1da177e4 | 677 |