diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/xfs/xfs_utils.c |
Linux-2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'fs/xfs/xfs_utils.c')
-rw-r--r-- | fs/xfs/xfs_utils.c | 488 |
1 files changed, 488 insertions, 0 deletions
diff --git a/fs/xfs/xfs_utils.c b/fs/xfs/xfs_utils.c new file mode 100644 index 00000000000..816b945fa0e --- /dev/null +++ b/fs/xfs/xfs_utils.c | |||
@@ -0,0 +1,488 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved. | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify it | ||
5 | * under the terms of version 2 of the GNU General Public License as | ||
6 | * published by the Free Software Foundation. | ||
7 | * | ||
8 | * This program is distributed in the hope that it would be useful, but | ||
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
11 | * | ||
12 | * Further, this software is distributed without any warranty that it is | ||
13 | * free of the rightful claim of any third person regarding infringement | ||
14 | * or the like. Any license provided herein, whether implied or | ||
15 | * otherwise, applies only to this software file. Patent licenses, if | ||
16 | * any, provided herein do not apply to combinations of this program with | ||
17 | * other software, or any other product whatsoever. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License along | ||
20 | * with this program; if not, write the Free Software Foundation, Inc., 59 | ||
21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. | ||
22 | * | ||
23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, | ||
24 | * Mountain View, CA 94043, or: | ||
25 | * | ||
26 | * http://www.sgi.com | ||
27 | * | ||
28 | * For further information regarding this notice, see: | ||
29 | * | ||
30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ | ||
31 | */ | ||
32 | |||
33 | #include "xfs.h" | ||
34 | #include "xfs_macros.h" | ||
35 | #include "xfs_types.h" | ||
36 | #include "xfs_inum.h" | ||
37 | #include "xfs_log.h" | ||
38 | #include "xfs_trans.h" | ||
39 | #include "xfs_sb.h" | ||
40 | #include "xfs_dir.h" | ||
41 | #include "xfs_dir2.h" | ||
42 | #include "xfs_dmapi.h" | ||
43 | #include "xfs_mount.h" | ||
44 | #include "xfs_bmap_btree.h" | ||
45 | #include "xfs_attr_sf.h" | ||
46 | #include "xfs_dir_sf.h" | ||
47 | #include "xfs_dir2_sf.h" | ||
48 | #include "xfs_dinode.h" | ||
49 | #include "xfs_inode_item.h" | ||
50 | #include "xfs_inode.h" | ||
51 | #include "xfs_bmap.h" | ||
52 | #include "xfs_error.h" | ||
53 | #include "xfs_quota.h" | ||
54 | #include "xfs_rw.h" | ||
55 | #include "xfs_itable.h" | ||
56 | #include "xfs_utils.h" | ||
57 | |||
58 | /* | ||
59 | * xfs_get_dir_entry is used to get a reference to an inode given | ||
60 | * its parent directory inode and the name of the file. It does | ||
61 | * not lock the child inode, and it unlocks the directory before | ||
62 | * returning. The directory's generation number is returned for | ||
63 | * use by a later call to xfs_lock_dir_and_entry. | ||
64 | */ | ||
65 | int | ||
66 | xfs_get_dir_entry( | ||
67 | vname_t *dentry, | ||
68 | xfs_inode_t **ipp) | ||
69 | { | ||
70 | vnode_t *vp; | ||
71 | bhv_desc_t *bdp; | ||
72 | |||
73 | vp = VNAME_TO_VNODE(dentry); | ||
74 | bdp = vn_bhv_lookup_unlocked(VN_BHV_HEAD(vp), &xfs_vnodeops); | ||
75 | if (!bdp) { | ||
76 | *ipp = NULL; | ||
77 | return XFS_ERROR(ENOENT); | ||
78 | } | ||
79 | VN_HOLD(vp); | ||
80 | *ipp = XFS_BHVTOI(bdp); | ||
81 | return 0; | ||
82 | } | ||
83 | |||
84 | int | ||
85 | xfs_dir_lookup_int( | ||
86 | bhv_desc_t *dir_bdp, | ||
87 | uint lock_mode, | ||
88 | vname_t *dentry, | ||
89 | xfs_ino_t *inum, | ||
90 | xfs_inode_t **ipp) | ||
91 | { | ||
92 | vnode_t *dir_vp; | ||
93 | xfs_inode_t *dp; | ||
94 | int error; | ||
95 | |||
96 | dir_vp = BHV_TO_VNODE(dir_bdp); | ||
97 | vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address); | ||
98 | |||
99 | dp = XFS_BHVTOI(dir_bdp); | ||
100 | |||
101 | error = XFS_DIR_LOOKUP(dp->i_mount, NULL, dp, | ||
102 | VNAME(dentry), VNAMELEN(dentry), inum); | ||
103 | if (!error) { | ||
104 | /* | ||
105 | * Unlock the directory. We do this because we can't | ||
106 | * hold the directory lock while doing the vn_get() | ||
107 | * in xfs_iget(). Doing so could cause us to hold | ||
108 | * a lock while waiting for the inode to finish | ||
109 | * being inactive while it's waiting for a log | ||
110 | * reservation in the inactive routine. | ||
111 | */ | ||
112 | xfs_iunlock(dp, lock_mode); | ||
113 | error = xfs_iget(dp->i_mount, NULL, *inum, 0, 0, ipp, 0); | ||
114 | xfs_ilock(dp, lock_mode); | ||
115 | |||
116 | if (error) { | ||
117 | *ipp = NULL; | ||
118 | } else if ((*ipp)->i_d.di_mode == 0) { | ||
119 | /* | ||
120 | * The inode has been freed. Something is | ||
121 | * wrong so just get out of here. | ||
122 | */ | ||
123 | xfs_iunlock(dp, lock_mode); | ||
124 | xfs_iput_new(*ipp, 0); | ||
125 | *ipp = NULL; | ||
126 | xfs_ilock(dp, lock_mode); | ||
127 | error = XFS_ERROR(ENOENT); | ||
128 | } | ||
129 | } | ||
130 | return error; | ||
131 | } | ||
132 | |||
133 | /* | ||
134 | * Allocates a new inode from disk and return a pointer to the | ||
135 | * incore copy. This routine will internally commit the current | ||
136 | * transaction and allocate a new one if the Space Manager needed | ||
137 | * to do an allocation to replenish the inode free-list. | ||
138 | * | ||
139 | * This routine is designed to be called from xfs_create and | ||
140 | * xfs_create_dir. | ||
141 | * | ||
142 | */ | ||
143 | int | ||
144 | xfs_dir_ialloc( | ||
145 | xfs_trans_t **tpp, /* input: current transaction; | ||
146 | output: may be a new transaction. */ | ||
147 | xfs_inode_t *dp, /* directory within whose allocate | ||
148 | the inode. */ | ||
149 | mode_t mode, | ||
150 | nlink_t nlink, | ||
151 | xfs_dev_t rdev, | ||
152 | cred_t *credp, | ||
153 | prid_t prid, /* project id */ | ||
154 | int okalloc, /* ok to allocate new space */ | ||
155 | xfs_inode_t **ipp, /* pointer to inode; it will be | ||
156 | locked. */ | ||
157 | int *committed) | ||
158 | |||
159 | { | ||
160 | xfs_trans_t *tp; | ||
161 | xfs_trans_t *ntp; | ||
162 | xfs_inode_t *ip; | ||
163 | xfs_buf_t *ialloc_context = NULL; | ||
164 | boolean_t call_again = B_FALSE; | ||
165 | int code; | ||
166 | uint log_res; | ||
167 | uint log_count; | ||
168 | void *dqinfo; | ||
169 | uint tflags; | ||
170 | |||
171 | tp = *tpp; | ||
172 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); | ||
173 | |||
174 | /* | ||
175 | * xfs_ialloc will return a pointer to an incore inode if | ||
176 | * the Space Manager has an available inode on the free | ||
177 | * list. Otherwise, it will do an allocation and replenish | ||
178 | * the freelist. Since we can only do one allocation per | ||
179 | * transaction without deadlocks, we will need to commit the | ||
180 | * current transaction and start a new one. We will then | ||
181 | * need to call xfs_ialloc again to get the inode. | ||
182 | * | ||
183 | * If xfs_ialloc did an allocation to replenish the freelist, | ||
184 | * it returns the bp containing the head of the freelist as | ||
185 | * ialloc_context. We will hold a lock on it across the | ||
186 | * transaction commit so that no other process can steal | ||
187 | * the inode(s) that we've just allocated. | ||
188 | */ | ||
189 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc, | ||
190 | &ialloc_context, &call_again, &ip); | ||
191 | |||
192 | /* | ||
193 | * Return an error if we were unable to allocate a new inode. | ||
194 | * This should only happen if we run out of space on disk or | ||
195 | * encounter a disk error. | ||
196 | */ | ||
197 | if (code) { | ||
198 | *ipp = NULL; | ||
199 | return code; | ||
200 | } | ||
201 | if (!call_again && (ip == NULL)) { | ||
202 | *ipp = NULL; | ||
203 | return XFS_ERROR(ENOSPC); | ||
204 | } | ||
205 | |||
206 | /* | ||
207 | * If call_again is set, then we were unable to get an | ||
208 | * inode in one operation. We need to commit the current | ||
209 | * transaction and call xfs_ialloc() again. It is guaranteed | ||
210 | * to succeed the second time. | ||
211 | */ | ||
212 | if (call_again) { | ||
213 | |||
214 | /* | ||
215 | * Normally, xfs_trans_commit releases all the locks. | ||
216 | * We call bhold to hang on to the ialloc_context across | ||
217 | * the commit. Holding this buffer prevents any other | ||
218 | * processes from doing any allocations in this | ||
219 | * allocation group. | ||
220 | */ | ||
221 | xfs_trans_bhold(tp, ialloc_context); | ||
222 | /* | ||
223 | * Save the log reservation so we can use | ||
224 | * them in the next transaction. | ||
225 | */ | ||
226 | log_res = xfs_trans_get_log_res(tp); | ||
227 | log_count = xfs_trans_get_log_count(tp); | ||
228 | |||
229 | /* | ||
230 | * We want the quota changes to be associated with the next | ||
231 | * transaction, NOT this one. So, detach the dqinfo from this | ||
232 | * and attach it to the next transaction. | ||
233 | */ | ||
234 | dqinfo = NULL; | ||
235 | tflags = 0; | ||
236 | if (tp->t_dqinfo) { | ||
237 | dqinfo = (void *)tp->t_dqinfo; | ||
238 | tp->t_dqinfo = NULL; | ||
239 | tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY; | ||
240 | tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY); | ||
241 | } | ||
242 | |||
243 | ntp = xfs_trans_dup(tp); | ||
244 | code = xfs_trans_commit(tp, 0, NULL); | ||
245 | tp = ntp; | ||
246 | if (committed != NULL) { | ||
247 | *committed = 1; | ||
248 | } | ||
249 | /* | ||
250 | * If we get an error during the commit processing, | ||
251 | * release the buffer that is still held and return | ||
252 | * to the caller. | ||
253 | */ | ||
254 | if (code) { | ||
255 | xfs_buf_relse(ialloc_context); | ||
256 | if (dqinfo) { | ||
257 | tp->t_dqinfo = dqinfo; | ||
258 | XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp); | ||
259 | } | ||
260 | *tpp = ntp; | ||
261 | *ipp = NULL; | ||
262 | return code; | ||
263 | } | ||
264 | code = xfs_trans_reserve(tp, 0, log_res, 0, | ||
265 | XFS_TRANS_PERM_LOG_RES, log_count); | ||
266 | /* | ||
267 | * Re-attach the quota info that we detached from prev trx. | ||
268 | */ | ||
269 | if (dqinfo) { | ||
270 | tp->t_dqinfo = dqinfo; | ||
271 | tp->t_flags |= tflags; | ||
272 | } | ||
273 | |||
274 | if (code) { | ||
275 | xfs_buf_relse(ialloc_context); | ||
276 | *tpp = ntp; | ||
277 | *ipp = NULL; | ||
278 | return code; | ||
279 | } | ||
280 | xfs_trans_bjoin(tp, ialloc_context); | ||
281 | |||
282 | /* | ||
283 | * Call ialloc again. Since we've locked out all | ||
284 | * other allocations in this allocation group, | ||
285 | * this call should always succeed. | ||
286 | */ | ||
287 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, | ||
288 | okalloc, &ialloc_context, &call_again, &ip); | ||
289 | |||
290 | /* | ||
291 | * If we get an error at this point, return to the caller | ||
292 | * so that the current transaction can be aborted. | ||
293 | */ | ||
294 | if (code) { | ||
295 | *tpp = tp; | ||
296 | *ipp = NULL; | ||
297 | return code; | ||
298 | } | ||
299 | ASSERT ((!call_again) && (ip != NULL)); | ||
300 | |||
301 | } else { | ||
302 | if (committed != NULL) { | ||
303 | *committed = 0; | ||
304 | } | ||
305 | } | ||
306 | |||
307 | *ipp = ip; | ||
308 | *tpp = tp; | ||
309 | |||
310 | return 0; | ||
311 | } | ||
312 | |||
313 | /* | ||
314 | * Decrement the link count on an inode & log the change. | ||
315 | * If this causes the link count to go to zero, initiate the | ||
316 | * logging activity required to truncate a file. | ||
317 | */ | ||
318 | int /* error */ | ||
319 | xfs_droplink( | ||
320 | xfs_trans_t *tp, | ||
321 | xfs_inode_t *ip) | ||
322 | { | ||
323 | int error; | ||
324 | |||
325 | xfs_ichgtime(ip, XFS_ICHGTIME_CHG); | ||
326 | |||
327 | ASSERT (ip->i_d.di_nlink > 0); | ||
328 | ip->i_d.di_nlink--; | ||
329 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | ||
330 | |||
331 | error = 0; | ||
332 | if (ip->i_d.di_nlink == 0) { | ||
333 | /* | ||
334 | * We're dropping the last link to this file. | ||
335 | * Move the on-disk inode to the AGI unlinked list. | ||
336 | * From xfs_inactive() we will pull the inode from | ||
337 | * the list and free it. | ||
338 | */ | ||
339 | error = xfs_iunlink(tp, ip); | ||
340 | } | ||
341 | return error; | ||
342 | } | ||
343 | |||
344 | /* | ||
345 | * This gets called when the inode's version needs to be changed from 1 to 2. | ||
346 | * Currently this happens when the nlink field overflows the old 16-bit value | ||
347 | * or when chproj is called to change the project for the first time. | ||
348 | * As a side effect the superblock version will also get rev'd | ||
349 | * to contain the NLINK bit. | ||
350 | */ | ||
351 | void | ||
352 | xfs_bump_ino_vers2( | ||
353 | xfs_trans_t *tp, | ||
354 | xfs_inode_t *ip) | ||
355 | { | ||
356 | xfs_mount_t *mp; | ||
357 | unsigned long s; | ||
358 | |||
359 | ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE)); | ||
360 | ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1); | ||
361 | |||
362 | ip->i_d.di_version = XFS_DINODE_VERSION_2; | ||
363 | ip->i_d.di_onlink = 0; | ||
364 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); | ||
365 | mp = tp->t_mountp; | ||
366 | if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) { | ||
367 | s = XFS_SB_LOCK(mp); | ||
368 | if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) { | ||
369 | XFS_SB_VERSION_ADDNLINK(&mp->m_sb); | ||
370 | XFS_SB_UNLOCK(mp, s); | ||
371 | xfs_mod_sb(tp, XFS_SB_VERSIONNUM); | ||
372 | } else { | ||
373 | XFS_SB_UNLOCK(mp, s); | ||
374 | } | ||
375 | } | ||
376 | /* Caller must log the inode */ | ||
377 | } | ||
378 | |||
379 | /* | ||
380 | * Increment the link count on an inode & log the change. | ||
381 | */ | ||
382 | int | ||
383 | xfs_bumplink( | ||
384 | xfs_trans_t *tp, | ||
385 | xfs_inode_t *ip) | ||
386 | { | ||
387 | if (ip->i_d.di_nlink >= XFS_MAXLINK) | ||
388 | return XFS_ERROR(EMLINK); | ||
389 | xfs_ichgtime(ip, XFS_ICHGTIME_CHG); | ||
390 | |||
391 | ASSERT(ip->i_d.di_nlink > 0); | ||
392 | ip->i_d.di_nlink++; | ||
393 | if ((ip->i_d.di_version == XFS_DINODE_VERSION_1) && | ||
394 | (ip->i_d.di_nlink > XFS_MAXLINK_1)) { | ||
395 | /* | ||
396 | * The inode has increased its number of links beyond | ||
397 | * what can fit in an old format inode. It now needs | ||
398 | * to be converted to a version 2 inode with a 32 bit | ||
399 | * link count. If this is the first inode in the file | ||
400 | * system to do this, then we need to bump the superblock | ||
401 | * version number as well. | ||
402 | */ | ||
403 | xfs_bump_ino_vers2(tp, ip); | ||
404 | } | ||
405 | |||
406 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | ||
407 | return 0; | ||
408 | } | ||
409 | |||
410 | /* | ||
411 | * Try to truncate the given file to 0 length. Currently called | ||
412 | * only out of xfs_remove when it has to truncate a file to free | ||
413 | * up space for the remove to proceed. | ||
414 | */ | ||
415 | int | ||
416 | xfs_truncate_file( | ||
417 | xfs_mount_t *mp, | ||
418 | xfs_inode_t *ip) | ||
419 | { | ||
420 | xfs_trans_t *tp; | ||
421 | int error; | ||
422 | |||
423 | #ifdef QUOTADEBUG | ||
424 | /* | ||
425 | * This is called to truncate the quotainodes too. | ||
426 | */ | ||
427 | if (XFS_IS_UQUOTA_ON(mp)) { | ||
428 | if (ip->i_ino != mp->m_sb.sb_uquotino) | ||
429 | ASSERT(ip->i_udquot); | ||
430 | } | ||
431 | if (XFS_IS_GQUOTA_ON(mp)) { | ||
432 | if (ip->i_ino != mp->m_sb.sb_gquotino) | ||
433 | ASSERT(ip->i_gdquot); | ||
434 | } | ||
435 | #endif | ||
436 | /* | ||
437 | * Make the call to xfs_itruncate_start before starting the | ||
438 | * transaction, because we cannot make the call while we're | ||
439 | * in a transaction. | ||
440 | */ | ||
441 | xfs_ilock(ip, XFS_IOLOCK_EXCL); | ||
442 | xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0); | ||
443 | |||
444 | tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE); | ||
445 | if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, | ||
446 | XFS_TRANS_PERM_LOG_RES, | ||
447 | XFS_ITRUNCATE_LOG_COUNT))) { | ||
448 | xfs_trans_cancel(tp, 0); | ||
449 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); | ||
450 | return error; | ||
451 | } | ||
452 | |||
453 | /* | ||
454 | * Follow the normal truncate locking protocol. Since we | ||
455 | * hold the inode in the transaction, we know that it's number | ||
456 | * of references will stay constant. | ||
457 | */ | ||
458 | xfs_ilock(ip, XFS_ILOCK_EXCL); | ||
459 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | ||
460 | xfs_trans_ihold(tp, ip); | ||
461 | /* | ||
462 | * Signal a sync xaction. The only case where that isn't | ||
463 | * the case is if we're truncating an already unlinked file | ||
464 | * on a wsync fs. In that case, we know the blocks can't | ||
465 | * reappear in the file because the links to file are | ||
466 | * permanently toast. Currently, we're always going to | ||
467 | * want a sync transaction because this code is being | ||
468 | * called from places where nlink is guaranteed to be 1 | ||
469 | * but I'm leaving the tests in to protect against future | ||
470 | * changes -- rcc. | ||
471 | */ | ||
472 | error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0, | ||
473 | XFS_DATA_FORK, | ||
474 | ((ip->i_d.di_nlink != 0 || | ||
475 | !(mp->m_flags & XFS_MOUNT_WSYNC)) | ||
476 | ? 1 : 0)); | ||
477 | if (error) { | ||
478 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | | ||
479 | XFS_TRANS_ABORT); | ||
480 | } else { | ||
481 | xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | ||
482 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, | ||
483 | NULL); | ||
484 | } | ||
485 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | ||
486 | |||
487 | return error; | ||
488 | } | ||