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