blob: 59509ae0b2734be2fce52994d1ea75a5afce7013 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * 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 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
32#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_dinode.h"
34#include "xfs_inode.h"
35#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_ialloc.h"
38#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_bmap.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020040#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_attr.h"
42#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_quota.h"
45#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110046#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100049#include "xfs_filestream.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100050#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000051#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Christoph Hellwig993386c2007-08-28 16:12:30 +100053int
Linus Torvalds1da177e2005-04-16 15:20:36 -070054xfs_setattr(
Christoph Hellwig0f285c82008-07-18 17:13:28 +100055 struct xfs_inode *ip,
56 struct iattr *iattr,
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +110057 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Christoph Hellwig993386c2007-08-28 16:12:30 +100059 xfs_mount_t *mp = ip->i_mount;
David Chinnere4f75292008-08-13 16:00:45 +100060 struct inode *inode = VFS_I(ip);
Christoph Hellwig0f285c82008-07-18 17:13:28 +100061 int mask = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int code;
64 uint lock_flags;
65 uint commit_flags=0;
66 uid_t uid=0, iuid=0;
67 gid_t gid=0, igid=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
Dean Roehrich5fcbab32005-05-05 13:27:19 -070069 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Christoph Hellwigcca28fb2010-06-24 11:57:09 +100071 trace_xfs_setattr(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Christoph Hellwigbd186aa2007-08-30 17:21:12 +100073 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return XFS_ERROR(EROFS);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (XFS_FORCED_SHUTDOWN(mp))
77 return XFS_ERROR(EIO);
78
Christoph Hellwigc4cd7472008-12-09 04:47:34 -050079 code = -inode_change_ok(inode, iattr);
80 if (code)
81 return code;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 olddquot1 = olddquot2 = NULL;
84 udqp = gdqp = NULL;
85
86 /*
87 * If disk quotas is on, we make sure that the dquots do exist on disk,
88 * before we start any other transactions. Trying to do this later
89 * is messy. We don't care to take a readlock to look at the ids
90 * in inode here, because we can't hold it across the trans_reserve.
91 * If the IDs do change before we take the ilock, we're covered
92 * because the i_*dquot fields will get updated anyway.
93 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +100094 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 uint qflags = 0;
96
Christoph Hellwig0f285c82008-07-18 17:13:28 +100097 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
98 uid = iattr->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 qflags |= XFS_QMOPT_UQUOTA;
100 } else {
101 uid = ip->i_d.di_uid;
102 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000103 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
104 gid = iattr->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 qflags |= XFS_QMOPT_GQUOTA;
106 } else {
107 gid = ip->i_d.di_gid;
108 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /*
111 * We take a reference when we initialize udqp and gdqp,
112 * so it is important that we never blindly double trip on
113 * the same variable. See xfs_create() for an example.
114 */
115 ASSERT(udqp == NULL);
116 ASSERT(gdqp == NULL);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000117 code = xfs_qm_vop_dqalloc(ip, uid, gid, xfs_get_projid(ip),
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000118 qflags, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100120 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 /*
124 * For the other attributes, we acquire the inode lock and
125 * first do an error checking pass.
126 */
127 tp = NULL;
128 lock_flags = XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000129 if (flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700130 need_iolock = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000131 if (!(mask & ATTR_SIZE)) {
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000132 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
133 commit_flags = 0;
134 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp),
135 0, 0, 0);
136 if (code) {
137 lock_flags = 0;
138 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (need_iolock)
142 lock_flags |= XFS_IOLOCK_EXCL;
143 }
144
145 xfs_ilock(ip, lock_flags);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /*
148 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000150 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /*
152 * These IDs could have changed since we last looked at them.
153 * But, we're assured that if the ownership did change
154 * while we didn't have the inode locked, inode's dquot(s)
155 * would have changed also.
156 */
157 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000159 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
160 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000163 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * going to change.
165 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200166 if (XFS_IS_QUOTA_RUNNING(mp) &&
167 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
168 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200170 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 capable(CAP_FOWNER) ?
172 XFS_QMOPT_FORCE_RES : 0);
173 if (code) /* out of quota */
174 goto error_return;
175 }
176 }
177
178 /*
179 * Truncate file. Must have write permission and not be a directory.
180 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000181 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000183 if (iattr->ia_size == 0 &&
184 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 xfs_iunlock(ip, XFS_ILOCK_EXCL);
186 lock_flags &= ~XFS_ILOCK_EXCL;
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000187 if (mask & ATTR_CTIME) {
188 inode->i_mtime = inode->i_ctime =
189 current_fs_time(inode->i_sb);
190 xfs_mark_inode_dirty_sync(ip);
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 code = 0;
193 goto error_return;
194 }
195
Christoph Hellwig42173f62008-04-22 17:33:25 +1000196 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 code = XFS_ERROR(EISDIR);
198 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000199 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 code = XFS_ERROR(EINVAL);
201 goto error_return;
202 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /*
205 * Make sure that the dquots are attached to the inode.
206 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200207 code = xfs_qm_dqattach_locked(ip, 0);
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500208 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500211 /*
212 * Now we can make the changes. Before we join the inode
213 * to the transaction, if ATTR_SIZE is set then take care of
214 * the part of the truncation that must be done without the
215 * inode lock. This needs to be done before joining the inode
216 * to the transaction, because the inode cannot be unlocked
217 * once it is a part of the transaction.
218 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000219 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000220 /*
221 * Do the first part of growing a file: zero any data
222 * in the last block that is beyond the old EOF. We
223 * need to do this before the inode is joined to the
224 * transaction to modify the i_size.
225 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000226 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -0400227 if (code)
228 goto error_return;
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -0400231 lock_flags &= ~XFS_ILOCK_EXCL;
David Chinnerc32676e2007-07-19 16:28:58 +1000232
233 /*
234 * We are going to log the inode size change in this
235 * transaction so any previous writes that are beyond the on
236 * disk EOF and the new EOF that have not been written out need
237 * to be written here. If we do not write the data out, we
238 * expose ourselves to the null files problem.
239 *
240 * Only flush from the on disk size to the smaller of the in
241 * memory file size or the new size as that's the range we
242 * really care about here and prevents waiting for other data
243 * not within the range we care about here.
244 */
Christoph Hellwigfa9b2272010-06-14 05:17:31 -0400245 if (ip->i_size != ip->i_d.di_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000246 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000247 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000248 ip->i_d.di_size, iattr->ia_size,
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000249 XBF_ASYNC, FI_NONE);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -0400250 if (code)
251 goto error_return;
David Chinnerc32676e2007-07-19 16:28:58 +1000252 }
253
254 /* wait for all I/O to complete */
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100255 xfs_ioend_wait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000256
Christoph Hellwigfa9b2272010-06-14 05:17:31 -0400257 code = -block_truncate_page(inode->i_mapping, iattr->ia_size,
258 xfs_get_blocks);
259 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto error_return;
Christoph Hellwigfa9b2272010-06-14 05:17:31 -0400261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -0400263 code = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
264 XFS_TRANS_PERM_LOG_RES,
265 XFS_ITRUNCATE_LOG_COUNT);
266 if (code)
267 goto error_return;
268
269 truncate_setsize(inode, iattr->ia_size);
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwigfa9b2272010-06-14 05:17:31 -0400272 lock_flags |= XFS_ILOCK_EXCL;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Christoph Hellwig898621d2010-06-24 11:36:58 +1000276 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David Chinner44d814c2008-03-06 13:45:29 +1100278 /*
279 * Only change the c/mtime if we are changing the size
280 * or we are explicitly asked to change it. This handles
281 * the semantic difference between truncate() and ftruncate()
282 * as implemented in the VFS.
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000283 *
284 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME
285 * is a special case where we need to update the times despite
286 * not having these flags set. For all other operations the
287 * VFS set these flags explicitly if it wants a timestamp
288 * update.
David Chinner44d814c2008-03-06 13:45:29 +1100289 */
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000290 if (iattr->ia_size != ip->i_size &&
291 (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
292 iattr->ia_ctime = iattr->ia_mtime =
293 current_fs_time(inode->i_sb);
294 mask |= ATTR_CTIME | ATTR_MTIME;
295 }
David Chinner44d814c2008-03-06 13:45:29 +1100296
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000297 if (iattr->ia_size > ip->i_size) {
298 ip->i_d.di_size = iattr->ia_size;
299 ip->i_size = iattr->ia_size;
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000300 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000301 } else if (iattr->ia_size <= ip->i_size ||
302 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /*
304 * signal a sync transaction unless
305 * we're truncating an already unlinked
306 * file on a wsync filesystem
307 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000308 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 XFS_DATA_FORK,
310 ((ip->i_d.di_nlink != 0 ||
311 !(mp->m_flags & XFS_MOUNT_WSYNC))
312 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000313 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000315 /*
316 * Truncated "down", so we're removing references
317 * to old data here - if we now delay flushing for
318 * a long time, we expose ourselves unduly to the
319 * notorious NULL files problem. So, we mark this
320 * vnode and flush it when the file is closed, and
321 * do not wait the usual (long) time for writeout.
322 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000323 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500325 } else if (tp) {
Christoph Hellwig898621d2010-06-24 11:36:58 +1000326 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
329 /*
330 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000332 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /*
334 * CAP_FSETID overrides the following restrictions:
335 *
336 * The set-user-ID and set-group-ID bits of a file will be
337 * cleared upon successful return from chown()
338 */
339 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
340 !capable(CAP_FSETID)) {
341 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
342 }
343
344 /*
345 * Change the ownerships and register quota modifications
346 * in the transaction.
347 */
348 if (iuid != uid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200349 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000350 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ASSERT(udqp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200352 olddquot1 = xfs_qm_vop_chown(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 &ip->i_udquot, udqp);
354 }
355 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000356 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 if (igid != gid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200359 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000360 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000361 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 ASSERT(gdqp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200363 olddquot2 = xfs_qm_vop_chown(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 &ip->i_gdquot, gdqp);
365 }
366 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000367 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500371 /*
372 * Change file access modes.
373 */
374 if (mask & ATTR_MODE) {
375 umode_t mode = iattr->ia_mode;
376
377 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
378 mode &= ~S_ISGID;
379
380 ip->i_d.di_mode &= S_IFMT;
381 ip->i_d.di_mode |= mode & ~S_IFMT;
382
383 inode->i_mode &= S_IFMT;
384 inode->i_mode |= mode & ~S_IFMT;
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /*
388 * Change file access or modified times.
389 */
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000390 if (mask & ATTR_ATIME) {
391 inode->i_atime = iattr->ia_atime;
392 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
393 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
394 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000396 if (mask & ATTR_CTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000397 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000398 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
399 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 ip->i_update_core = 1;
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000401 }
402 if (mask & ATTR_MTIME) {
403 inode->i_mtime = iattr->ia_mtime;
404 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
405 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
406 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409 /*
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000410 * And finally, log the inode core if any attribute in it
411 * has been changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000413 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE|
414 ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
415 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 XFS_STATS_INC(xs_ig_attrchg);
418
419 /*
420 * If this is a synchronous mount, make sure that the
421 * transaction goes to disk before returning to the user.
422 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000423 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * One for the truncate and one for the timestamps since we
425 * don't want to change the timestamps unless we're sure the
426 * truncate worked. Truncates are less than 1% of the laddis
427 * mix so this probably isn't worth the trouble to optimize.
428 */
429 code = 0;
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000430 if (mp->m_flags & XFS_MOUNT_WSYNC)
431 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000433 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 xfs_iunlock(ip, lock_flags);
436
437 /*
438 * Release any dquot(s) the inode had kept before chown.
439 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200440 xfs_qm_dqrele(olddquot1);
441 xfs_qm_dqrele(olddquot2);
442 xfs_qm_dqrele(udqp);
443 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200445 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return code;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200447
448 /*
449 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
450 * update. We could avoid this with linked transactions
451 * and passing down the transaction pointer all the way
452 * to attr_set. No previous user of the generic
453 * Posix ACL code seems to care about this issue either.
454 */
455 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
456 code = -xfs_acl_chmod(inode);
457 if (code)
458 return XFS_ERROR(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462
463 abort_return:
464 commit_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +0200466 xfs_qm_dqrele(udqp);
467 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (tp) {
469 xfs_trans_cancel(tp, commit_flags);
470 }
471 if (lock_flags != 0) {
472 xfs_iunlock(ip, lock_flags);
473 }
474 return code;
475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000478 * The maximum pathlen is 1024 bytes. Since the minimum file system
479 * blocksize is 512 bytes, we can get a max of 2 extents back from
480 * bmapi.
481 */
482#define SYMLINK_MAPS 2
483
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000484STATIC int
485xfs_readlink_bmap(
486 xfs_inode_t *ip,
487 char *link)
488{
489 xfs_mount_t *mp = ip->i_mount;
490 int pathlen = ip->i_d.di_size;
491 int nmaps = SYMLINK_MAPS;
492 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
493 xfs_daddr_t d;
494 int byte_cnt;
495 int n;
496 xfs_buf_t *bp;
497 int error = 0;
498
499 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000500 mval, &nmaps, NULL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000501 if (error)
502 goto out;
503
504 for (n = 0; n < nmaps; n++) {
505 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
506 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
507
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000508 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
509 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000510 error = XFS_BUF_GETERROR(bp);
511 if (error) {
512 xfs_ioerror_alert("xfs_readlink",
513 ip->i_mount, bp, XFS_BUF_ADDR(bp));
514 xfs_buf_relse(bp);
515 goto out;
516 }
517 if (pathlen < byte_cnt)
518 byte_cnt = pathlen;
519 pathlen -= byte_cnt;
520
521 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
522 xfs_buf_relse(bp);
523 }
524
525 link[ip->i_d.di_size] = '\0';
526 error = 0;
527
528 out:
529 return error;
530}
531
Christoph Hellwig993386c2007-08-28 16:12:30 +1000532int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000534 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000535 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000537 xfs_mount_t *mp = ip->i_mount;
Carlos Maiolino626ff2d2011-11-19 13:13:44 -0500538 xfs_fsize_t pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000541 trace_xfs_readlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (XFS_FORCED_SHUTDOWN(mp))
544 return XFS_ERROR(EIO);
545
546 xfs_ilock(ip, XFS_ILOCK_SHARED);
547
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000548 pathlen = ip->i_d.di_size;
549 if (!pathlen)
550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Carlos Maiolino626ff2d2011-11-19 13:13:44 -0500552 if (pathlen < 0 || pathlen > MAXPATHLEN) {
553 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
554 __func__, (unsigned long long) ip->i_ino,
555 (long long) pathlen);
556 ASSERT(0);
Jan Kara2fbe11f2012-01-11 18:52:10 +0000557 error = XFS_ERROR(EFSCORRUPTED);
558 goto out;
Carlos Maiolino626ff2d2011-11-19 13:13:44 -0500559 }
560
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000563 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
564 link[pathlen] = '\0';
565 } else {
566 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000569 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return error;
572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/*
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000575 * Flags for xfs_free_eofblocks
576 */
577#define XFS_FREE_EOF_TRYLOCK (1<<0)
578
579/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000580 * This is called by xfs_inactive to free any blocks beyond eof
581 * when the link count isn't zero and by xfs_dm_punch_hole() when
582 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500584STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000585xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000587 xfs_inode_t *ip,
588 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 xfs_trans_t *tp;
591 int error;
592 xfs_fileoff_t end_fsb;
593 xfs_fileoff_t last_fsb;
594 xfs_filblks_t map_len;
595 int nimaps;
596 xfs_bmbt_irec_t imap;
597
598 /*
599 * Figure out if there are any blocks beyond the end
600 * of the file. If not, then there is nothing to do.
601 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000602 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000604 if (last_fsb <= end_fsb)
Jesper Juhl014c2542006-01-15 02:37:08 +0100605 return 0;
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000606 map_len = last_fsb - end_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 nimaps = 1;
609 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000610 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000611 NULL, 0, &imap, &nimaps, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 xfs_iunlock(ip, XFS_ILOCK_SHARED);
613
614 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100615 (imap.br_startblock != HOLESTARTBLOCK ||
616 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /*
618 * Attach the dquots to the inode up front.
619 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200620 error = xfs_qm_dqattach(ip, 0);
621 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100622 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /*
625 * There are blocks after the end of file.
626 * Free them up now by truncating the file to
627 * its current size.
628 */
629 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
630
631 /*
632 * Do the xfs_itruncate_start() call before
633 * reserving any log space because
634 * itruncate_start will call into the buffer
635 * cache and we can't
636 * do that within a transaction.
637 */
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000638 if (flags & XFS_FREE_EOF_TRYLOCK) {
639 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
640 xfs_trans_cancel(tp, 0);
641 return 0;
642 }
643 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000644 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000645 }
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000646 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000647 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000648 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000649 xfs_trans_cancel(tp, 0);
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000650 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000651 return error;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 error = xfs_trans_reserve(tp, 0,
655 XFS_ITRUNCATE_LOG_RES(mp),
656 0, XFS_TRANS_PERM_LOG_RES,
657 XFS_ITRUNCATE_LOG_COUNT);
658 if (error) {
659 ASSERT(XFS_FORCED_SHUTDOWN(mp));
660 xfs_trans_cancel(tp, 0);
661 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100662 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000666 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000669 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 XFS_DATA_FORK,
671 0);
672 /*
673 * If we get an error at this point we
674 * simply don't bother truncating the file.
675 */
676 if (error) {
677 xfs_trans_cancel(tp,
678 (XFS_TRANS_RELEASE_LOG_RES |
679 XFS_TRANS_ABORT));
680 } else {
681 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000682 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000684 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100686 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/*
690 * Free a symlink that has blocks associated with it.
691 */
692STATIC int
693xfs_inactive_symlink_rmt(
694 xfs_inode_t *ip,
695 xfs_trans_t **tpp)
696{
697 xfs_buf_t *bp;
698 int committed;
699 int done;
700 int error;
701 xfs_fsblock_t first_block;
702 xfs_bmap_free_t free_list;
703 int i;
704 xfs_mount_t *mp;
705 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
706 int nmaps;
707 xfs_trans_t *ntp;
708 int size;
709 xfs_trans_t *tp;
710
711 tp = *tpp;
712 mp = ip->i_mount;
713 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
714 /*
715 * We're freeing a symlink that has some
716 * blocks allocated to it. Free the
717 * blocks here. We know that we've got
718 * either 1 or 2 extents and that we can
719 * free them all in one bunmapi call.
720 */
721 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
722 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
723 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
724 ASSERT(XFS_FORCED_SHUTDOWN(mp));
725 xfs_trans_cancel(tp, 0);
726 *tpp = NULL;
727 return error;
728 }
729 /*
730 * Lock the inode, fix the size, and join it to the transaction.
731 * Hold it so in the normal path, we still have it locked for
732 * the second transaction. In the error paths we need it
733 * held so the cancel won't rele it, see below.
734 */
735 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
736 size = (int)ip->i_d.di_size;
737 ip->i_d.di_size = 0;
Christoph Hellwig898621d2010-06-24 11:36:58 +1000738 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
740 /*
741 * Find the block(s) so we can inval and unmap them.
742 */
743 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600744 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800745 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
747 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000748 &free_list)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto error0;
750 /*
751 * Invalidate the block(s).
752 */
753 for (i = 0; i < nmaps; i++) {
754 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
755 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
756 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
757 xfs_trans_binval(tp, bp);
758 }
759 /*
760 * Unmap the dead block(s) to the free_list.
761 */
762 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000763 &first_block, &free_list, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 goto error1;
765 ASSERT(done);
766 /*
767 * Commit the first transaction. This logs the EFI and the inode.
768 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100769 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto error1;
771 /*
772 * The transaction must have been committed, since there were
773 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
774 * The new tp has the extent freeing and EFDs.
775 */
776 ASSERT(committed);
777 /*
778 * The first xact was committed, so add the inode to the new one.
779 * Mark it dirty so it will be logged and moved forward in the log as
780 * part of every commit.
781 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000782 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
784 /*
785 * Get a new, empty transaction to return to our caller.
786 */
787 ntp = xfs_trans_dup(tp);
788 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000789 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * If we get an error on the commit here or on the reserve below,
791 * we need to unlock the inode since the new transaction doesn't
792 * have the inode attached.
793 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000794 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 tp = ntp;
796 if (error) {
797 ASSERT(XFS_FORCED_SHUTDOWN(mp));
798 goto error0;
799 }
800 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100801 * transaction commit worked ok so we can drop the extra ticket
802 * reference that we gained in xfs_trans_dup()
803 */
804 xfs_log_ticket_put(tp->t_ticket);
805
806 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * Remove the memory for extent descriptions (just bookkeeping).
808 */
809 if (ip->i_df.if_bytes)
810 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
811 ASSERT(ip->i_df.if_bytes == 0);
812 /*
813 * Put an itruncate log reservation in the new transaction
814 * for our caller.
815 */
816 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
817 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
818 ASSERT(XFS_FORCED_SHUTDOWN(mp));
819 goto error0;
820 }
821 /*
822 * Return with the inode locked but not joined to the transaction.
823 */
824 *tpp = tp;
825 return 0;
826
827 error1:
828 xfs_bmap_cancel(&free_list);
829 error0:
830 /*
831 * Have to come here with the inode locked and either
832 * (held and in the transaction) or (not in the transaction).
833 * If the inode isn't held then cancel would iput it, but
834 * that's wrong since this is inactive and the vnode ref
835 * count is 0 already.
836 * Cancel won't do anything to the inode if held, but it still
837 * needs to be locked until the cancel is done, if it was
838 * joined to the transaction.
839 */
840 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
841 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
842 *tpp = NULL;
843 return error;
844
845}
846
847STATIC int
848xfs_inactive_symlink_local(
849 xfs_inode_t *ip,
850 xfs_trans_t **tpp)
851{
852 int error;
853
854 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
855 /*
856 * We're freeing a symlink which fit into
857 * the inode. Just free the memory used
858 * to hold the old symlink.
859 */
860 error = xfs_trans_reserve(*tpp, 0,
861 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
862 0, XFS_TRANS_PERM_LOG_RES,
863 XFS_ITRUNCATE_LOG_COUNT);
864
865 if (error) {
866 xfs_trans_cancel(*tpp, 0);
867 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100868 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
871
872 /*
873 * Zero length symlinks _can_ exist.
874 */
875 if (ip->i_df.if_bytes > 0) {
876 xfs_idata_realloc(ip,
877 -(ip->i_df.if_bytes),
878 XFS_DATA_FORK);
879 ASSERT(ip->i_df.if_bytes == 0);
880 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884STATIC int
885xfs_inactive_attrs(
886 xfs_inode_t *ip,
887 xfs_trans_t **tpp)
888{
889 xfs_trans_t *tp;
890 int error;
891 xfs_mount_t *mp;
892
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000893 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 tp = *tpp;
895 mp = ip->i_mount;
896 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +1000897 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +1000899 if (error)
900 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +1000903 if (error)
904 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
907 error = xfs_trans_reserve(tp, 0,
908 XFS_IFREE_LOG_RES(mp),
909 0, XFS_TRANS_PERM_LOG_RES,
910 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +1000911 if (error)
912 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000915 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
917
918 ASSERT(ip->i_d.di_anextents == 0);
919
920 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +0100921 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +1000922
923error_cancel:
924 ASSERT(XFS_FORCED_SHUTDOWN(mp));
925 xfs_trans_cancel(tp, 0);
926error_unlock:
927 *tpp = NULL;
928 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
929 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Christoph Hellwig993386c2007-08-28 16:12:30 +1000932int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000934 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000936 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int error;
938
Christoph Hellwig42173f62008-04-22 17:33:25 +1000939 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000943 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
945
David Chinner2a82b8b2007-07-11 11:09:12 +1000946 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000947 int truncated;
948
David Chinner2a82b8b2007-07-11 11:09:12 +1000949 /*
950 * If we are using filestreams, and we have an unlinked
951 * file that we are processing the last close on, then nothing
952 * will be able to reopen and write to this file. Purge this
953 * inode from the filestreams cache so that it doesn't delay
954 * teardown of the inode.
955 */
956 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
957 xfs_filestream_deassociate(ip);
958
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000959 /*
960 * If we previously truncated this file and removed old data
961 * in the process, we want to initiate "early" writeout on
962 * the last close. This is an attempt to combat the notorious
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300963 * NULL files problem which is particularly noticeable from a
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000964 * truncate down, buffered (re-)write (delalloc), followed by
965 * a crash. What we are effectively doing here is
966 * significantly reducing the time window where we'd otherwise
967 * be exposed to that problem.
968 */
Christoph Hellwig09262b42007-08-29 11:44:50 +1000969 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Dave Chinnerdf4368a2011-06-23 01:35:00 +0000970 if (truncated) {
971 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
972 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
973 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
974 }
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000975 }
976
Dave Chinner6e857562010-12-23 12:02:31 +1100977 if (ip->i_d.di_nlink == 0)
978 return 0;
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000979
Dave Chinner6e857562010-12-23 12:02:31 +1100980 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
981 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
982 ip->i_delayed_blks > 0)) &&
983 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
984 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
985
986 /*
987 * If we can't get the iolock just skip truncating the blocks
988 * past EOF because we could deadlock with the mmap_sem
989 * otherwise. We'll get another chance to drop them once the
990 * last reference to the inode is dropped, so we'll never leak
991 * blocks permanently.
992 *
993 * Further, check if the inode is being opened, written and
994 * closed frequently and we have delayed allocation blocks
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300995 * outstanding (e.g. streaming writes from the NFS server),
Dave Chinner6e857562010-12-23 12:02:31 +1100996 * truncating the blocks past EOF will cause fragmentation to
997 * occur.
998 *
999 * In this case don't do the truncation, either, but we have to
1000 * be careful how we detect this case. Blocks beyond EOF show
1001 * up as i_delayed_blks even when the inode is clean, so we
1002 * need to truncate them away first before checking for a dirty
1003 * release. Hence on the first dirty close we will still remove
1004 * the speculative allocation, but after that we will leave it
1005 * in place.
1006 */
1007 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1008 return 0;
1009
1010 error = xfs_free_eofblocks(mp, ip,
1011 XFS_FREE_EOF_TRYLOCK);
1012 if (error)
1013 return error;
1014
1015 /* delalloc blocks after truncation means it really is dirty */
1016 if (ip->i_delayed_blks)
1017 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
1021
1022/*
1023 * xfs_inactive
1024 *
1025 * This is called when the vnode reference count for the vnode
1026 * goes to zero. If the file has been unlinked, then it must
1027 * now be truncated. Also, we clear all of the read-ahead state
1028 * kept for the inode here since the file is now closed.
1029 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001030int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001032 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001034 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 xfs_fsblock_t first_block;
1036 int committed;
1037 xfs_trans_t *tp;
1038 xfs_mount_t *mp;
1039 int error;
1040 int truncate;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
1043 * If the inode is already free, then there can be nothing
1044 * to clean up here.
1045 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +01001046 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 ASSERT(ip->i_df.if_real_bytes == 0);
1048 ASSERT(ip->i_df.if_broot_bytes == 0);
1049 return VN_INACTIVE_CACHE;
1050 }
1051
1052 /*
1053 * Only do a truncate if it's a regular file with
1054 * some actual space in it. It's OK to look at the
1055 * inode's fields without the lock because we're the
1056 * only one with a reference to the inode.
1057 */
1058 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001059 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1060 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1062
1063 mp = ip->i_mount;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 error = 0;
1066
1067 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001068 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 goto out;
1070
1071 if (ip->i_d.di_nlink != 0) {
1072 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001073 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001074 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001075 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1076 (!(ip->i_d.di_flags &
1077 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1078 (ip->i_delayed_blks != 0)))) {
Christoph Hellwigc56c9632009-10-19 04:03:46 +00001079 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001080 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001081 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 goto out;
1084 }
1085
1086 ASSERT(ip->i_d.di_nlink == 0);
1087
Christoph Hellwig7d095252009-06-08 15:33:32 +02001088 error = xfs_qm_dqattach(ip, 0);
1089 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001090 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1093 if (truncate) {
1094 /*
1095 * Do the xfs_itruncate_start() call before
1096 * reserving any log space because itruncate_start
1097 * will call into the buffer cache and we can't
1098 * do that within a transaction.
1099 */
1100 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1101
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001102 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1103 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001104 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001105 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1106 return VN_INACTIVE_CACHE;
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 error = xfs_trans_reserve(tp, 0,
1110 XFS_ITRUNCATE_LOG_RES(mp),
1111 0, XFS_TRANS_PERM_LOG_RES,
1112 XFS_ITRUNCATE_LOG_COUNT);
1113 if (error) {
1114 /* Don't call itruncate_cleanup */
1115 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1116 xfs_trans_cancel(tp, 0);
1117 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001118 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
1121 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001122 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /*
1125 * normally, we have to run xfs_itruncate_finish sync.
1126 * But if filesystem is wsync and we're in the inactive
1127 * path, then we know that nlink == 0, and that the
1128 * xaction that made nlink == 0 is permanently committed
1129 * since xfs_remove runs as a synchronous transaction.
1130 */
1131 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1132 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1133
1134 if (error) {
1135 xfs_trans_cancel(tp,
1136 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1137 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001138 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1141
1142 /*
1143 * If we get an error while cleaning up a
1144 * symlink we bail out.
1145 */
1146 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1147 xfs_inactive_symlink_rmt(ip, &tp) :
1148 xfs_inactive_symlink_local(ip, &tp);
1149
1150 if (error) {
1151 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001152 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Christoph Hellwig898621d2010-06-24 11:36:58 +10001155 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 } else {
1157 error = xfs_trans_reserve(tp, 0,
1158 XFS_IFREE_LOG_RES(mp),
1159 0, XFS_TRANS_PERM_LOG_RES,
1160 XFS_INACTIVE_LOG_COUNT);
1161 if (error) {
1162 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1163 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001164 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
1167 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001168 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170
1171 /*
1172 * If there are attributes associated with the file
1173 * then blow them away now. The code calls a routine
1174 * that recursively deconstructs the attribute fork.
1175 * We need to just commit the current transaction
1176 * because we can't use it for xfs_attr_inactive().
1177 */
1178 if (ip->i_d.di_anextents > 0) {
1179 error = xfs_inactive_attrs(ip, &tp);
1180 /*
1181 * If we got an error, the transaction is already
1182 * cancelled, and the inode is unlocked. Just get out.
1183 */
1184 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001185 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 } else if (ip->i_afp) {
1187 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1188 }
1189
1190 /*
1191 * Free the inode.
1192 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001193 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 error = xfs_ifree(tp, ip, &free_list);
1195 if (error) {
1196 /*
1197 * If we fail to free the inode, shut down. The cancel
1198 * might do that, we need to make sure. Otherwise the
1199 * inode might be lost for a long time or forever.
1200 */
1201 if (!XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001202 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1203 __func__, error);
Nathan Scott7d04a332006-06-09 14:58:38 +10001204 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1207 } else {
1208 /*
1209 * Credit the quota account(s). The inode is gone.
1210 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001211 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /*
David Chinner78e9da72008-04-10 12:24:17 +10001214 * Just ignore errors at this point. There is nothing we can
1215 * do except to try to keep going. Make sure it's not a silent
1216 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
David Chinner78e9da72008-04-10 12:24:17 +10001218 error = xfs_bmap_finish(&tp, &free_list, &committed);
1219 if (error)
Dave Chinner53487782011-03-07 10:05:35 +11001220 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
1221 __func__, error);
David Chinner78e9da72008-04-10 12:24:17 +10001222 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1223 if (error)
Dave Chinner53487782011-03-07 10:05:35 +11001224 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1225 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Christoph Hellwig7d095252009-06-08 15:33:32 +02001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /*
1229 * Release the dquots held by inode, if any.
1230 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001231 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1233
1234 out:
1235 return VN_INACTIVE_CACHE;
1236}
1237
Barry Naujok384f3ce2008-05-21 16:58:22 +10001238/*
1239 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1240 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1241 * ci_name->name will point to a the actual name (caller must free) or
1242 * will be set to NULL if an exact match is found.
1243 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001244int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001246 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001247 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001248 xfs_inode_t **ipp,
1249 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001251 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int error;
1253 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001255 trace_xfs_lookup(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1258 return XFS_ERROR(EIO);
1259
1260 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001261 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001263
1264 if (error)
1265 goto out;
1266
Dave Chinner7b6259e2010-06-24 11:35:17 +10001267 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001268 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001269 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001270
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001271 return 0;
1272
Barry Naujok384f3ce2008-05-21 16:58:22 +10001273out_free_name:
1274 if (ci_name)
1275 kmem_free(ci_name->name);
1276out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001277 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return error;
1279}
1280
Christoph Hellwig993386c2007-08-28 16:12:30 +10001281int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001283 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001284 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001285 mode_t mode,
1286 xfs_dev_t rdev,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001287 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001289 int is_dir = S_ISDIR(mode);
1290 struct xfs_mount *mp = dp->i_mount;
1291 struct xfs_inode *ip = NULL;
1292 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +10001293 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 xfs_bmap_free_t free_list;
1295 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001296 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 uint cancel_flags;
1298 int committed;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001299 prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001300 struct xfs_dquot *udqp = NULL;
1301 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001303 uint log_res;
1304 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001306 trace_xfs_create(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001308 if (XFS_FORCED_SHUTDOWN(mp))
1309 return XFS_ERROR(EIO);
1310
Nathan Scott365ca832005-06-21 15:39:12 +10001311 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001312 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001314 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /*
1317 * Make sure that we have allocated dquot(s) on disk.
1318 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001319 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001320 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001322 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001324 if (is_dir) {
1325 rdev = 0;
1326 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1327 log_res = XFS_MKDIR_LOG_RES(mp);
1328 log_count = XFS_MKDIR_LOG_COUNT;
1329 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1330 } else {
1331 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1332 log_res = XFS_CREATE_LOG_RES(mp);
1333 log_count = XFS_CREATE_LOG_COUNT;
1334 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /*
1340 * Initially assume that the file does not exist and
1341 * reserve the resources for that case. If that is not
1342 * the case we'll drop the one we have and get a more
1343 * appropriate transaction later.
1344 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001345 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1346 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +02001348 /* flush outstanding delalloc blocks and retry */
1349 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -05001350 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1351 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +02001352 }
1353 if (error == ENOSPC) {
1354 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001356 error = xfs_trans_reserve(tp, 0, log_res, 0,
1357 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 if (error) {
1360 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001361 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001364 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001365 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001367 /*
1368 * Check for directory link count overflow.
1369 */
1370 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1371 error = XFS_ERROR(EMLINK);
1372 goto out_trans_cancel;
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001375 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /*
1378 * Reserve disk quota and the inode.
1379 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001380 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001382 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Barry Naujok556b8b12008-04-10 12:22:07 +10001384 error = xfs_dir_canenter(tp, dp, name, resblks);
1385 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001386 goto out_trans_cancel;
1387
1388 /*
1389 * A newly created regular or special file just has one directory
1390 * entry pointing to them, but a directory also the "." entry
1391 * pointing to itself.
1392 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001393 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001394 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (error) {
1396 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001397 goto out_trans_cancel;
1398 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001402 * Now we join the directory inode to the transaction. We do not do it
1403 * earlier because xfs_dir_ialloc might commit the previous transaction
1404 * (and release all the locks). An error from here on will result in
1405 * the transaction cancel unlocking dp so don't do it explicitly in the
1406 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001408 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001409 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Barry Naujok556b8b12008-04-10 12:22:07 +10001411 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001412 &first_block, &free_list, resblks ?
1413 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (error) {
1415 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001416 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001418 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1420
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001421 if (is_dir) {
1422 error = xfs_dir_init(tp, ip, dp);
1423 if (error)
1424 goto out_bmap_cancel;
1425
1426 error = xfs_bumplink(tp, dp);
1427 if (error)
1428 goto out_bmap_cancel;
1429 }
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /*
1432 * If this is a synchronous mount, make sure that the
1433 * create transaction goes to disk before returning to
1434 * the user.
1435 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001436 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /*
1440 * Attach the dquot(s) to the inodes and modify them incore.
1441 * These ids of the inode couldn't have changed since the new
1442 * inode has been locked ever since it was created.
1443 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001444 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001446 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001447 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001448 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001450 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001451 if (error)
1452 goto out_release_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Christoph Hellwig7d095252009-06-08 15:33:32 +02001454 xfs_qm_dqrele(udqp);
1455 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001457 *ipp = ip;
Christoph Hellwig288699f2010-06-23 18:11:15 +10001458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001460 out_bmap_cancel:
1461 xfs_bmap_cancel(&free_list);
1462 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001464 out_trans_cancel:
1465 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001466 out_release_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /*
1468 * Wait until after the current transaction is aborted to
1469 * release the inode. This prevents recursive transactions
1470 * and deadlocks from xfs_inactive.
1471 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001472 if (ip)
1473 IRELE(ip);
1474
1475 xfs_qm_dqrele(udqp);
1476 xfs_qm_dqrele(gdqp);
1477
1478 if (unlock_dp_on_error)
1479 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1480 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484int xfs_locked_n;
1485int xfs_small_retries;
1486int xfs_middle_retries;
1487int xfs_lots_retries;
1488int xfs_lock_delays;
1489#endif
1490
1491/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001492 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1493 * a different value
1494 */
1495static inline int
1496xfs_lock_inumorder(int lock_mode, int subclass)
1497{
1498 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001499 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001500 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001501 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001502
1503 return lock_mode;
1504}
1505
1506/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * The following routine will lock n inodes in exclusive mode.
1508 * We assume the caller calls us with the inodes in i_ino order.
1509 *
1510 * We need to detect deadlock where an inode that we lock
1511 * is in the AIL and we start waiting for another inode that is locked
1512 * by a thread in a long running transaction (such as truncate). This can
1513 * result in deadlock since the long running trans might need to wait
1514 * for the inode we just locked in order to push the tail and free space
1515 * in the log.
1516 */
1517void
1518xfs_lock_inodes(
1519 xfs_inode_t **ips,
1520 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 uint lock_mode)
1522{
1523 int attempts = 0, i, j, try_lock;
1524 xfs_log_item_t *lp;
1525
1526 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1527
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001528 try_lock = 0;
1529 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531again:
1532 for (; i < inodes; i++) {
1533 ASSERT(ips[i]);
1534
1535 if (i && (ips[i] == ips[i-1])) /* Already locked */
1536 continue;
1537
1538 /*
1539 * If try_lock is not set yet, make sure all locked inodes
1540 * are not in the AIL.
1541 * If any are, set try_lock to be used later.
1542 */
1543
1544 if (!try_lock) {
1545 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1546 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1547 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1548 try_lock++;
1549 }
1550 }
1551 }
1552
1553 /*
1554 * If any of the previous locks we have locked is in the AIL,
1555 * we must TRY to get the second and subsequent locks. If
1556 * we can't get any, we must release all we have
1557 * and try again.
1558 */
1559
1560 if (try_lock) {
1561 /* try_lock must be 0 if i is 0. */
1562 /*
1563 * try_lock means we have an inode locked
1564 * that is in the AIL.
1565 */
1566 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001567 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 attempts++;
1569
1570 /*
1571 * Unlock all previous guys and try again.
1572 * xfs_iunlock will try to push the tail
1573 * if the inode is in the AIL.
1574 */
1575
1576 for(j = i - 1; j >= 0; j--) {
1577
1578 /*
1579 * Check to see if we've already
1580 * unlocked this one.
1581 * Not the first one going back,
1582 * and the inode ptr is the same.
1583 */
1584 if ((j != (i - 1)) && ips[j] ==
1585 ips[j+1])
1586 continue;
1587
1588 xfs_iunlock(ips[j], lock_mode);
1589 }
1590
1591 if ((attempts % 5) == 0) {
1592 delay(1); /* Don't just spin the CPU */
1593#ifdef DEBUG
1594 xfs_lock_delays++;
1595#endif
1596 }
1597 i = 0;
1598 try_lock = 0;
1599 goto again;
1600 }
1601 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001602 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604 }
1605
1606#ifdef DEBUG
1607 if (attempts) {
1608 if (attempts < 5) xfs_small_retries++;
1609 else if (attempts < 100) xfs_middle_retries++;
1610 else xfs_lots_retries++;
1611 } else {
1612 xfs_locked_n++;
1613 }
1614#endif
1615}
1616
David Chinnerf9114eb2008-09-17 16:51:21 +10001617/*
1618 * xfs_lock_two_inodes() can only be used to lock one type of lock
1619 * at a time - the iolock or the ilock, but not both at once. If
1620 * we lock both at once, lockdep will report false positives saying
1621 * we have violated locking orders.
1622 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001623void
1624xfs_lock_two_inodes(
1625 xfs_inode_t *ip0,
1626 xfs_inode_t *ip1,
1627 uint lock_mode)
1628{
1629 xfs_inode_t *temp;
1630 int attempts = 0;
1631 xfs_log_item_t *lp;
1632
David Chinnerf9114eb2008-09-17 16:51:21 +10001633 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1634 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001635 ASSERT(ip0->i_ino != ip1->i_ino);
1636
1637 if (ip0->i_ino > ip1->i_ino) {
1638 temp = ip0;
1639 ip0 = ip1;
1640 ip1 = temp;
1641 }
1642
1643 again:
1644 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1645
1646 /*
1647 * If the first lock we have locked is in the AIL, we must TRY to get
1648 * the second lock. If we can't get it, we must release the first one
1649 * and try again.
1650 */
1651 lp = (xfs_log_item_t *)ip0->i_itemp;
1652 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1653 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1654 xfs_iunlock(ip0, lock_mode);
1655 if ((++attempts % 5) == 0)
1656 delay(1); /* Don't just spin the CPU */
1657 goto again;
1658 }
1659 } else {
1660 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1661 }
1662}
1663
Christoph Hellwig993386c2007-08-28 16:12:30 +10001664int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001666 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001667 struct xfs_name *name,
1668 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001670 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001672 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 int error = 0;
1674 xfs_bmap_free_t free_list;
1675 xfs_fsblock_t first_block;
1676 int cancel_flags;
1677 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int link_zero;
1679 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001680 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001682 trace_xfs_remove(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (XFS_FORCED_SHUTDOWN(mp))
1685 return XFS_ERROR(EIO);
1686
Christoph Hellwig7d095252009-06-08 15:33:32 +02001687 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001688 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Christoph Hellwig7d095252009-06-08 15:33:32 +02001691 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001692 if (error)
1693 goto std_return;
1694
1695 if (is_dir) {
1696 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1697 log_count = XFS_DEFAULT_LOG_COUNT;
1698 } else {
1699 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1700 log_count = XFS_REMOVE_LOG_COUNT;
1701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /*
1705 * We try to get the real space reservation first,
1706 * allowing for directory btree deletion(s) implying
1707 * possible bmap insert(s). If we can't get the space
1708 * reservation then we use 0 instead, and avoid the bmap
1709 * btree insert(s) in the directory code by, if the bmap
1710 * insert tries to happen, instead trimming the LAST
1711 * block from the directory.
1712 */
1713 resblks = XFS_REMOVE_SPACE_RES(mp);
1714 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001715 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (error == ENOSPC) {
1717 resblks = 0;
1718 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001719 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 if (error) {
1722 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001723 cancel_flags = 0;
1724 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001727 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Christoph Hellwig898621d2010-06-24 11:36:58 +10001729 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1730 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001733 * If we're removing a directory perform some additional validation.
1734 */
1735 if (is_dir) {
1736 ASSERT(ip->i_d.di_nlink >= 2);
1737 if (ip->i_d.di_nlink != 2) {
1738 error = XFS_ERROR(ENOTEMPTY);
1739 goto out_trans_cancel;
1740 }
1741 if (!xfs_dir_isempty(ip)) {
1742 error = XFS_ERROR(ENOTEMPTY);
1743 goto out_trans_cancel;
1744 }
1745 }
1746
Eric Sandeen9d87c312009-01-14 23:22:07 -06001747 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001748 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001749 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (error) {
1751 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001752 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001754 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001756 if (is_dir) {
1757 /*
1758 * Drop the link from ip's "..".
1759 */
1760 error = xfs_droplink(tp, dp);
1761 if (error)
1762 goto out_bmap_cancel;
1763
1764 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001765 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001766 */
1767 error = xfs_droplink(tp, ip);
1768 if (error)
1769 goto out_bmap_cancel;
1770 } else {
1771 /*
1772 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001773 * inode here. For a directory this is done implicitly
1774 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001775 */
1776 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001779 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001780 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001781 */
1782 error = xfs_droplink(tp, ip);
1783 if (error)
1784 goto out_bmap_cancel;
1785
1786 /*
1787 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 * we are in the transaction.
1789 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001790 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 * If this is a synchronous mount, make sure that the
1794 * remove transaction goes to disk before returning to
1795 * the user.
1796 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001797 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001800 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001801 if (error)
1802 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001804 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001805 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001809 * If we are using filestreams, kill the stream association.
1810 * If the file is still open it may get a new one but that
1811 * will get killed on last close in xfs_close() so we don't
1812 * have to worry about that.
1813 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001814 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001815 xfs_filestream_deassociate(ip);
1816
Christoph Hellwig288699f2010-06-23 18:11:15 +10001817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001819 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 xfs_bmap_cancel(&free_list);
1821 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001822 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001824 std_return:
1825 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Christoph Hellwig993386c2007-08-28 16:12:30 +10001828int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001830 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001831 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10001832 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001834 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int error;
1837 xfs_bmap_free_t free_list;
1838 xfs_fsblock_t first_block;
1839 int cancel_flags;
1840 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001843 trace_xfs_link(tdp, target_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001845 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (XFS_FORCED_SHUTDOWN(mp))
1848 return XFS_ERROR(EIO);
1849
Christoph Hellwig7d095252009-06-08 15:33:32 +02001850 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01001851 if (error)
1852 goto std_return;
1853
Christoph Hellwig7d095252009-06-08 15:33:32 +02001854 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (error)
1856 goto std_return;
1857
1858 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1859 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001860 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1862 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1863 if (error == ENOSPC) {
1864 resblks = 0;
1865 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1866 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1867 }
1868 if (error) {
1869 cancel_flags = 0;
1870 goto error_return;
1871 }
1872
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001873 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Christoph Hellwig898621d2010-06-24 11:36:58 +10001875 xfs_trans_ijoin_ref(tp, sip, XFS_ILOCK_EXCL);
1876 xfs_trans_ijoin_ref(tp, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 /*
1879 * If the source has too many links, we can't make any more to it.
1880 */
1881 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1882 error = XFS_ERROR(EMLINK);
1883 goto error_return;
1884 }
1885
Nathan Scott365ca832005-06-21 15:39:12 +10001886 /*
1887 * If we are using project inheritance, we only allow hard link
1888 * creation in our tree when the project IDs are the same; else
1889 * the tree quota mechanism could be circumvented.
1890 */
1891 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001892 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10001893 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10001894 goto error_return;
1895 }
1896
Barry Naujok556b8b12008-04-10 12:22:07 +10001897 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1898 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 goto error_return;
1900
Eric Sandeen9d87c312009-01-14 23:22:07 -06001901 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Barry Naujok556b8b12008-04-10 12:22:07 +10001903 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1904 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (error)
1906 goto abort_return;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001907 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1909
1910 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001911 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /*
1915 * If this is a synchronous mount, make sure that the
1916 * link transaction goes to disk before returning to
1917 * the user.
1918 */
1919 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1920 xfs_trans_set_sync(tp);
1921 }
1922
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001923 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (error) {
1925 xfs_bmap_cancel(&free_list);
1926 goto abort_return;
1927 }
1928
Christoph Hellwig288699f2010-06-23 18:11:15 +10001929 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 abort_return:
1932 cancel_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 error_return:
1934 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001935 std_return:
1936 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001938
Christoph Hellwig993386c2007-08-28 16:12:30 +10001939int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001941 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001942 struct xfs_name *link_name,
1943 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001944 mode_t mode,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001945 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001947 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 xfs_inode_t *ip;
1950 int error;
1951 int pathlen;
1952 xfs_bmap_free_t free_list;
1953 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001954 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 uint cancel_flags;
1956 int committed;
1957 xfs_fileoff_t first_fsb;
1958 xfs_filblks_t fs_blocks;
1959 int nmaps;
1960 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1961 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10001962 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 int byte_cnt;
1964 int n;
1965 xfs_buf_t *bp;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001966 prid_t prid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 struct xfs_dquot *udqp, *gdqp;
1968 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Christoph Hellwig3937be52008-03-06 13:46:19 +11001970 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 error = 0;
1972 ip = NULL;
1973 tp = NULL;
1974
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001975 trace_xfs_symlink(dp, link_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 if (XFS_FORCED_SHUTDOWN(mp))
1978 return XFS_ERROR(EIO);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /*
1981 * Check component lengths of the target path name.
1982 */
1983 pathlen = strlen(target_path);
1984 if (pathlen >= MAXPATHLEN) /* total string too long */
1985 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001988 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001989 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001991 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 /*
1994 * Make sure that we have allocated dquot(s) on disk.
1995 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001996 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1998 if (error)
1999 goto std_return;
2000
2001 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2002 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2003 /*
2004 * The symlink will fit into the inode data fork?
2005 * There can't be any attributes so we get the whole variable part.
2006 */
2007 if (pathlen <= XFS_LITINO(mp))
2008 fs_blocks = 0;
2009 else
2010 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002011 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2013 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2014 if (error == ENOSPC && fs_blocks == 0) {
2015 resblks = 0;
2016 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2017 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2018 }
2019 if (error) {
2020 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 goto error_return;
2022 }
2023
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002024 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002025 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 /*
2028 * Check whether the directory allows new symlinks or not.
2029 */
2030 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2031 error = XFS_ERROR(EPERM);
2032 goto error_return;
2033 }
2034
2035 /*
2036 * Reserve disk quota : blocks and inode.
2037 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002038 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (error)
2040 goto error_return;
2041
2042 /*
2043 * Check for ability to enter directory entry, if no space reserved.
2044 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002045 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2046 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 goto error_return;
2048 /*
2049 * Initialize the bmap freelist prior to calling either
2050 * bmapi or the directory create code.
2051 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002052 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 /*
2055 * Allocate an inode for the symlink.
2056 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00002057 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
2058 prid, resblks > 0, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (error) {
2060 if (error == ENOSPC)
2061 goto error_return;
2062 goto error1;
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Christoph Hellwig993386c2007-08-28 16:12:30 +10002065 /*
2066 * An error after we've joined dp to the transaction will result in the
2067 * transaction cancel unlocking dp so don't do it explicitly in the
2068 * error path.
2069 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10002070 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002071 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /*
2074 * Also attach the dquot(s) to it, if applicable.
2075 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002076 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 if (resblks)
2079 resblks -= XFS_IALLOC_SPACE_RES(mp);
2080 /*
2081 * If the symlink will fit into the inode, write it inline.
2082 */
2083 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2084 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2085 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2086 ip->i_d.di_size = pathlen;
2087
2088 /*
2089 * The inode was initially created in extent format.
2090 */
2091 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2092 ip->i_df.if_flags |= XFS_IFINLINE;
2093
2094 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2095 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2096
2097 } else {
2098 first_fsb = 0;
2099 nmaps = SYMLINK_MAPS;
2100
2101 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2102 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2103 &first_block, resblks, mval, &nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002104 &free_list);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00002105 if (error)
2106 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 if (resblks)
2109 resblks -= fs_blocks;
2110 ip->i_d.di_size = pathlen;
2111 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2112
2113 cur_chunk = target_path;
2114 for (n = 0; n < nmaps; n++) {
2115 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2116 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2117 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2118 BTOBB(byte_cnt), 0);
2119 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2120 if (pathlen < byte_cnt) {
2121 byte_cnt = pathlen;
2122 }
2123 pathlen -= byte_cnt;
2124
2125 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2126 cur_chunk += byte_cnt;
2127
2128 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2129 }
2130 }
2131
2132 /*
2133 * Create the directory entry for the symlink.
2134 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002135 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2136 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002137 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +00002138 goto error2;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10002139 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2141
2142 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 * If this is a synchronous mount, make sure that the
2144 * symlink transaction goes to disk before returning to
2145 * the user.
2146 */
2147 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2148 xfs_trans_set_sync(tp);
2149 }
2150
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002151 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (error) {
2153 goto error2;
2154 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002155 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002156 xfs_qm_dqrele(udqp);
2157 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Christoph Hellwig288699f2010-06-23 18:11:15 +10002159 *ipp = ip;
2160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 error2:
2163 IRELE(ip);
2164 error1:
2165 xfs_bmap_cancel(&free_list);
2166 cancel_flags |= XFS_TRANS_ABORT;
2167 error_return:
2168 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002169 xfs_qm_dqrele(udqp);
2170 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Christoph Hellwig993386c2007-08-28 16:12:30 +10002172 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002174 std_return:
2175 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002179xfs_set_dmattrs(
2180 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002182 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002184 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 int error;
2187
2188 if (!capable(CAP_SYS_ADMIN))
2189 return XFS_ERROR(EPERM);
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (XFS_FORCED_SHUTDOWN(mp))
2192 return XFS_ERROR(EIO);
2193
2194 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2195 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2196 if (error) {
2197 xfs_trans_cancel(tp, 0);
2198 return error;
2199 }
2200 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10002201 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Christoph Hellwig613d7042007-10-11 17:44:08 +10002203 ip->i_d.di_dmevmask = evmask;
2204 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002207 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 return error;
2210}
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212/*
2213 * xfs_alloc_file_space()
2214 * This routine allocates disk space for the given file.
2215 *
2216 * If alloc_type == 0, this request is for an ALLOCSP type
2217 * request which will change the file size. In this case, no
2218 * DMAPI event will be generated by the call. A TRUNCATE event
2219 * will be generated later by xfs_setattr.
2220 *
2221 * If alloc_type != 0, this request is for a RESVSP type
2222 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2223 * lower block boundary byte address is less than the file's
2224 * length.
2225 *
2226 * RETURNS:
2227 * 0 on success
2228 * errno on error
2229 *
2230 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002231STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232xfs_alloc_file_space(
2233 xfs_inode_t *ip,
2234 xfs_off_t offset,
2235 xfs_off_t len,
2236 int alloc_type,
2237 int attr_flags)
2238{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002239 xfs_mount_t *mp = ip->i_mount;
2240 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 xfs_filblks_t allocated_fsb;
2242 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002243 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002245 xfs_fsblock_t firstfsb;
2246 int nimaps;
2247 int bmapi_flag;
2248 int quota_flag;
2249 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002251 xfs_bmbt_irec_t imaps[1], *imapp;
2252 xfs_bmap_free_t free_list;
2253 uint qblocks, resblks, resrtextents;
2254 int committed;
2255 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002257 trace_xfs_alloc_file_space(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 if (XFS_FORCED_SHUTDOWN(mp))
2260 return XFS_ERROR(EIO);
2261
Christoph Hellwig7d095252009-06-08 15:33:32 +02002262 error = xfs_qm_dqattach(ip, 0);
2263 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return error;
2265
2266 if (len <= 0)
2267 return XFS_ERROR(EINVAL);
2268
David Chinner957d0eb2007-06-18 16:50:37 +10002269 rt = XFS_IS_REALTIME_INODE(ip);
2270 extsz = xfs_get_extsz_hint(ip);
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002274 nimaps = 1;
Dave Chinner44722352010-08-24 12:02:11 +10002275 bmapi_flag = XFS_BMAPI_WRITE | alloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2277 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002280 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002283 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Nathan Scottdd9f4382006-01-11 15:28:28 +11002285 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002286 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002287 */
2288 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002290 do_div(s, extsz);
2291 s *= extsz;
2292 e = startoffset_fsb + allocatesize_fsb;
2293 if ((temp = do_mod(startoffset_fsb, extsz)))
2294 e += temp;
2295 if ((temp = do_mod(e, extsz)))
2296 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002298 s = 0;
2299 e = allocatesize_fsb;
2300 }
2301
Dave Chinner72656c42010-09-03 12:19:33 +10002302 /*
2303 * The transaction reservation is limited to a 32-bit block
2304 * count, hence we need to limit the number of blocks we are
2305 * trying to reserve to avoid an overflow. We can't allocate
2306 * more than @nimaps extents, and an extent is limited on disk
2307 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
2308 */
2309 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
Nathan Scottdd9f4382006-01-11 15:28:28 +11002310 if (unlikely(rt)) {
Dave Chinner72656c42010-09-03 12:19:33 +10002311 resrtextents = qblocks = resblks;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002312 resrtextents /= mp->m_sb.sb_rextsize;
2313 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2314 quota_flag = XFS_QMOPT_RES_RTBLKS;
2315 } else {
2316 resrtextents = 0;
Dave Chinner72656c42010-09-03 12:19:33 +10002317 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002318 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320
2321 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002322 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 */
2324 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002325 error = xfs_trans_reserve(tp, resblks,
2326 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 XFS_TRANS_PERM_LOG_RES,
2328 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002330 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 */
2332 if (error) {
2333 /*
2334 * Free the transaction structure.
2335 */
2336 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2337 xfs_trans_cancel(tp, 0);
2338 break;
2339 }
2340 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002341 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2342 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (error)
2344 goto error1;
2345
Christoph Hellwig898621d2010-06-24 11:36:58 +10002346 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002349 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002351 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002352 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002353 allocatesize_fsb, bmapi_flag,
2354 &firstfsb, 0, imapp, &nimaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002355 &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (error) {
2357 goto error0;
2358 }
2359
2360 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002361 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002363 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (error) {
2365 goto error0;
2366 }
2367
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002368 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2370 if (error) {
2371 break;
2372 }
2373
2374 allocated_fsb = imapp->br_blockcount;
2375
Nathan Scottdd9f4382006-01-11 15:28:28 +11002376 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 error = XFS_ERROR(ENOSPC);
2378 break;
2379 }
2380
2381 startoffset_fsb += allocated_fsb;
2382 allocatesize_fsb -= allocated_fsb;
2383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 return error;
2386
Nathan Scottdd9f4382006-01-11 15:28:28 +11002387error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002389 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002390
2391error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2393 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002394 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
2397/*
2398 * Zero file bytes between startoff and endoff inclusive.
2399 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002400 *
2401 * This function is used by xfs_free_file_space() to zero
2402 * partial blocks when the range to free is not block aligned.
2403 * When unreserving space with boundaries that are not block
2404 * aligned we round up the start and round down the end
2405 * boundaries and then use this function to zero the parts of
2406 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 */
2408STATIC int
2409xfs_zero_remaining_bytes(
2410 xfs_inode_t *ip,
2411 xfs_off_t startoff,
2412 xfs_off_t endoff)
2413{
2414 xfs_bmbt_irec_t imap;
2415 xfs_fileoff_t offset_fsb;
2416 xfs_off_t lastoffset;
2417 xfs_off_t offset;
2418 xfs_buf_t *bp;
2419 xfs_mount_t *mp = ip->i_mount;
2420 int nimap;
2421 int error = 0;
2422
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002423 /*
2424 * Avoid doing I/O beyond eof - it's not necessary
2425 * since nothing can read beyond eof. The space will
2426 * be zeroed when the file is extended anyway.
2427 */
2428 if (startoff >= ip->i_size)
2429 return 0;
2430
2431 if (endoff > ip->i_size)
2432 endoff = ip->i_size;
2433
Dave Chinner686865f2010-09-24 20:07:47 +10002434 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
2435 mp->m_rtdev_targp : mp->m_ddev_targp,
2436 mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11002437 if (!bp)
2438 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2441 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2442 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002443 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002444 NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (error || nimap < 1)
2446 break;
2447 ASSERT(imap.br_blockcount >= 1);
2448 ASSERT(imap.br_startoff == offset_fsb);
2449 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2450 if (lastoffset > endoff)
2451 lastoffset = endoff;
2452 if (imap.br_startblock == HOLESTARTBLOCK)
2453 continue;
2454 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2455 if (imap.br_state == XFS_EXT_UNWRITTEN)
2456 continue;
2457 XFS_BUF_UNDONE(bp);
2458 XFS_BUF_UNWRITE(bp);
2459 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002460 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002462 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002463 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2465 mp, bp, XFS_BUF_ADDR(bp));
2466 break;
2467 }
2468 memset(XFS_BUF_PTR(bp) +
2469 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2470 0, lastoffset - offset + 1);
2471 XFS_BUF_UNDONE(bp);
2472 XFS_BUF_UNREAD(bp);
2473 XFS_BUF_WRITE(bp);
2474 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002475 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002476 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2478 mp, bp, XFS_BUF_ADDR(bp));
2479 break;
2480 }
2481 }
2482 xfs_buf_free(bp);
2483 return error;
2484}
2485
2486/*
2487 * xfs_free_file_space()
2488 * This routine frees disk space for the given file.
2489 *
2490 * This routine is only called by xfs_change_file_space
2491 * for an UNRESVSP type call.
2492 *
2493 * RETURNS:
2494 * 0 on success
2495 * errno on error
2496 *
2497 */
2498STATIC int
2499xfs_free_file_space(
2500 xfs_inode_t *ip,
2501 xfs_off_t offset,
2502 xfs_off_t len,
2503 int attr_flags)
2504{
2505 int committed;
2506 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 xfs_fileoff_t endoffset_fsb;
2508 int error;
2509 xfs_fsblock_t firstfsb;
2510 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 xfs_bmbt_irec_t imap;
2512 xfs_off_t ioffset;
2513 xfs_extlen_t mod=0;
2514 xfs_mount_t *mp;
2515 int nimap;
2516 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002517 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 int rt;
2519 xfs_fileoff_t startoffset_fsb;
2520 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002521 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 mp = ip->i_mount;
2524
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002525 trace_xfs_free_file_space(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002526
Christoph Hellwig7d095252009-06-08 15:33:32 +02002527 error = xfs_qm_dqattach(ip, 0);
2528 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return error;
2530
2531 error = 0;
2532 if (len <= 0) /* if nothing being freed */
2533 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002534 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002536 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002538 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002539 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002540 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002542 /* wait for the completion of any pending DIOs */
2543 xfs_ioend_wait(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11002544 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002545
Tim Shimmine6a4b372007-11-23 16:30:42 +11002546 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002548
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002549 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002550 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002551 if (error)
2552 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002553 }
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /*
2556 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002557 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2559 * will take care of it for us.
2560 */
Eric Sandeen62118702008-03-06 13:44:28 +11002561 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002563 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002564 1, 0, NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (error)
2566 goto out_unlock_iolock;
2567 ASSERT(nimap == 0 || nimap == 1);
2568 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2569 xfs_daddr_t block;
2570
2571 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2572 block = imap.br_startblock;
2573 mod = do_div(block, mp->m_sb.sb_rextsize);
2574 if (mod)
2575 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2576 }
2577 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002578 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002579 1, 0, NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (error)
2581 goto out_unlock_iolock;
2582 ASSERT(nimap == 0 || nimap == 1);
2583 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2584 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2585 mod++;
2586 if (mod && (mod != mp->m_sb.sb_rextsize))
2587 endoffset_fsb -= mod;
2588 }
2589 }
2590 if ((done = (endoffset_fsb <= startoffset_fsb)))
2591 /*
2592 * One contiguous piece to clear
2593 */
2594 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2595 else {
2596 /*
2597 * Some full blocks, possibly two pieces to clear
2598 */
2599 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2600 error = xfs_zero_remaining_bytes(ip, offset,
2601 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2602 if (!error &&
2603 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2604 error = xfs_zero_remaining_bytes(ip,
2605 XFS_FSB_TO_B(mp, endoffset_fsb),
2606 offset + len - 1);
2607 }
2608
2609 /*
2610 * free file space until done or until there is an error
2611 */
2612 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2613 while (!error && !done) {
2614
2615 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002616 * allocate and setup the transaction. Allow this
2617 * transaction to dip into the reserve blocks to ensure
2618 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 */
2620 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002621 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 error = xfs_trans_reserve(tp,
2623 resblks,
2624 XFS_WRITE_LOG_RES(mp),
2625 0,
2626 XFS_TRANS_PERM_LOG_RES,
2627 XFS_WRITE_LOG_COUNT);
2628
2629 /*
2630 * check for running out of space
2631 */
2632 if (error) {
2633 /*
2634 * Free the transaction structure.
2635 */
2636 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2637 xfs_trans_cancel(tp, 0);
2638 break;
2639 }
2640 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002641 error = xfs_trans_reserve_quota(tp, mp,
2642 ip->i_udquot, ip->i_gdquot,
2643 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (error)
2645 goto error1;
2646
Christoph Hellwig898621d2010-06-24 11:36:58 +10002647 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 /*
2650 * issue the bunmapi() call to free the blocks
2651 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002652 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002653 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 endoffset_fsb - startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002655 0, 2, &firstfsb, &free_list, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (error) {
2657 goto error0;
2658 }
2659
2660 /*
2661 * complete the transaction
2662 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002663 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (error) {
2665 goto error0;
2666 }
2667
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002668 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2670 }
2671
2672 out_unlock_iolock:
2673 if (need_iolock)
2674 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2675 return error;
2676
2677 error0:
2678 xfs_bmap_cancel(&free_list);
2679 error1:
2680 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2681 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2682 XFS_ILOCK_EXCL);
2683 return error;
2684}
2685
2686/*
2687 * xfs_change_file_space()
2688 * This routine allocates or frees disk space for the given file.
2689 * The user specified parameters are checked for alignment and size
2690 * limitations.
2691 *
2692 * RETURNS:
2693 * 0 on success
2694 * errno on error
2695 *
2696 */
2697int
2698xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002699 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 int cmd,
2701 xfs_flock64_t *bf,
2702 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 int attr_flags)
2704{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002705 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 int clrprealloc;
2707 int error;
2708 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 int setprealloc;
2710 xfs_off_t startoffset;
2711 xfs_off_t llen;
2712 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002713 struct iattr iattr;
Dave Chinner44722352010-08-24 12:02:11 +10002714 int prealloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Christoph Hellwig993386c2007-08-28 16:12:30 +10002716 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return XFS_ERROR(EINVAL);
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 switch (bf->l_whence) {
2720 case 0: /*SEEK_SET*/
2721 break;
2722 case 1: /*SEEK_CUR*/
2723 bf->l_start += offset;
2724 break;
2725 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002726 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 break;
2728 default:
2729 return XFS_ERROR(EINVAL);
2730 }
2731
2732 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2733
2734 if ( (bf->l_start < 0)
2735 || (bf->l_start > XFS_MAXIOFFSET(mp))
2736 || (bf->l_start + llen < 0)
2737 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2738 return XFS_ERROR(EINVAL);
2739
2740 bf->l_whence = 0;
2741
2742 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002743 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 /*
2746 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2747 * file space.
2748 * These calls do NOT zero the data space allocated to the file,
2749 * nor do they change the file size.
2750 *
2751 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2752 * space.
2753 * These calls cause the new file data to be zeroed and the file
2754 * size to be changed.
2755 */
2756 setprealloc = clrprealloc = 0;
Dave Chinner44722352010-08-24 12:02:11 +10002757 prealloc_type = XFS_BMAPI_PREALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 switch (cmd) {
Dave Chinner44722352010-08-24 12:02:11 +10002760 case XFS_IOC_ZERO_RANGE:
2761 prealloc_type |= XFS_BMAPI_CONVERT;
2762 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2763 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 case XFS_IOC_RESVSP:
2765 case XFS_IOC_RESVSP64:
2766 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
Dave Chinner44722352010-08-24 12:02:11 +10002767 prealloc_type, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (error)
2769 return error;
2770 setprealloc = 1;
2771 break;
2772
2773 case XFS_IOC_UNRESVSP:
2774 case XFS_IOC_UNRESVSP64:
2775 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2776 attr_flags)))
2777 return error;
2778 break;
2779
2780 case XFS_IOC_ALLOCSP:
2781 case XFS_IOC_ALLOCSP64:
2782 case XFS_IOC_FREESP:
2783 case XFS_IOC_FREESP64:
2784 if (startoffset > fsize) {
2785 error = xfs_alloc_file_space(ip, fsize,
2786 startoffset - fsize, 0, attr_flags);
2787 if (error)
2788 break;
2789 }
2790
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002791 iattr.ia_valid = ATTR_SIZE;
2792 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11002794 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 if (error)
2797 return error;
2798
2799 clrprealloc = 1;
2800 break;
2801
2802 default:
2803 ASSERT(0);
2804 return XFS_ERROR(EINVAL);
2805 }
2806
2807 /*
2808 * update the inode timestamp, mode, and prealloc flag bits
2809 */
2810 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2811
2812 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2813 0, 0, 0))) {
2814 /* ASSERT(0); */
2815 xfs_trans_cancel(tp, 0);
2816 return error;
2817 }
2818
2819 xfs_ilock(ip, XFS_ILOCK_EXCL);
2820
Christoph Hellwig898621d2010-06-24 11:36:58 +10002821 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002823 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 ip->i_d.di_mode &= ~S_ISUID;
2825
2826 /*
2827 * Note that we don't have to worry about mandatory
2828 * file locking being disabled here because we only
2829 * clear the S_ISGID bit if the Group execute bit is
2830 * on, but if it was on then mandatory locking wouldn't
2831 * have been enabled.
2832 */
2833 if (ip->i_d.di_mode & S_IXGRP)
2834 ip->i_d.di_mode &= ~S_ISGID;
2835
Dave Chinnerdcd79a12010-09-28 12:27:25 +10002836 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838 if (setprealloc)
2839 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2840 else if (clrprealloc)
2841 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2842
2843 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinner82878892011-03-26 09:13:08 +11002844 if (attr_flags & XFS_ATTR_SYNC)
2845 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002847 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2850
2851 return error;
2852}