blob: 79c05ac85bfec663d7c2b1d34cad77d65b06f320 [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_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_bmap.h"
34#include "xfs_error.h"
35#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_itable.h"
37#include "xfs_utils.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * Allocates a new inode from disk and return a pointer to the
42 * incore copy. This routine will internally commit the current
43 * transaction and allocate a new one if the Space Manager needed
44 * to do an allocation to replenish the inode free-list.
45 *
46 * This routine is designed to be called from xfs_create and
47 * xfs_create_dir.
48 *
49 */
50int
51xfs_dir_ialloc(
52 xfs_trans_t **tpp, /* input: current transaction;
53 output: may be a new transaction. */
54 xfs_inode_t *dp, /* directory within whose allocate
55 the inode. */
Al Viro576b1d62011-07-26 02:50:15 -040056 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -070057 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 xfs_dev_t rdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 prid_t prid, /* project id */
60 int okalloc, /* ok to allocate new space */
61 xfs_inode_t **ipp, /* pointer to inode; it will be
62 locked. */
63 int *committed)
64
65{
66 xfs_trans_t *tp;
67 xfs_trans_t *ntp;
68 xfs_inode_t *ip;
69 xfs_buf_t *ialloc_context = NULL;
70 boolean_t call_again = B_FALSE;
71 int code;
72 uint log_res;
73 uint log_count;
74 void *dqinfo;
75 uint tflags;
76
77 tp = *tpp;
78 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
79
80 /*
81 * xfs_ialloc will return a pointer to an incore inode if
82 * the Space Manager has an available inode on the free
83 * list. Otherwise, it will do an allocation and replenish
84 * the freelist. Since we can only do one allocation per
85 * transaction without deadlocks, we will need to commit the
86 * current transaction and start a new one. We will then
87 * need to call xfs_ialloc again to get the inode.
88 *
89 * If xfs_ialloc did an allocation to replenish the freelist,
90 * it returns the bp containing the head of the freelist as
91 * ialloc_context. We will hold a lock on it across the
92 * transaction commit so that no other process can steal
93 * the inode(s) that we've just allocated.
94 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +000095 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 &ialloc_context, &call_again, &ip);
97
98 /*
99 * Return an error if we were unable to allocate a new inode.
100 * This should only happen if we run out of space on disk or
101 * encounter a disk error.
102 */
103 if (code) {
104 *ipp = NULL;
105 return code;
106 }
107 if (!call_again && (ip == NULL)) {
108 *ipp = NULL;
109 return XFS_ERROR(ENOSPC);
110 }
111
112 /*
113 * If call_again is set, then we were unable to get an
114 * inode in one operation. We need to commit the current
115 * transaction and call xfs_ialloc() again. It is guaranteed
116 * to succeed the second time.
117 */
118 if (call_again) {
119
120 /*
121 * Normally, xfs_trans_commit releases all the locks.
122 * We call bhold to hang on to the ialloc_context across
123 * the commit. Holding this buffer prevents any other
124 * processes from doing any allocations in this
125 * allocation group.
126 */
127 xfs_trans_bhold(tp, ialloc_context);
128 /*
129 * Save the log reservation so we can use
130 * them in the next transaction.
131 */
132 log_res = xfs_trans_get_log_res(tp);
133 log_count = xfs_trans_get_log_count(tp);
134
135 /*
136 * We want the quota changes to be associated with the next
137 * transaction, NOT this one. So, detach the dqinfo from this
138 * and attach it to the next transaction.
139 */
140 dqinfo = NULL;
141 tflags = 0;
142 if (tp->t_dqinfo) {
143 dqinfo = (void *)tp->t_dqinfo;
144 tp->t_dqinfo = NULL;
145 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
146 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
147 }
148
149 ntp = xfs_trans_dup(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000150 code = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 tp = ntp;
152 if (committed != NULL) {
153 *committed = 1;
154 }
155 /*
156 * If we get an error during the commit processing,
157 * release the buffer that is still held and return
158 * to the caller.
159 */
160 if (code) {
161 xfs_buf_relse(ialloc_context);
162 if (dqinfo) {
163 tp->t_dqinfo = dqinfo;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200164 xfs_trans_free_dqinfo(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 *tpp = ntp;
167 *ipp = NULL;
168 return code;
169 }
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100170
171 /*
172 * transaction commit worked ok so we can drop the extra ticket
173 * reference that we gained in xfs_trans_dup()
174 */
175 xfs_log_ticket_put(tp->t_ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 code = xfs_trans_reserve(tp, 0, log_res, 0,
177 XFS_TRANS_PERM_LOG_RES, log_count);
178 /*
179 * Re-attach the quota info that we detached from prev trx.
180 */
181 if (dqinfo) {
182 tp->t_dqinfo = dqinfo;
183 tp->t_flags |= tflags;
184 }
185
186 if (code) {
187 xfs_buf_relse(ialloc_context);
188 *tpp = ntp;
189 *ipp = NULL;
190 return code;
191 }
192 xfs_trans_bjoin(tp, ialloc_context);
193
194 /*
195 * Call ialloc again. Since we've locked out all
196 * other allocations in this allocation group,
197 * this call should always succeed.
198 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000199 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 okalloc, &ialloc_context, &call_again, &ip);
201
202 /*
203 * If we get an error at this point, return to the caller
204 * so that the current transaction can be aborted.
205 */
206 if (code) {
207 *tpp = tp;
208 *ipp = NULL;
209 return code;
210 }
211 ASSERT ((!call_again) && (ip != NULL));
212
213 } else {
214 if (committed != NULL) {
215 *committed = 0;
216 }
217 }
218
219 *ipp = ip;
220 *tpp = tp;
221
222 return 0;
223}
224
225/*
226 * Decrement the link count on an inode & log the change.
227 * If this causes the link count to go to zero, initiate the
228 * logging activity required to truncate a file.
229 */
230int /* error */
231xfs_droplink(
232 xfs_trans_t *tp,
233 xfs_inode_t *ip)
234{
235 int error;
236
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000237 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 ASSERT (ip->i_d.di_nlink > 0);
240 ip->i_d.di_nlink--;
David Chinner01651642008-08-13 15:45:15 +1000241 drop_nlink(VFS_I(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
243
244 error = 0;
245 if (ip->i_d.di_nlink == 0) {
246 /*
247 * We're dropping the last link to this file.
248 * Move the on-disk inode to the AGI unlinked list.
249 * From xfs_inactive() we will pull the inode from
250 * the list and free it.
251 */
252 error = xfs_iunlink(tp, ip);
253 }
254 return error;
255}
256
257/*
258 * This gets called when the inode's version needs to be changed from 1 to 2.
259 * Currently this happens when the nlink field overflows the old 16-bit value
260 * or when chproj is called to change the project for the first time.
261 * As a side effect the superblock version will also get rev'd
262 * to contain the NLINK bit.
263 */
264void
265xfs_bump_ino_vers2(
266 xfs_trans_t *tp,
267 xfs_inode_t *ip)
268{
269 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000271 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100272 ASSERT(ip->i_d.di_version == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100274 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ip->i_d.di_onlink = 0;
276 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
277 mp = tp->t_mountp;
Eric Sandeen62118702008-03-06 13:44:28 +1100278 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000279 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100280 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
281 xfs_sb_version_addnlink(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000282 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
284 } else {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000285 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 }
288 /* Caller must log the inode */
289}
290
291/*
292 * Increment the link count on an inode & log the change.
293 */
294int
295xfs_bumplink(
296 xfs_trans_t *tp,
297 xfs_inode_t *ip)
298{
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000299 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 ASSERT(ip->i_d.di_nlink > 0);
302 ip->i_d.di_nlink++;
David Chinner01651642008-08-13 15:45:15 +1000303 inc_nlink(VFS_I(ip));
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100304 if ((ip->i_d.di_version == 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
306 /*
307 * The inode has increased its number of links beyond
308 * what can fit in an old format inode. It now needs
309 * to be converted to a version 2 inode with a 32 bit
310 * link count. If this is the first inode in the file
311 * system to do this, then we need to bump the superblock
312 * version number as well.
313 */
314 xfs_bump_ino_vers2(tp, ip);
315 }
316
317 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
318 return 0;
319}