blob: fde7d2231ee8b7e252665cf886694b78d13a35a8 [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"
Dave Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100026#include "xfs_da_btree.h"
27#include "xfs_dir2_format.h"
28#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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,
Christoph Hellwig08358902012-07-04 10:54:47 -040095 &ialloc_context, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
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 }
Christoph Hellwig08358902012-07-04 10:54:47 -0400106 if (!ialloc_context && !ip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *ipp = NULL;
108 return XFS_ERROR(ENOSPC);
109 }
110
111 /*
Christoph Hellwig08358902012-07-04 10:54:47 -0400112 * If the AGI buffer is non-NULL, then we were unable to get an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * 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 */
Christoph Hellwig08358902012-07-04 10:54:47 -0400117 if (ialloc_context) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /*
119 * Normally, xfs_trans_commit releases all the locks.
120 * We call bhold to hang on to the ialloc_context across
121 * the commit. Holding this buffer prevents any other
122 * processes from doing any allocations in this
123 * allocation group.
124 */
125 xfs_trans_bhold(tp, ialloc_context);
126 /*
127 * Save the log reservation so we can use
128 * them in the next transaction.
129 */
130 log_res = xfs_trans_get_log_res(tp);
131 log_count = xfs_trans_get_log_count(tp);
132
133 /*
134 * We want the quota changes to be associated with the next
135 * transaction, NOT this one. So, detach the dqinfo from this
136 * and attach it to the next transaction.
137 */
138 dqinfo = NULL;
139 tflags = 0;
140 if (tp->t_dqinfo) {
141 dqinfo = (void *)tp->t_dqinfo;
142 tp->t_dqinfo = NULL;
143 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
144 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
145 }
146
147 ntp = xfs_trans_dup(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000148 code = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 tp = ntp;
150 if (committed != NULL) {
151 *committed = 1;
152 }
153 /*
154 * If we get an error during the commit processing,
155 * release the buffer that is still held and return
156 * to the caller.
157 */
158 if (code) {
159 xfs_buf_relse(ialloc_context);
160 if (dqinfo) {
161 tp->t_dqinfo = dqinfo;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200162 xfs_trans_free_dqinfo(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 *tpp = ntp;
165 *ipp = NULL;
166 return code;
167 }
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100168
169 /*
170 * transaction commit worked ok so we can drop the extra ticket
171 * reference that we gained in xfs_trans_dup()
172 */
173 xfs_log_ticket_put(tp->t_ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 code = xfs_trans_reserve(tp, 0, log_res, 0,
175 XFS_TRANS_PERM_LOG_RES, log_count);
176 /*
177 * Re-attach the quota info that we detached from prev trx.
178 */
179 if (dqinfo) {
180 tp->t_dqinfo = dqinfo;
181 tp->t_flags |= tflags;
182 }
183
184 if (code) {
185 xfs_buf_relse(ialloc_context);
186 *tpp = ntp;
187 *ipp = NULL;
188 return code;
189 }
190 xfs_trans_bjoin(tp, ialloc_context);
191
192 /*
193 * Call ialloc again. Since we've locked out all
194 * other allocations in this allocation group,
195 * this call should always succeed.
196 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000197 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
Christoph Hellwig08358902012-07-04 10:54:47 -0400198 okalloc, &ialloc_context, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 /*
201 * If we get an error at this point, return to the caller
202 * so that the current transaction can be aborted.
203 */
204 if (code) {
205 *tpp = tp;
206 *ipp = NULL;
207 return code;
208 }
Christoph Hellwig08358902012-07-04 10:54:47 -0400209 ASSERT(!ialloc_context && ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 } else {
Christoph Hellwig08358902012-07-04 10:54:47 -0400212 if (committed != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *committed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 *ipp = ip;
217 *tpp = tp;
218
219 return 0;
220}
221
222/*
223 * Decrement the link count on an inode & log the change.
224 * If this causes the link count to go to zero, initiate the
225 * logging activity required to truncate a file.
226 */
227int /* error */
228xfs_droplink(
229 xfs_trans_t *tp,
230 xfs_inode_t *ip)
231{
232 int error;
233
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000234 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 ASSERT (ip->i_d.di_nlink > 0);
237 ip->i_d.di_nlink--;
David Chinner01651642008-08-13 15:45:15 +1000238 drop_nlink(VFS_I(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
240
241 error = 0;
242 if (ip->i_d.di_nlink == 0) {
243 /*
244 * We're dropping the last link to this file.
245 * Move the on-disk inode to the AGI unlinked list.
246 * From xfs_inactive() we will pull the inode from
247 * the list and free it.
248 */
249 error = xfs_iunlink(tp, ip);
250 }
251 return error;
252}
253
254/*
255 * This gets called when the inode's version needs to be changed from 1 to 2.
256 * Currently this happens when the nlink field overflows the old 16-bit value
257 * or when chproj is called to change the project for the first time.
258 * As a side effect the superblock version will also get rev'd
259 * to contain the NLINK bit.
260 */
261void
262xfs_bump_ino_vers2(
263 xfs_trans_t *tp,
264 xfs_inode_t *ip)
265{
266 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000268 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100269 ASSERT(ip->i_d.di_version == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100271 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 ip->i_d.di_onlink = 0;
273 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
274 mp = tp->t_mountp;
Eric Sandeen62118702008-03-06 13:44:28 +1100275 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000276 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100277 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
278 xfs_sb_version_addnlink(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000279 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
281 } else {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000282 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 }
285 /* Caller must log the inode */
286}
287
288/*
289 * Increment the link count on an inode & log the change.
290 */
291int
292xfs_bumplink(
293 xfs_trans_t *tp,
294 xfs_inode_t *ip)
295{
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000296 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 ASSERT(ip->i_d.di_nlink > 0);
299 ip->i_d.di_nlink++;
David Chinner01651642008-08-13 15:45:15 +1000300 inc_nlink(VFS_I(ip));
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100301 if ((ip->i_d.di_version == 1) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
303 /*
304 * The inode has increased its number of links beyond
305 * what can fit in an old format inode. It now needs
306 * to be converted to a version 2 inode with a 32 bit
307 * link count. If this is the first inode in the file
308 * system to do this, then we need to bump the superblock
309 * version number as well.
310 */
311 xfs_bump_ino_vers2(tp, ip);
312 }
313
314 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
315 return 0;
316}