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