blob: 4b1df677abd9213bc2956d656d3c9869a9367da2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
29#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_bmap.h"
36#include "xfs_error.h"
37#include "xfs_quota.h"
38#include "xfs_rw.h"
39#include "xfs_itable.h"
40#include "xfs_utils.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Allocates a new inode from disk and return a pointer to the
45 * incore copy. This routine will internally commit the current
46 * transaction and allocate a new one if the Space Manager needed
47 * to do an allocation to replenish the inode free-list.
48 *
49 * This routine is designed to be called from xfs_create and
50 * xfs_create_dir.
51 *
52 */
53int
54xfs_dir_ialloc(
55 xfs_trans_t **tpp, /* input: current transaction;
56 output: may be a new transaction. */
57 xfs_inode_t *dp, /* directory within whose allocate
58 the inode. */
59 mode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -070060 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 xfs_dev_t rdev,
62 cred_t *credp,
63 prid_t prid, /* project id */
64 int okalloc, /* ok to allocate new space */
65 xfs_inode_t **ipp, /* pointer to inode; it will be
66 locked. */
67 int *committed)
68
69{
70 xfs_trans_t *tp;
71 xfs_trans_t *ntp;
72 xfs_inode_t *ip;
73 xfs_buf_t *ialloc_context = NULL;
74 boolean_t call_again = B_FALSE;
75 int code;
76 uint log_res;
77 uint log_count;
78 void *dqinfo;
79 uint tflags;
80
81 tp = *tpp;
82 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
83
84 /*
85 * xfs_ialloc will return a pointer to an incore inode if
86 * the Space Manager has an available inode on the free
87 * list. Otherwise, it will do an allocation and replenish
88 * the freelist. Since we can only do one allocation per
89 * transaction without deadlocks, we will need to commit the
90 * current transaction and start a new one. We will then
91 * need to call xfs_ialloc again to get the inode.
92 *
93 * If xfs_ialloc did an allocation to replenish the freelist,
94 * it returns the bp containing the head of the freelist as
95 * ialloc_context. We will hold a lock on it across the
96 * transaction commit so that no other process can steal
97 * the inode(s) that we've just allocated.
98 */
99 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc,
100 &ialloc_context, &call_again, &ip);
101
102 /*
103 * Return an error if we were unable to allocate a new inode.
104 * This should only happen if we run out of space on disk or
105 * encounter a disk error.
106 */
107 if (code) {
108 *ipp = NULL;
109 return code;
110 }
111 if (!call_again && (ip == NULL)) {
112 *ipp = NULL;
113 return XFS_ERROR(ENOSPC);
114 }
115
116 /*
117 * If call_again is set, then we were unable to get an
118 * inode in one operation. We need to commit the current
119 * transaction and call xfs_ialloc() again. It is guaranteed
120 * to succeed the second time.
121 */
122 if (call_again) {
123
124 /*
125 * Normally, xfs_trans_commit releases all the locks.
126 * We call bhold to hang on to the ialloc_context across
127 * the commit. Holding this buffer prevents any other
128 * processes from doing any allocations in this
129 * allocation group.
130 */
131 xfs_trans_bhold(tp, ialloc_context);
132 /*
133 * Save the log reservation so we can use
134 * them in the next transaction.
135 */
136 log_res = xfs_trans_get_log_res(tp);
137 log_count = xfs_trans_get_log_count(tp);
138
139 /*
140 * We want the quota changes to be associated with the next
141 * transaction, NOT this one. So, detach the dqinfo from this
142 * and attach it to the next transaction.
143 */
144 dqinfo = NULL;
145 tflags = 0;
146 if (tp->t_dqinfo) {
147 dqinfo = (void *)tp->t_dqinfo;
148 tp->t_dqinfo = NULL;
149 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
150 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
151 }
152
153 ntp = xfs_trans_dup(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000154 code = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 tp = ntp;
156 if (committed != NULL) {
157 *committed = 1;
158 }
159 /*
160 * If we get an error during the commit processing,
161 * release the buffer that is still held and return
162 * to the caller.
163 */
164 if (code) {
165 xfs_buf_relse(ialloc_context);
166 if (dqinfo) {
167 tp->t_dqinfo = dqinfo;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200168 xfs_trans_free_dqinfo(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 *tpp = ntp;
171 *ipp = NULL;
172 return code;
173 }
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100174
175 /*
176 * transaction commit worked ok so we can drop the extra ticket
177 * reference that we gained in xfs_trans_dup()
178 */
179 xfs_log_ticket_put(tp->t_ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 code = xfs_trans_reserve(tp, 0, log_res, 0,
181 XFS_TRANS_PERM_LOG_RES, log_count);
182 /*
183 * Re-attach the quota info that we detached from prev trx.
184 */
185 if (dqinfo) {
186 tp->t_dqinfo = dqinfo;
187 tp->t_flags |= tflags;
188 }
189
190 if (code) {
191 xfs_buf_relse(ialloc_context);
192 *tpp = ntp;
193 *ipp = NULL;
194 return code;
195 }
196 xfs_trans_bjoin(tp, ialloc_context);
197
198 /*
199 * Call ialloc again. Since we've locked out all
200 * other allocations in this allocation group,
201 * this call should always succeed.
202 */
203 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid,
204 okalloc, &ialloc_context, &call_again, &ip);
205
206 /*
207 * If we get an error at this point, return to the caller
208 * so that the current transaction can be aborted.
209 */
210 if (code) {
211 *tpp = tp;
212 *ipp = NULL;
213 return code;
214 }
215 ASSERT ((!call_again) && (ip != NULL));
216
217 } else {
218 if (committed != NULL) {
219 *committed = 0;
220 }
221 }
222
223 *ipp = ip;
224 *tpp = tp;
225
226 return 0;
227}
228
229/*
230 * Decrement the link count on an inode & log the change.
231 * If this causes the link count to go to zero, initiate the
232 * logging activity required to truncate a file.
233 */
234int /* error */
235xfs_droplink(
236 xfs_trans_t *tp,
237 xfs_inode_t *ip)
238{
239 int error;
240
241 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
242
243 ASSERT (ip->i_d.di_nlink > 0);
244 ip->i_d.di_nlink--;
David Chinner01651642008-08-13 15:45:15 +1000245 drop_nlink(VFS_I(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
247
248 error = 0;
249 if (ip->i_d.di_nlink == 0) {
250 /*
251 * We're dropping the last link to this file.
252 * Move the on-disk inode to the AGI unlinked list.
253 * From xfs_inactive() we will pull the inode from
254 * the list and free it.
255 */
256 error = xfs_iunlink(tp, ip);
257 }
258 return error;
259}
260
261/*
262 * This gets called when the inode's version needs to be changed from 1 to 2.
263 * Currently this happens when the nlink field overflows the old 16-bit value
264 * or when chproj is called to change the project for the first time.
265 * As a side effect the superblock version will also get rev'd
266 * to contain the NLINK bit.
267 */
268void
269xfs_bump_ino_vers2(
270 xfs_trans_t *tp,
271 xfs_inode_t *ip)
272{
273 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000275 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100276 ASSERT(ip->i_d.di_version == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100278 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ip->i_d.di_onlink = 0;
280 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
281 mp = tp->t_mountp;
Eric Sandeen62118702008-03-06 13:44:28 +1100282 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000283 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100284 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
285 xfs_sb_version_addnlink(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000286 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
288 } else {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000289 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 }
292 /* Caller must log the inode */
293}
294
295/*
296 * Increment the link count on an inode & log the change.
297 */
298int
299xfs_bumplink(
300 xfs_trans_t *tp,
301 xfs_inode_t *ip)
302{
303 if (ip->i_d.di_nlink >= XFS_MAXLINK)
304 return XFS_ERROR(EMLINK);
305 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
306
307 ASSERT(ip->i_d.di_nlink > 0);
308 ip->i_d.di_nlink++;
David Chinner01651642008-08-13 15:45:15 +1000309 inc_nlink(VFS_I(ip));
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100310 if ((ip->i_d.di_version == 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
312 /*
313 * The inode has increased its number of links beyond
314 * what can fit in an old format inode. It now needs
315 * to be converted to a version 2 inode with a 32 bit
316 * link count. If this is the first inode in the file
317 * system to do this, then we need to bump the superblock
318 * version number as well.
319 */
320 xfs_bump_ino_vers2(tp, ip);
321 }
322
323 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
324 return 0;
325}
326
327/*
328 * Try to truncate the given file to 0 length. Currently called
329 * only out of xfs_remove when it has to truncate a file to free
330 * up space for the remove to proceed.
331 */
332int
333xfs_truncate_file(
334 xfs_mount_t *mp,
335 xfs_inode_t *ip)
336{
337 xfs_trans_t *tp;
338 int error;
339
340#ifdef QUOTADEBUG
341 /*
342 * This is called to truncate the quotainodes too.
343 */
344 if (XFS_IS_UQUOTA_ON(mp)) {
345 if (ip->i_ino != mp->m_sb.sb_uquotino)
346 ASSERT(ip->i_udquot);
347 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000348 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (ip->i_ino != mp->m_sb.sb_gquotino)
350 ASSERT(ip->i_gdquot);
351 }
352#endif
353 /*
354 * Make the call to xfs_itruncate_start before starting the
355 * transaction, because we cannot make the call while we're
356 * in a transaction.
357 */
358 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000359 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0);
360 if (error) {
361 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
362 return error;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
366 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
367 XFS_TRANS_PERM_LOG_RES,
368 XFS_ITRUNCATE_LOG_COUNT))) {
369 xfs_trans_cancel(tp, 0);
370 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
371 return error;
372 }
373
374 /*
375 * Follow the normal truncate locking protocol. Since we
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200376 * hold the inode in the transaction, we know that its number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * of references will stay constant.
378 */
379 xfs_ilock(ip, XFS_ILOCK_EXCL);
380 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
381 xfs_trans_ihold(tp, ip);
382 /*
383 * Signal a sync xaction. The only case where that isn't
384 * the case is if we're truncating an already unlinked file
385 * on a wsync fs. In that case, we know the blocks can't
386 * reappear in the file because the links to file are
387 * permanently toast. Currently, we're always going to
388 * want a sync transaction because this code is being
389 * called from places where nlink is guaranteed to be 1
390 * but I'm leaving the tests in to protect against future
391 * changes -- rcc.
392 */
393 error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0,
394 XFS_DATA_FORK,
395 ((ip->i_d.di_nlink != 0 ||
396 !(mp->m_flags & XFS_MOUNT_WSYNC))
397 ? 1 : 0));
398 if (error) {
399 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
400 XFS_TRANS_ABORT);
401 } else {
402 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000403 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
406
407 return error;
408}