blob: d8e6f8cd6f0c89782dd0d5df7a2e7a0329688235 [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 Hellwig993386c12007-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 Hellwig993386c12007-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 Hellwig993386c12007-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 Hellwig993386c12007-08-28 16:12:30 +1000532int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533xfs_readlink(
Christoph Hellwig993386c12007-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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int 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
548 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000549 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000551 pathlen = ip->i_d.di_size;
552 if (!pathlen)
553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000556 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
557 link[pathlen] = '\0';
558 } else {
559 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000562 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return error;
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000568 * Flags for xfs_free_eofblocks
569 */
570#define XFS_FREE_EOF_TRYLOCK (1<<0)
571
572/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000573 * This is called by xfs_inactive to free any blocks beyond eof
574 * when the link count isn't zero and by xfs_dm_punch_hole() when
575 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500577STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000578xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000580 xfs_inode_t *ip,
581 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 xfs_trans_t *tp;
584 int error;
585 xfs_fileoff_t end_fsb;
586 xfs_fileoff_t last_fsb;
587 xfs_filblks_t map_len;
588 int nimaps;
589 xfs_bmbt_irec_t imap;
590
591 /*
592 * Figure out if there are any blocks beyond the end
593 * of the file. If not, then there is nothing to do.
594 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000595 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000597 if (last_fsb <= end_fsb)
Jesper Juhl014c2542006-01-15 02:37:08 +0100598 return 0;
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000599 map_len = last_fsb - end_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 nimaps = 1;
602 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000603 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000604 NULL, 0, &imap, &nimaps, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 xfs_iunlock(ip, XFS_ILOCK_SHARED);
606
607 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100608 (imap.br_startblock != HOLESTARTBLOCK ||
609 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
611 * Attach the dquots to the inode up front.
612 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200613 error = xfs_qm_dqattach(ip, 0);
614 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100615 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /*
618 * There are blocks after the end of file.
619 * Free them up now by truncating the file to
620 * its current size.
621 */
622 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
623
624 /*
625 * Do the xfs_itruncate_start() call before
626 * reserving any log space because
627 * itruncate_start will call into the buffer
628 * cache and we can't
629 * do that within a transaction.
630 */
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000631 if (flags & XFS_FREE_EOF_TRYLOCK) {
632 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
633 xfs_trans_cancel(tp, 0);
634 return 0;
635 }
636 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000637 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000638 }
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000639 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000640 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000641 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000642 xfs_trans_cancel(tp, 0);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000643 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000644 return error;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 error = xfs_trans_reserve(tp, 0,
648 XFS_ITRUNCATE_LOG_RES(mp),
649 0, XFS_TRANS_PERM_LOG_RES,
650 XFS_ITRUNCATE_LOG_COUNT);
651 if (error) {
652 ASSERT(XFS_FORCED_SHUTDOWN(mp));
653 xfs_trans_cancel(tp, 0);
654 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100655 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
658 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000659 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000662 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 XFS_DATA_FORK,
664 0);
665 /*
666 * If we get an error at this point we
667 * simply don't bother truncating the file.
668 */
669 if (error) {
670 xfs_trans_cancel(tp,
671 (XFS_TRANS_RELEASE_LOG_RES |
672 XFS_TRANS_ABORT));
673 } else {
674 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000675 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000677 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100679 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682/*
683 * Free a symlink that has blocks associated with it.
684 */
685STATIC int
686xfs_inactive_symlink_rmt(
687 xfs_inode_t *ip,
688 xfs_trans_t **tpp)
689{
690 xfs_buf_t *bp;
691 int committed;
692 int done;
693 int error;
694 xfs_fsblock_t first_block;
695 xfs_bmap_free_t free_list;
696 int i;
697 xfs_mount_t *mp;
698 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
699 int nmaps;
700 xfs_trans_t *ntp;
701 int size;
702 xfs_trans_t *tp;
703
704 tp = *tpp;
705 mp = ip->i_mount;
706 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
707 /*
708 * We're freeing a symlink that has some
709 * blocks allocated to it. Free the
710 * blocks here. We know that we've got
711 * either 1 or 2 extents and that we can
712 * free them all in one bunmapi call.
713 */
714 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
715 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
716 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
717 ASSERT(XFS_FORCED_SHUTDOWN(mp));
718 xfs_trans_cancel(tp, 0);
719 *tpp = NULL;
720 return error;
721 }
722 /*
723 * Lock the inode, fix the size, and join it to the transaction.
724 * Hold it so in the normal path, we still have it locked for
725 * the second transaction. In the error paths we need it
726 * held so the cancel won't rele it, see below.
727 */
728 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
729 size = (int)ip->i_d.di_size;
730 ip->i_d.di_size = 0;
Christoph Hellwig898621d2010-06-24 11:36:58 +1000731 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
733 /*
734 * Find the block(s) so we can inval and unmap them.
735 */
736 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600737 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800738 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
740 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000741 &free_list)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 goto error0;
743 /*
744 * Invalidate the block(s).
745 */
746 for (i = 0; i < nmaps; i++) {
747 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
748 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
749 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
750 xfs_trans_binval(tp, bp);
751 }
752 /*
753 * Unmap the dead block(s) to the free_list.
754 */
755 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000756 &first_block, &free_list, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto error1;
758 ASSERT(done);
759 /*
760 * Commit the first transaction. This logs the EFI and the inode.
761 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100762 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto error1;
764 /*
765 * The transaction must have been committed, since there were
766 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
767 * The new tp has the extent freeing and EFDs.
768 */
769 ASSERT(committed);
770 /*
771 * The first xact was committed, so add the inode to the new one.
772 * Mark it dirty so it will be logged and moved forward in the log as
773 * part of every commit.
774 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000775 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
777 /*
778 * Get a new, empty transaction to return to our caller.
779 */
780 ntp = xfs_trans_dup(tp);
781 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000782 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * If we get an error on the commit here or on the reserve below,
784 * we need to unlock the inode since the new transaction doesn't
785 * have the inode attached.
786 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000787 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 tp = ntp;
789 if (error) {
790 ASSERT(XFS_FORCED_SHUTDOWN(mp));
791 goto error0;
792 }
793 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100794 * transaction commit worked ok so we can drop the extra ticket
795 * reference that we gained in xfs_trans_dup()
796 */
797 xfs_log_ticket_put(tp->t_ticket);
798
799 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * Remove the memory for extent descriptions (just bookkeeping).
801 */
802 if (ip->i_df.if_bytes)
803 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
804 ASSERT(ip->i_df.if_bytes == 0);
805 /*
806 * Put an itruncate log reservation in the new transaction
807 * for our caller.
808 */
809 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
810 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
811 ASSERT(XFS_FORCED_SHUTDOWN(mp));
812 goto error0;
813 }
814 /*
815 * Return with the inode locked but not joined to the transaction.
816 */
817 *tpp = tp;
818 return 0;
819
820 error1:
821 xfs_bmap_cancel(&free_list);
822 error0:
823 /*
824 * Have to come here with the inode locked and either
825 * (held and in the transaction) or (not in the transaction).
826 * If the inode isn't held then cancel would iput it, but
827 * that's wrong since this is inactive and the vnode ref
828 * count is 0 already.
829 * Cancel won't do anything to the inode if held, but it still
830 * needs to be locked until the cancel is done, if it was
831 * joined to the transaction.
832 */
833 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
834 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
835 *tpp = NULL;
836 return error;
837
838}
839
840STATIC int
841xfs_inactive_symlink_local(
842 xfs_inode_t *ip,
843 xfs_trans_t **tpp)
844{
845 int error;
846
847 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
848 /*
849 * We're freeing a symlink which fit into
850 * the inode. Just free the memory used
851 * to hold the old symlink.
852 */
853 error = xfs_trans_reserve(*tpp, 0,
854 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
855 0, XFS_TRANS_PERM_LOG_RES,
856 XFS_ITRUNCATE_LOG_COUNT);
857
858 if (error) {
859 xfs_trans_cancel(*tpp, 0);
860 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100861 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
864
865 /*
866 * Zero length symlinks _can_ exist.
867 */
868 if (ip->i_df.if_bytes > 0) {
869 xfs_idata_realloc(ip,
870 -(ip->i_df.if_bytes),
871 XFS_DATA_FORK);
872 ASSERT(ip->i_df.if_bytes == 0);
873 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877STATIC int
878xfs_inactive_attrs(
879 xfs_inode_t *ip,
880 xfs_trans_t **tpp)
881{
882 xfs_trans_t *tp;
883 int error;
884 xfs_mount_t *mp;
885
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000886 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 tp = *tpp;
888 mp = ip->i_mount;
889 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +1000890 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +1000892 if (error)
893 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +1000896 if (error)
897 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
900 error = xfs_trans_reserve(tp, 0,
901 XFS_IFREE_LOG_RES(mp),
902 0, XFS_TRANS_PERM_LOG_RES,
903 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +1000904 if (error)
905 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000908 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
910
911 ASSERT(ip->i_d.di_anextents == 0);
912
913 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +0100914 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +1000915
916error_cancel:
917 ASSERT(XFS_FORCED_SHUTDOWN(mp));
918 xfs_trans_cancel(tp, 0);
919error_unlock:
920 *tpp = NULL;
921 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
922 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Christoph Hellwig993386c12007-08-28 16:12:30 +1000925int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926xfs_release(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000927 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Christoph Hellwig993386c12007-08-28 16:12:30 +1000929 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int error;
931
Christoph Hellwig42173f62008-04-22 17:33:25 +1000932 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000936 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938
David Chinner2a82b8b2007-07-11 11:09:12 +1000939 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000940 int truncated;
941
David Chinner2a82b8b2007-07-11 11:09:12 +1000942 /*
943 * If we are using filestreams, and we have an unlinked
944 * file that we are processing the last close on, then nothing
945 * will be able to reopen and write to this file. Purge this
946 * inode from the filestreams cache so that it doesn't delay
947 * teardown of the inode.
948 */
949 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
950 xfs_filestream_deassociate(ip);
951
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000952 /*
953 * If we previously truncated this file and removed old data
954 * in the process, we want to initiate "early" writeout on
955 * the last close. This is an attempt to combat the notorious
956 * NULL files problem which is particularly noticable from a
957 * truncate down, buffered (re-)write (delalloc), followed by
958 * a crash. What we are effectively doing here is
959 * significantly reducing the time window where we'd otherwise
960 * be exposed to that problem.
961 */
Christoph Hellwig09262b432007-08-29 11:44:50 +1000962 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +1000963 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000964 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000965 }
966
Dave Chinner6e857562010-12-23 12:02:31 +1100967 if (ip->i_d.di_nlink == 0)
968 return 0;
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000969
Dave Chinner6e857562010-12-23 12:02:31 +1100970 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
971 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
972 ip->i_delayed_blks > 0)) &&
973 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
974 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
975
976 /*
977 * If we can't get the iolock just skip truncating the blocks
978 * past EOF because we could deadlock with the mmap_sem
979 * otherwise. We'll get another chance to drop them once the
980 * last reference to the inode is dropped, so we'll never leak
981 * blocks permanently.
982 *
983 * Further, check if the inode is being opened, written and
984 * closed frequently and we have delayed allocation blocks
985 * oustanding (e.g. streaming writes from the NFS server),
986 * truncating the blocks past EOF will cause fragmentation to
987 * occur.
988 *
989 * In this case don't do the truncation, either, but we have to
990 * be careful how we detect this case. Blocks beyond EOF show
991 * up as i_delayed_blks even when the inode is clean, so we
992 * need to truncate them away first before checking for a dirty
993 * release. Hence on the first dirty close we will still remove
994 * the speculative allocation, but after that we will leave it
995 * in place.
996 */
997 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
998 return 0;
999
1000 error = xfs_free_eofblocks(mp, ip,
1001 XFS_FREE_EOF_TRYLOCK);
1002 if (error)
1003 return error;
1004
1005 /* delalloc blocks after truncation means it really is dirty */
1006 if (ip->i_delayed_blks)
1007 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010}
1011
1012/*
1013 * xfs_inactive
1014 *
1015 * This is called when the vnode reference count for the vnode
1016 * goes to zero. If the file has been unlinked, then it must
1017 * now be truncated. Also, we clear all of the read-ahead state
1018 * kept for the inode here since the file is now closed.
1019 */
Christoph Hellwig993386c12007-08-28 16:12:30 +10001020int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021xfs_inactive(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001022 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001024 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 xfs_fsblock_t first_block;
1026 int committed;
1027 xfs_trans_t *tp;
1028 xfs_mount_t *mp;
1029 int error;
1030 int truncate;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /*
1033 * If the inode is already free, then there can be nothing
1034 * to clean up here.
1035 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +01001036 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 ASSERT(ip->i_df.if_real_bytes == 0);
1038 ASSERT(ip->i_df.if_broot_bytes == 0);
1039 return VN_INACTIVE_CACHE;
1040 }
1041
1042 /*
1043 * Only do a truncate if it's a regular file with
1044 * some actual space in it. It's OK to look at the
1045 * inode's fields without the lock because we're the
1046 * only one with a reference to the inode.
1047 */
1048 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001049 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1050 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1052
1053 mp = ip->i_mount;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 error = 0;
1056
1057 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001058 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 goto out;
1060
1061 if (ip->i_d.di_nlink != 0) {
1062 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001063 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001064 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001065 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1066 (!(ip->i_d.di_flags &
1067 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1068 (ip->i_delayed_blks != 0)))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +00001069 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001070 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001071 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 goto out;
1074 }
1075
1076 ASSERT(ip->i_d.di_nlink == 0);
1077
Christoph Hellwig7d095252009-06-08 15:33:32 +02001078 error = xfs_qm_dqattach(ip, 0);
1079 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001080 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1083 if (truncate) {
1084 /*
1085 * Do the xfs_itruncate_start() call before
1086 * reserving any log space because itruncate_start
1087 * will call into the buffer cache and we can't
1088 * do that within a transaction.
1089 */
1090 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1091
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001092 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1093 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001094 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001095 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1096 return VN_INACTIVE_CACHE;
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 error = xfs_trans_reserve(tp, 0,
1100 XFS_ITRUNCATE_LOG_RES(mp),
1101 0, XFS_TRANS_PERM_LOG_RES,
1102 XFS_ITRUNCATE_LOG_COUNT);
1103 if (error) {
1104 /* Don't call itruncate_cleanup */
1105 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1106 xfs_trans_cancel(tp, 0);
1107 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001108 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
1111 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001112 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /*
1115 * normally, we have to run xfs_itruncate_finish sync.
1116 * But if filesystem is wsync and we're in the inactive
1117 * path, then we know that nlink == 0, and that the
1118 * xaction that made nlink == 0 is permanently committed
1119 * since xfs_remove runs as a synchronous transaction.
1120 */
1121 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1122 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1123
1124 if (error) {
1125 xfs_trans_cancel(tp,
1126 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1127 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001128 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1131
1132 /*
1133 * If we get an error while cleaning up a
1134 * symlink we bail out.
1135 */
1136 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1137 xfs_inactive_symlink_rmt(ip, &tp) :
1138 xfs_inactive_symlink_local(ip, &tp);
1139
1140 if (error) {
1141 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001142 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Christoph Hellwig898621d2010-06-24 11:36:58 +10001145 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 } else {
1147 error = xfs_trans_reserve(tp, 0,
1148 XFS_IFREE_LOG_RES(mp),
1149 0, XFS_TRANS_PERM_LOG_RES,
1150 XFS_INACTIVE_LOG_COUNT);
1151 if (error) {
1152 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1153 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001154 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001158 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
1161 /*
1162 * If there are attributes associated with the file
1163 * then blow them away now. The code calls a routine
1164 * that recursively deconstructs the attribute fork.
1165 * We need to just commit the current transaction
1166 * because we can't use it for xfs_attr_inactive().
1167 */
1168 if (ip->i_d.di_anextents > 0) {
1169 error = xfs_inactive_attrs(ip, &tp);
1170 /*
1171 * If we got an error, the transaction is already
1172 * cancelled, and the inode is unlocked. Just get out.
1173 */
1174 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001175 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 } else if (ip->i_afp) {
1177 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1178 }
1179
1180 /*
1181 * Free the inode.
1182 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001183 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 error = xfs_ifree(tp, ip, &free_list);
1185 if (error) {
1186 /*
1187 * If we fail to free the inode, shut down. The cancel
1188 * might do that, we need to make sure. Otherwise the
1189 * inode might be lost for a long time or forever.
1190 */
1191 if (!XFS_FORCED_SHUTDOWN(mp)) {
1192 cmn_err(CE_NOTE,
1193 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1194 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001195 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1198 } else {
1199 /*
1200 * Credit the quota account(s). The inode is gone.
1201 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001202 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /*
David Chinner78e9da72008-04-10 12:24:17 +10001205 * Just ignore errors at this point. There is nothing we can
1206 * do except to try to keep going. Make sure it's not a silent
1207 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
David Chinner78e9da72008-04-10 12:24:17 +10001209 error = xfs_bmap_finish(&tp, &free_list, &committed);
1210 if (error)
1211 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1212 "xfs_bmap_finish() returned error %d", error);
1213 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1214 if (error)
1215 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1216 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
Christoph Hellwig7d095252009-06-08 15:33:32 +02001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /*
1220 * Release the dquots held by inode, if any.
1221 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001222 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1224
1225 out:
1226 return VN_INACTIVE_CACHE;
1227}
1228
Barry Naujok384f3ce2008-05-21 16:58:22 +10001229/*
1230 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1231 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1232 * ci_name->name will point to a the actual name (caller must free) or
1233 * will be set to NULL if an exact match is found.
1234 */
Christoph Hellwig993386c12007-08-28 16:12:30 +10001235int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236xfs_lookup(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001237 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001238 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001239 xfs_inode_t **ipp,
1240 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001242 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int error;
1244 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001246 trace_xfs_lookup(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1249 return XFS_ERROR(EIO);
1250
1251 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001252 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001254
1255 if (error)
1256 goto out;
1257
Dave Chinner7b6259e2010-06-24 11:35:17 +10001258 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001259 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001260 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001261
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001262 return 0;
1263
Barry Naujok384f3ce2008-05-21 16:58:22 +10001264out_free_name:
1265 if (ci_name)
1266 kmem_free(ci_name->name);
1267out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001268 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return error;
1270}
1271
Christoph Hellwig993386c12007-08-28 16:12:30 +10001272int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273xfs_create(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001274 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001275 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001276 mode_t mode,
1277 xfs_dev_t rdev,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001278 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001280 int is_dir = S_ISDIR(mode);
1281 struct xfs_mount *mp = dp->i_mount;
1282 struct xfs_inode *ip = NULL;
1283 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +10001284 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 xfs_bmap_free_t free_list;
1286 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +10001287 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 uint cancel_flags;
1289 int committed;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001290 prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001291 struct xfs_dquot *udqp = NULL;
1292 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001294 uint log_res;
1295 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001297 trace_xfs_create(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001299 if (XFS_FORCED_SHUTDOWN(mp))
1300 return XFS_ERROR(EIO);
1301
Nathan Scott365ca832005-06-21 15:39:12 +10001302 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001303 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001305 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /*
1308 * Make sure that we have allocated dquot(s) on disk.
1309 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001310 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001311 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (error)
1313 goto std_return;
1314
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001315 if (is_dir) {
1316 rdev = 0;
1317 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1318 log_res = XFS_MKDIR_LOG_RES(mp);
1319 log_count = XFS_MKDIR_LOG_COUNT;
1320 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1321 } else {
1322 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1323 log_res = XFS_CREATE_LOG_RES(mp);
1324 log_count = XFS_CREATE_LOG_COUNT;
1325 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /*
1331 * Initially assume that the file does not exist and
1332 * reserve the resources for that case. If that is not
1333 * the case we'll drop the one we have and get a more
1334 * appropriate transaction later.
1335 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001336 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1337 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +02001339 /* flush outstanding delalloc blocks and retry */
1340 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -05001341 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1342 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +02001343 }
1344 if (error == ENOSPC) {
1345 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001347 error = xfs_trans_reserve(tp, 0, log_res, 0,
1348 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 if (error) {
1351 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001352 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001355 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001356 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001358 /*
1359 * Check for directory link count overflow.
1360 */
1361 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1362 error = XFS_ERROR(EMLINK);
1363 goto out_trans_cancel;
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001366 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 /*
1369 * Reserve disk quota and the inode.
1370 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001371 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001373 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Barry Naujok556b8b12008-04-10 12:22:07 +10001375 error = xfs_dir_canenter(tp, dp, name, resblks);
1376 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001377 goto out_trans_cancel;
1378
1379 /*
1380 * A newly created regular or special file just has one directory
1381 * entry pointing to them, but a directory also the "." entry
1382 * pointing to itself.
1383 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001384 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001385 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (error) {
1387 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001388 goto out_trans_cancel;
1389 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
1393 * At this point, we've gotten a newly allocated inode.
1394 * It is locked (and joined to the transaction).
1395 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001396 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
Christoph Hellwig993386c12007-08-28 16:12:30 +10001399 * Now we join the directory inode to the transaction. We do not do it
1400 * earlier because xfs_dir_ialloc might commit the previous transaction
1401 * (and release all the locks). An error from here on will result in
1402 * the transaction cancel unlocking dp so don't do it explicitly in the
1403 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001405 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001406 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Barry Naujok556b8b12008-04-10 12:22:07 +10001408 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001409 &first_block, &free_list, resblks ?
1410 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (error) {
1412 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001413 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001415 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1417
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001418 if (is_dir) {
1419 error = xfs_dir_init(tp, ip, dp);
1420 if (error)
1421 goto out_bmap_cancel;
1422
1423 error = xfs_bumplink(tp, dp);
1424 if (error)
1425 goto out_bmap_cancel;
1426 }
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /*
1429 * If this is a synchronous mount, make sure that the
1430 * create transaction goes to disk before returning to
1431 * the user.
1432 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001433 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /*
1437 * Attach the dquot(s) to the inodes and modify them incore.
1438 * These ids of the inode couldn't have changed since the new
1439 * inode has been locked ever since it was created.
1440 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001441 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /*
1444 * xfs_trans_commit normally decrements the vnode ref count
1445 * when it unlocks the inode. Since we want to return the
1446 * vnode to the caller, we bump the vnode ref count now.
1447 */
1448 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001450 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001451 if (error)
1452 goto out_abort_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001454 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (error) {
1456 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001457 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
Christoph Hellwig7d095252009-06-08 15:33:32 +02001460 xfs_qm_dqrele(udqp);
1461 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001463 *ipp = ip;
Christoph Hellwig288699f2010-06-23 18:11:15 +10001464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001466 out_bmap_cancel:
1467 xfs_bmap_cancel(&free_list);
1468 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001470 out_trans_cancel:
1471 xfs_trans_cancel(tp, cancel_flags);
1472 out_dqrele:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001473 xfs_qm_dqrele(udqp);
1474 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Christoph Hellwig993386c12007-08-28 16:12:30 +10001476 if (unlock_dp_on_error)
1477 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001478 std_return:
1479 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001481 out_abort_rele:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /*
1483 * Wait until after the current transaction is aborted to
1484 * release the inode. This prevents recursive transactions
1485 * and deadlocks from xfs_inactive.
1486 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001487 xfs_bmap_cancel(&free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 cancel_flags |= XFS_TRANS_ABORT;
1489 xfs_trans_cancel(tp, cancel_flags);
1490 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001491 unlock_dp_on_error = B_FALSE;
1492 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496int xfs_locked_n;
1497int xfs_small_retries;
1498int xfs_middle_retries;
1499int xfs_lots_retries;
1500int xfs_lock_delays;
1501#endif
1502
1503/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001504 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1505 * a different value
1506 */
1507static inline int
1508xfs_lock_inumorder(int lock_mode, int subclass)
1509{
1510 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001511 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001512 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001513 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001514
1515 return lock_mode;
1516}
1517
1518/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 * The following routine will lock n inodes in exclusive mode.
1520 * We assume the caller calls us with the inodes in i_ino order.
1521 *
1522 * We need to detect deadlock where an inode that we lock
1523 * is in the AIL and we start waiting for another inode that is locked
1524 * by a thread in a long running transaction (such as truncate). This can
1525 * result in deadlock since the long running trans might need to wait
1526 * for the inode we just locked in order to push the tail and free space
1527 * in the log.
1528 */
1529void
1530xfs_lock_inodes(
1531 xfs_inode_t **ips,
1532 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 uint lock_mode)
1534{
1535 int attempts = 0, i, j, try_lock;
1536 xfs_log_item_t *lp;
1537
1538 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1539
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001540 try_lock = 0;
1541 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543again:
1544 for (; i < inodes; i++) {
1545 ASSERT(ips[i]);
1546
1547 if (i && (ips[i] == ips[i-1])) /* Already locked */
1548 continue;
1549
1550 /*
1551 * If try_lock is not set yet, make sure all locked inodes
1552 * are not in the AIL.
1553 * If any are, set try_lock to be used later.
1554 */
1555
1556 if (!try_lock) {
1557 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1558 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1559 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1560 try_lock++;
1561 }
1562 }
1563 }
1564
1565 /*
1566 * If any of the previous locks we have locked is in the AIL,
1567 * we must TRY to get the second and subsequent locks. If
1568 * we can't get any, we must release all we have
1569 * and try again.
1570 */
1571
1572 if (try_lock) {
1573 /* try_lock must be 0 if i is 0. */
1574 /*
1575 * try_lock means we have an inode locked
1576 * that is in the AIL.
1577 */
1578 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001579 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 attempts++;
1581
1582 /*
1583 * Unlock all previous guys and try again.
1584 * xfs_iunlock will try to push the tail
1585 * if the inode is in the AIL.
1586 */
1587
1588 for(j = i - 1; j >= 0; j--) {
1589
1590 /*
1591 * Check to see if we've already
1592 * unlocked this one.
1593 * Not the first one going back,
1594 * and the inode ptr is the same.
1595 */
1596 if ((j != (i - 1)) && ips[j] ==
1597 ips[j+1])
1598 continue;
1599
1600 xfs_iunlock(ips[j], lock_mode);
1601 }
1602
1603 if ((attempts % 5) == 0) {
1604 delay(1); /* Don't just spin the CPU */
1605#ifdef DEBUG
1606 xfs_lock_delays++;
1607#endif
1608 }
1609 i = 0;
1610 try_lock = 0;
1611 goto again;
1612 }
1613 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001614 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616 }
1617
1618#ifdef DEBUG
1619 if (attempts) {
1620 if (attempts < 5) xfs_small_retries++;
1621 else if (attempts < 100) xfs_middle_retries++;
1622 else xfs_lots_retries++;
1623 } else {
1624 xfs_locked_n++;
1625 }
1626#endif
1627}
1628
David Chinnerf9114eb2008-09-17 16:51:21 +10001629/*
1630 * xfs_lock_two_inodes() can only be used to lock one type of lock
1631 * at a time - the iolock or the ilock, but not both at once. If
1632 * we lock both at once, lockdep will report false positives saying
1633 * we have violated locking orders.
1634 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001635void
1636xfs_lock_two_inodes(
1637 xfs_inode_t *ip0,
1638 xfs_inode_t *ip1,
1639 uint lock_mode)
1640{
1641 xfs_inode_t *temp;
1642 int attempts = 0;
1643 xfs_log_item_t *lp;
1644
David Chinnerf9114eb2008-09-17 16:51:21 +10001645 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1646 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001647 ASSERT(ip0->i_ino != ip1->i_ino);
1648
1649 if (ip0->i_ino > ip1->i_ino) {
1650 temp = ip0;
1651 ip0 = ip1;
1652 ip1 = temp;
1653 }
1654
1655 again:
1656 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1657
1658 /*
1659 * If the first lock we have locked is in the AIL, we must TRY to get
1660 * the second lock. If we can't get it, we must release the first one
1661 * and try again.
1662 */
1663 lp = (xfs_log_item_t *)ip0->i_itemp;
1664 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1665 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1666 xfs_iunlock(ip0, lock_mode);
1667 if ((++attempts % 5) == 0)
1668 delay(1); /* Don't just spin the CPU */
1669 goto again;
1670 }
1671 } else {
1672 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1673 }
1674}
1675
Christoph Hellwig993386c12007-08-28 16:12:30 +10001676int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677xfs_remove(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001678 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001679 struct xfs_name *name,
1680 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001682 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001684 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 int error = 0;
1686 xfs_bmap_free_t free_list;
1687 xfs_fsblock_t first_block;
1688 int cancel_flags;
1689 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int link_zero;
1691 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001692 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001694 trace_xfs_remove(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (XFS_FORCED_SHUTDOWN(mp))
1697 return XFS_ERROR(EIO);
1698
Christoph Hellwig7d095252009-06-08 15:33:32 +02001699 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001700 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Christoph Hellwig7d095252009-06-08 15:33:32 +02001703 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001704 if (error)
1705 goto std_return;
1706
1707 if (is_dir) {
1708 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1709 log_count = XFS_DEFAULT_LOG_COUNT;
1710 } else {
1711 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1712 log_count = XFS_REMOVE_LOG_COUNT;
1713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * We try to get the real space reservation first,
1718 * allowing for directory btree deletion(s) implying
1719 * possible bmap insert(s). If we can't get the space
1720 * reservation then we use 0 instead, and avoid the bmap
1721 * btree insert(s) in the directory code by, if the bmap
1722 * insert tries to happen, instead trimming the LAST
1723 * block from the directory.
1724 */
1725 resblks = XFS_REMOVE_SPACE_RES(mp);
1726 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001727 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (error == ENOSPC) {
1729 resblks = 0;
1730 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001731 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733 if (error) {
1734 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001735 cancel_flags = 0;
1736 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001739 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Christoph Hellwig898621d2010-06-24 11:36:58 +10001741 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1742 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001745 * If we're removing a directory perform some additional validation.
1746 */
1747 if (is_dir) {
1748 ASSERT(ip->i_d.di_nlink >= 2);
1749 if (ip->i_d.di_nlink != 2) {
1750 error = XFS_ERROR(ENOTEMPTY);
1751 goto out_trans_cancel;
1752 }
1753 if (!xfs_dir_isempty(ip)) {
1754 error = XFS_ERROR(ENOTEMPTY);
1755 goto out_trans_cancel;
1756 }
1757 }
1758
Eric Sandeen9d87c312009-01-14 23:22:07 -06001759 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001760 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001761 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (error) {
1763 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001764 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001766 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001768 if (is_dir) {
1769 /*
1770 * Drop the link from ip's "..".
1771 */
1772 error = xfs_droplink(tp, dp);
1773 if (error)
1774 goto out_bmap_cancel;
1775
1776 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001777 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001778 */
1779 error = xfs_droplink(tp, ip);
1780 if (error)
1781 goto out_bmap_cancel;
1782 } else {
1783 /*
1784 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001785 * inode here. For a directory this is done implicitly
1786 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001787 */
1788 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001791 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001792 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001793 */
1794 error = xfs_droplink(tp, ip);
1795 if (error)
1796 goto out_bmap_cancel;
1797
1798 /*
1799 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * we are in the transaction.
1801 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001802 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 * If this is a synchronous mount, make sure that the
1806 * remove transaction goes to disk before returning to
1807 * the user.
1808 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001809 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001812 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001813 if (error)
1814 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001816 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001817 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001821 * If we are using filestreams, kill the stream association.
1822 * If the file is still open it may get a new one but that
1823 * will get killed on last close in xfs_close() so we don't
1824 * have to worry about that.
1825 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001826 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001827 xfs_filestream_deassociate(ip);
1828
Christoph Hellwig288699f2010-06-23 18:11:15 +10001829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001831 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 xfs_bmap_cancel(&free_list);
1833 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001834 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001836 std_return:
1837 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Christoph Hellwig993386c12007-08-28 16:12:30 +10001840int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841xfs_link(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001842 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001843 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10001844 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001846 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 int error;
1849 xfs_bmap_free_t free_list;
1850 xfs_fsblock_t first_block;
1851 int cancel_flags;
1852 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001855 trace_xfs_link(tdp, target_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001857 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (XFS_FORCED_SHUTDOWN(mp))
1860 return XFS_ERROR(EIO);
1861
Christoph Hellwig7d095252009-06-08 15:33:32 +02001862 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01001863 if (error)
1864 goto std_return;
1865
Christoph Hellwig7d095252009-06-08 15:33:32 +02001866 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (error)
1868 goto std_return;
1869
1870 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1871 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001872 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1874 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1875 if (error == ENOSPC) {
1876 resblks = 0;
1877 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1878 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1879 }
1880 if (error) {
1881 cancel_flags = 0;
1882 goto error_return;
1883 }
1884
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001885 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Christoph Hellwig898621d2010-06-24 11:36:58 +10001887 xfs_trans_ijoin_ref(tp, sip, XFS_ILOCK_EXCL);
1888 xfs_trans_ijoin_ref(tp, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /*
1891 * If the source has too many links, we can't make any more to it.
1892 */
1893 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1894 error = XFS_ERROR(EMLINK);
1895 goto error_return;
1896 }
1897
Nathan Scott365ca832005-06-21 15:39:12 +10001898 /*
1899 * If we are using project inheritance, we only allow hard link
1900 * creation in our tree when the project IDs are the same; else
1901 * the tree quota mechanism could be circumvented.
1902 */
1903 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001904 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10001905 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10001906 goto error_return;
1907 }
1908
Barry Naujok556b8b12008-04-10 12:22:07 +10001909 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1910 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto error_return;
1912
Eric Sandeen9d87c312009-01-14 23:22:07 -06001913 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Barry Naujok556b8b12008-04-10 12:22:07 +10001915 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1916 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (error)
1918 goto abort_return;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001919 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1921
1922 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001923 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /*
1927 * If this is a synchronous mount, make sure that the
1928 * link transaction goes to disk before returning to
1929 * the user.
1930 */
1931 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1932 xfs_trans_set_sync(tp);
1933 }
1934
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001935 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (error) {
1937 xfs_bmap_cancel(&free_list);
1938 goto abort_return;
1939 }
1940
Christoph Hellwig288699f2010-06-23 18:11:15 +10001941 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 abort_return:
1944 cancel_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 error_return:
1946 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001947 std_return:
1948 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001950
Christoph Hellwig993386c12007-08-28 16:12:30 +10001951int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952xfs_symlink(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001953 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001954 struct xfs_name *link_name,
1955 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001956 mode_t mode,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001957 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001959 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 xfs_inode_t *ip;
1962 int error;
1963 int pathlen;
1964 xfs_bmap_free_t free_list;
1965 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +10001966 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 uint cancel_flags;
1968 int committed;
1969 xfs_fileoff_t first_fsb;
1970 xfs_filblks_t fs_blocks;
1971 int nmaps;
1972 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1973 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10001974 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int byte_cnt;
1976 int n;
1977 xfs_buf_t *bp;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001978 prid_t prid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 struct xfs_dquot *udqp, *gdqp;
1980 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Christoph Hellwig3937be52008-03-06 13:46:19 +11001982 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 error = 0;
1984 ip = NULL;
1985 tp = NULL;
1986
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001987 trace_xfs_symlink(dp, link_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 if (XFS_FORCED_SHUTDOWN(mp))
1990 return XFS_ERROR(EIO);
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /*
1993 * Check component lengths of the target path name.
1994 */
1995 pathlen = strlen(target_path);
1996 if (pathlen >= MAXPATHLEN) /* total string too long */
1997 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002000 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002001 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002003 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 /*
2006 * Make sure that we have allocated dquot(s) on disk.
2007 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002008 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2010 if (error)
2011 goto std_return;
2012
2013 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2014 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2015 /*
2016 * The symlink will fit into the inode data fork?
2017 * There can't be any attributes so we get the whole variable part.
2018 */
2019 if (pathlen <= XFS_LITINO(mp))
2020 fs_blocks = 0;
2021 else
2022 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002023 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2025 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2026 if (error == ENOSPC && fs_blocks == 0) {
2027 resblks = 0;
2028 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2029 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2030 }
2031 if (error) {
2032 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 goto error_return;
2034 }
2035
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002036 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002037 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /*
2040 * Check whether the directory allows new symlinks or not.
2041 */
2042 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2043 error = XFS_ERROR(EPERM);
2044 goto error_return;
2045 }
2046
2047 /*
2048 * Reserve disk quota : blocks and inode.
2049 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002050 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (error)
2052 goto error_return;
2053
2054 /*
2055 * Check for ability to enter directory entry, if no space reserved.
2056 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002057 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2058 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 goto error_return;
2060 /*
2061 * Initialize the bmap freelist prior to calling either
2062 * bmapi or the directory create code.
2063 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002064 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 /*
2067 * Allocate an inode for the symlink.
2068 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00002069 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
2070 prid, resblks > 0, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (error) {
2072 if (error == ENOSPC)
2073 goto error_return;
2074 goto error1;
2075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Christoph Hellwig993386c12007-08-28 16:12:30 +10002077 /*
2078 * An error after we've joined dp to the transaction will result in the
2079 * transaction cancel unlocking dp so don't do it explicitly in the
2080 * error path.
2081 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10002082 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002083 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 /*
2086 * Also attach the dquot(s) to it, if applicable.
2087 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002088 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 if (resblks)
2091 resblks -= XFS_IALLOC_SPACE_RES(mp);
2092 /*
2093 * If the symlink will fit into the inode, write it inline.
2094 */
2095 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2096 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2097 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2098 ip->i_d.di_size = pathlen;
2099
2100 /*
2101 * The inode was initially created in extent format.
2102 */
2103 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2104 ip->i_df.if_flags |= XFS_IFINLINE;
2105
2106 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2107 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2108
2109 } else {
2110 first_fsb = 0;
2111 nmaps = SYMLINK_MAPS;
2112
2113 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2114 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2115 &first_block, resblks, mval, &nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002116 &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (error) {
2118 goto error1;
2119 }
2120
2121 if (resblks)
2122 resblks -= fs_blocks;
2123 ip->i_d.di_size = pathlen;
2124 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2125
2126 cur_chunk = target_path;
2127 for (n = 0; n < nmaps; n++) {
2128 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2129 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2130 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2131 BTOBB(byte_cnt), 0);
2132 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2133 if (pathlen < byte_cnt) {
2134 byte_cnt = pathlen;
2135 }
2136 pathlen -= byte_cnt;
2137
2138 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2139 cur_chunk += byte_cnt;
2140
2141 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2142 }
2143 }
2144
2145 /*
2146 * Create the directory entry for the symlink.
2147 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002148 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2149 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002150 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 goto error1;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10002152 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2154
2155 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 * If this is a synchronous mount, make sure that the
2157 * symlink transaction goes to disk before returning to
2158 * the user.
2159 */
2160 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2161 xfs_trans_set_sync(tp);
2162 }
2163
2164 /*
2165 * xfs_trans_commit normally decrements the vnode ref count
2166 * when it unlocks the inode. Since we want to return the
2167 * vnode to the caller, we bump the vnode ref count now.
2168 */
2169 IHOLD(ip);
2170
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002171 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (error) {
2173 goto error2;
2174 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002175 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002176 xfs_qm_dqrele(udqp);
2177 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Christoph Hellwig288699f2010-06-23 18:11:15 +10002179 *ipp = ip;
2180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 error2:
2183 IRELE(ip);
2184 error1:
2185 xfs_bmap_cancel(&free_list);
2186 cancel_flags |= XFS_TRANS_ABORT;
2187 error_return:
2188 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002189 xfs_qm_dqrele(udqp);
2190 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Christoph Hellwig993386c12007-08-28 16:12:30 +10002192 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002194 std_return:
2195 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198int
Christoph Hellwig993386c12007-08-28 16:12:30 +10002199xfs_set_dmattrs(
2200 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 u_int evmask,
Christoph Hellwig993386c12007-08-28 16:12:30 +10002202 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002204 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int error;
2207
2208 if (!capable(CAP_SYS_ADMIN))
2209 return XFS_ERROR(EPERM);
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (XFS_FORCED_SHUTDOWN(mp))
2212 return XFS_ERROR(EIO);
2213
2214 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2215 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2216 if (error) {
2217 xfs_trans_cancel(tp, 0);
2218 return error;
2219 }
2220 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10002221 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Christoph Hellwig613d7042007-10-11 17:44:08 +10002223 ip->i_d.di_dmevmask = evmask;
2224 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002227 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 return error;
2230}
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232/*
2233 * xfs_alloc_file_space()
2234 * This routine allocates disk space for the given file.
2235 *
2236 * If alloc_type == 0, this request is for an ALLOCSP type
2237 * request which will change the file size. In this case, no
2238 * DMAPI event will be generated by the call. A TRUNCATE event
2239 * will be generated later by xfs_setattr.
2240 *
2241 * If alloc_type != 0, this request is for a RESVSP type
2242 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2243 * lower block boundary byte address is less than the file's
2244 * length.
2245 *
2246 * RETURNS:
2247 * 0 on success
2248 * errno on error
2249 *
2250 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002251STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252xfs_alloc_file_space(
2253 xfs_inode_t *ip,
2254 xfs_off_t offset,
2255 xfs_off_t len,
2256 int alloc_type,
2257 int attr_flags)
2258{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002259 xfs_mount_t *mp = ip->i_mount;
2260 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 xfs_filblks_t allocated_fsb;
2262 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002263 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002265 xfs_fsblock_t firstfsb;
2266 int nimaps;
2267 int bmapi_flag;
2268 int quota_flag;
2269 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002271 xfs_bmbt_irec_t imaps[1], *imapp;
2272 xfs_bmap_free_t free_list;
2273 uint qblocks, resblks, resrtextents;
2274 int committed;
2275 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002277 trace_xfs_alloc_file_space(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 if (XFS_FORCED_SHUTDOWN(mp))
2280 return XFS_ERROR(EIO);
2281
Christoph Hellwig7d095252009-06-08 15:33:32 +02002282 error = xfs_qm_dqattach(ip, 0);
2283 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return error;
2285
2286 if (len <= 0)
2287 return XFS_ERROR(EINVAL);
2288
David Chinner957d0eb2007-06-18 16:50:37 +10002289 rt = XFS_IS_REALTIME_INODE(ip);
2290 extsz = xfs_get_extsz_hint(ip);
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002294 nimaps = 1;
Dave Chinner44722352010-08-24 12:02:11 +10002295 bmapi_flag = XFS_BMAPI_WRITE | alloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2297 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002300 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002303 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Nathan Scottdd9f4382006-01-11 15:28:28 +11002305 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002306 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002307 */
2308 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002310 do_div(s, extsz);
2311 s *= extsz;
2312 e = startoffset_fsb + allocatesize_fsb;
2313 if ((temp = do_mod(startoffset_fsb, extsz)))
2314 e += temp;
2315 if ((temp = do_mod(e, extsz)))
2316 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002318 s = 0;
2319 e = allocatesize_fsb;
2320 }
2321
Dave Chinner72656c42010-09-03 12:19:33 +10002322 /*
2323 * The transaction reservation is limited to a 32-bit block
2324 * count, hence we need to limit the number of blocks we are
2325 * trying to reserve to avoid an overflow. We can't allocate
2326 * more than @nimaps extents, and an extent is limited on disk
2327 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
2328 */
2329 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
Nathan Scottdd9f4382006-01-11 15:28:28 +11002330 if (unlikely(rt)) {
Dave Chinner72656c42010-09-03 12:19:33 +10002331 resrtextents = qblocks = resblks;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002332 resrtextents /= mp->m_sb.sb_rextsize;
2333 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2334 quota_flag = XFS_QMOPT_RES_RTBLKS;
2335 } else {
2336 resrtextents = 0;
Dave Chinner72656c42010-09-03 12:19:33 +10002337 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002338 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340
2341 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002342 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 */
2344 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002345 error = xfs_trans_reserve(tp, resblks,
2346 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 XFS_TRANS_PERM_LOG_RES,
2348 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002350 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 */
2352 if (error) {
2353 /*
2354 * Free the transaction structure.
2355 */
2356 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2357 xfs_trans_cancel(tp, 0);
2358 break;
2359 }
2360 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002361 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2362 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (error)
2364 goto error1;
2365
Christoph Hellwig898621d2010-06-24 11:36:58 +10002366 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002369 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002371 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002372 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002373 allocatesize_fsb, bmapi_flag,
2374 &firstfsb, 0, imapp, &nimaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002375 &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (error) {
2377 goto error0;
2378 }
2379
2380 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002381 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002383 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (error) {
2385 goto error0;
2386 }
2387
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002388 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2390 if (error) {
2391 break;
2392 }
2393
2394 allocated_fsb = imapp->br_blockcount;
2395
Nathan Scottdd9f4382006-01-11 15:28:28 +11002396 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 error = XFS_ERROR(ENOSPC);
2398 break;
2399 }
2400
2401 startoffset_fsb += allocated_fsb;
2402 allocatesize_fsb -= allocated_fsb;
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 return error;
2406
Nathan Scottdd9f4382006-01-11 15:28:28 +11002407error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002409 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002410
2411error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2413 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002414 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
2417/*
2418 * Zero file bytes between startoff and endoff inclusive.
2419 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002420 *
2421 * This function is used by xfs_free_file_space() to zero
2422 * partial blocks when the range to free is not block aligned.
2423 * When unreserving space with boundaries that are not block
2424 * aligned we round up the start and round down the end
2425 * boundaries and then use this function to zero the parts of
2426 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 */
2428STATIC int
2429xfs_zero_remaining_bytes(
2430 xfs_inode_t *ip,
2431 xfs_off_t startoff,
2432 xfs_off_t endoff)
2433{
2434 xfs_bmbt_irec_t imap;
2435 xfs_fileoff_t offset_fsb;
2436 xfs_off_t lastoffset;
2437 xfs_off_t offset;
2438 xfs_buf_t *bp;
2439 xfs_mount_t *mp = ip->i_mount;
2440 int nimap;
2441 int error = 0;
2442
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002443 /*
2444 * Avoid doing I/O beyond eof - it's not necessary
2445 * since nothing can read beyond eof. The space will
2446 * be zeroed when the file is extended anyway.
2447 */
2448 if (startoff >= ip->i_size)
2449 return 0;
2450
2451 if (endoff > ip->i_size)
2452 endoff = ip->i_size;
2453
Dave Chinner686865f2010-09-24 20:07:47 +10002454 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
2455 mp->m_rtdev_targp : mp->m_ddev_targp,
2456 mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11002457 if (!bp)
2458 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2461 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2462 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002463 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002464 NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (error || nimap < 1)
2466 break;
2467 ASSERT(imap.br_blockcount >= 1);
2468 ASSERT(imap.br_startoff == offset_fsb);
2469 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2470 if (lastoffset > endoff)
2471 lastoffset = endoff;
2472 if (imap.br_startblock == HOLESTARTBLOCK)
2473 continue;
2474 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2475 if (imap.br_state == XFS_EXT_UNWRITTEN)
2476 continue;
2477 XFS_BUF_UNDONE(bp);
2478 XFS_BUF_UNWRITE(bp);
2479 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002480 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002482 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002483 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2485 mp, bp, XFS_BUF_ADDR(bp));
2486 break;
2487 }
2488 memset(XFS_BUF_PTR(bp) +
2489 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2490 0, lastoffset - offset + 1);
2491 XFS_BUF_UNDONE(bp);
2492 XFS_BUF_UNREAD(bp);
2493 XFS_BUF_WRITE(bp);
2494 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002495 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002496 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2498 mp, bp, XFS_BUF_ADDR(bp));
2499 break;
2500 }
2501 }
2502 xfs_buf_free(bp);
2503 return error;
2504}
2505
2506/*
2507 * xfs_free_file_space()
2508 * This routine frees disk space for the given file.
2509 *
2510 * This routine is only called by xfs_change_file_space
2511 * for an UNRESVSP type call.
2512 *
2513 * RETURNS:
2514 * 0 on success
2515 * errno on error
2516 *
2517 */
2518STATIC int
2519xfs_free_file_space(
2520 xfs_inode_t *ip,
2521 xfs_off_t offset,
2522 xfs_off_t len,
2523 int attr_flags)
2524{
2525 int committed;
2526 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 xfs_fileoff_t endoffset_fsb;
2528 int error;
2529 xfs_fsblock_t firstfsb;
2530 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 xfs_bmbt_irec_t imap;
2532 xfs_off_t ioffset;
2533 xfs_extlen_t mod=0;
2534 xfs_mount_t *mp;
2535 int nimap;
2536 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002537 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 int rt;
2539 xfs_fileoff_t startoffset_fsb;
2540 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002541 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 mp = ip->i_mount;
2544
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002545 trace_xfs_free_file_space(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002546
Christoph Hellwig7d095252009-06-08 15:33:32 +02002547 error = xfs_qm_dqattach(ip, 0);
2548 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return error;
2550
2551 error = 0;
2552 if (len <= 0) /* if nothing being freed */
2553 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002554 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002556 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002558 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002559 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002560 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002562 /* wait for the completion of any pending DIOs */
2563 xfs_ioend_wait(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11002564 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002565
Tim Shimmine6a4b372007-11-23 16:30:42 +11002566 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002568
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002569 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002570 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002571 if (error)
2572 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002573 }
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 /*
2576 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002577 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2579 * will take care of it for us.
2580 */
Eric Sandeen62118702008-03-06 13:44:28 +11002581 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002583 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002584 1, 0, NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (error)
2586 goto out_unlock_iolock;
2587 ASSERT(nimap == 0 || nimap == 1);
2588 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2589 xfs_daddr_t block;
2590
2591 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2592 block = imap.br_startblock;
2593 mod = do_div(block, mp->m_sb.sb_rextsize);
2594 if (mod)
2595 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2596 }
2597 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002598 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002599 1, 0, NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (error)
2601 goto out_unlock_iolock;
2602 ASSERT(nimap == 0 || nimap == 1);
2603 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2604 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2605 mod++;
2606 if (mod && (mod != mp->m_sb.sb_rextsize))
2607 endoffset_fsb -= mod;
2608 }
2609 }
2610 if ((done = (endoffset_fsb <= startoffset_fsb)))
2611 /*
2612 * One contiguous piece to clear
2613 */
2614 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2615 else {
2616 /*
2617 * Some full blocks, possibly two pieces to clear
2618 */
2619 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2620 error = xfs_zero_remaining_bytes(ip, offset,
2621 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2622 if (!error &&
2623 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2624 error = xfs_zero_remaining_bytes(ip,
2625 XFS_FSB_TO_B(mp, endoffset_fsb),
2626 offset + len - 1);
2627 }
2628
2629 /*
2630 * free file space until done or until there is an error
2631 */
2632 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2633 while (!error && !done) {
2634
2635 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002636 * allocate and setup the transaction. Allow this
2637 * transaction to dip into the reserve blocks to ensure
2638 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 */
2640 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002641 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 error = xfs_trans_reserve(tp,
2643 resblks,
2644 XFS_WRITE_LOG_RES(mp),
2645 0,
2646 XFS_TRANS_PERM_LOG_RES,
2647 XFS_WRITE_LOG_COUNT);
2648
2649 /*
2650 * check for running out of space
2651 */
2652 if (error) {
2653 /*
2654 * Free the transaction structure.
2655 */
2656 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2657 xfs_trans_cancel(tp, 0);
2658 break;
2659 }
2660 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002661 error = xfs_trans_reserve_quota(tp, mp,
2662 ip->i_udquot, ip->i_gdquot,
2663 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (error)
2665 goto error1;
2666
Christoph Hellwig898621d2010-06-24 11:36:58 +10002667 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /*
2670 * issue the bunmapi() call to free the blocks
2671 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002672 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002673 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 endoffset_fsb - startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002675 0, 2, &firstfsb, &free_list, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (error) {
2677 goto error0;
2678 }
2679
2680 /*
2681 * complete the transaction
2682 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002683 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (error) {
2685 goto error0;
2686 }
2687
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002688 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2690 }
2691
2692 out_unlock_iolock:
2693 if (need_iolock)
2694 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2695 return error;
2696
2697 error0:
2698 xfs_bmap_cancel(&free_list);
2699 error1:
2700 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2701 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2702 XFS_ILOCK_EXCL);
2703 return error;
2704}
2705
2706/*
2707 * xfs_change_file_space()
2708 * This routine allocates or frees disk space for the given file.
2709 * The user specified parameters are checked for alignment and size
2710 * limitations.
2711 *
2712 * RETURNS:
2713 * 0 on success
2714 * errno on error
2715 *
2716 */
2717int
2718xfs_change_file_space(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002719 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 int cmd,
2721 xfs_flock64_t *bf,
2722 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 int attr_flags)
2724{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002725 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 int clrprealloc;
2727 int error;
2728 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 int setprealloc;
2730 xfs_off_t startoffset;
2731 xfs_off_t llen;
2732 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002733 struct iattr iattr;
Dave Chinner44722352010-08-24 12:02:11 +10002734 int prealloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Christoph Hellwig993386c12007-08-28 16:12:30 +10002736 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return XFS_ERROR(EINVAL);
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 switch (bf->l_whence) {
2740 case 0: /*SEEK_SET*/
2741 break;
2742 case 1: /*SEEK_CUR*/
2743 bf->l_start += offset;
2744 break;
2745 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002746 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 break;
2748 default:
2749 return XFS_ERROR(EINVAL);
2750 }
2751
2752 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2753
2754 if ( (bf->l_start < 0)
2755 || (bf->l_start > XFS_MAXIOFFSET(mp))
2756 || (bf->l_start + llen < 0)
2757 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2758 return XFS_ERROR(EINVAL);
2759
2760 bf->l_whence = 0;
2761
2762 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002763 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 /*
2766 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2767 * file space.
2768 * These calls do NOT zero the data space allocated to the file,
2769 * nor do they change the file size.
2770 *
2771 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2772 * space.
2773 * These calls cause the new file data to be zeroed and the file
2774 * size to be changed.
2775 */
2776 setprealloc = clrprealloc = 0;
Dave Chinner44722352010-08-24 12:02:11 +10002777 prealloc_type = XFS_BMAPI_PREALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 switch (cmd) {
Dave Chinner44722352010-08-24 12:02:11 +10002780 case XFS_IOC_ZERO_RANGE:
2781 prealloc_type |= XFS_BMAPI_CONVERT;
2782 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2783 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 case XFS_IOC_RESVSP:
2785 case XFS_IOC_RESVSP64:
2786 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
Dave Chinner44722352010-08-24 12:02:11 +10002787 prealloc_type, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (error)
2789 return error;
2790 setprealloc = 1;
2791 break;
2792
2793 case XFS_IOC_UNRESVSP:
2794 case XFS_IOC_UNRESVSP64:
2795 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2796 attr_flags)))
2797 return error;
2798 break;
2799
2800 case XFS_IOC_ALLOCSP:
2801 case XFS_IOC_ALLOCSP64:
2802 case XFS_IOC_FREESP:
2803 case XFS_IOC_FREESP64:
2804 if (startoffset > fsize) {
2805 error = xfs_alloc_file_space(ip, fsize,
2806 startoffset - fsize, 0, attr_flags);
2807 if (error)
2808 break;
2809 }
2810
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002811 iattr.ia_valid = ATTR_SIZE;
2812 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11002814 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816 if (error)
2817 return error;
2818
2819 clrprealloc = 1;
2820 break;
2821
2822 default:
2823 ASSERT(0);
2824 return XFS_ERROR(EINVAL);
2825 }
2826
2827 /*
2828 * update the inode timestamp, mode, and prealloc flag bits
2829 */
2830 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2831
2832 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2833 0, 0, 0))) {
2834 /* ASSERT(0); */
2835 xfs_trans_cancel(tp, 0);
2836 return error;
2837 }
2838
2839 xfs_ilock(ip, XFS_ILOCK_EXCL);
2840
Christoph Hellwig898621d2010-06-24 11:36:58 +10002841 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002843 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 ip->i_d.di_mode &= ~S_ISUID;
2845
2846 /*
2847 * Note that we don't have to worry about mandatory
2848 * file locking being disabled here because we only
2849 * clear the S_ISGID bit if the Group execute bit is
2850 * on, but if it was on then mandatory locking wouldn't
2851 * have been enabled.
2852 */
2853 if (ip->i_d.di_mode & S_IXGRP)
2854 ip->i_d.di_mode &= ~S_ISGID;
2855
Dave Chinnerdcd79a12010-09-28 12:27:25 +10002856 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
2858 if (setprealloc)
2859 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2860 else if (clrprealloc)
2861 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2862
2863 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2864 xfs_trans_set_sync(tp);
2865
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002866 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2869
2870 return error;
2871}