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