blob: 0d1caec873a13f237b84d79eb4cada61d386d1d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
32#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_dinode.h"
34#include "xfs_inode.h"
35#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_ialloc.h"
38#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_bmap.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020040#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_attr.h"
42#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_quota.h"
45#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110046#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100049#include "xfs_filestream.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100050#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000051#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
Nathan Scott7d4fb402006-06-09 15:27:16 +100054 * The maximum pathlen is 1024 bytes. Since the minimum file system
55 * blocksize is 512 bytes, we can get a max of 2 extents back from
56 * bmapi.
57 */
58#define SYMLINK_MAPS 2
59
Christoph Hellwig804c83c2007-08-28 13:59:03 +100060STATIC int
61xfs_readlink_bmap(
62 xfs_inode_t *ip,
63 char *link)
64{
65 xfs_mount_t *mp = ip->i_mount;
66 int pathlen = ip->i_d.di_size;
67 int nmaps = SYMLINK_MAPS;
68 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
69 xfs_daddr_t d;
70 int byte_cnt;
71 int n;
72 xfs_buf_t *bp;
73 int error = 0;
74
75 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +100076 mval, &nmaps, NULL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +100077 if (error)
78 goto out;
79
80 for (n = 0; n < nmaps; n++) {
81 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
82 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
83
Christoph Hellwig6ad112b2009-11-24 18:02:23 +000084 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
85 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
Chandra Seetharamanac4d6882011-08-03 02:18:29 +000086 if (!bp)
87 return XFS_ERROR(ENOMEM);
Chandra Seetharamane5702802011-08-03 02:18:34 +000088 error = bp->b_error;
Christoph Hellwig804c83c2007-08-28 13:59:03 +100089 if (error) {
90 xfs_ioerror_alert("xfs_readlink",
91 ip->i_mount, bp, XFS_BUF_ADDR(bp));
92 xfs_buf_relse(bp);
93 goto out;
94 }
95 if (pathlen < byte_cnt)
96 byte_cnt = pathlen;
97 pathlen -= byte_cnt;
98
Chandra Seetharaman62926042011-07-22 23:40:15 +000099 memcpy(link, bp->b_addr, byte_cnt);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000100 xfs_buf_relse(bp);
101 }
102
103 link[ip->i_d.di_size] = '\0';
104 error = 0;
105
106 out:
107 return error;
108}
109
Christoph Hellwig993386c2007-08-28 16:12:30 +1000110int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000112 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000113 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000115 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000119 trace_xfs_readlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (XFS_FORCED_SHUTDOWN(mp))
122 return XFS_ERROR(EIO);
123
124 xfs_ilock(ip, XFS_ILOCK_SHARED);
125
Al Viroabbede12011-07-26 02:31:30 -0400126 ASSERT(S_ISLNK(ip->i_d.di_mode));
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000127 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000129 pathlen = ip->i_d.di_size;
130 if (!pathlen)
131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000134 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
135 link[pathlen] = '\0';
136 } else {
137 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000140 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return error;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000146 * Flags for xfs_free_eofblocks
147 */
148#define XFS_FREE_EOF_TRYLOCK (1<<0)
149
150/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000151 * This is called by xfs_inactive to free any blocks beyond eof
152 * when the link count isn't zero and by xfs_dm_punch_hole() when
153 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500155STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000156xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000158 xfs_inode_t *ip,
159 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 xfs_trans_t *tp;
162 int error;
163 xfs_fileoff_t end_fsb;
164 xfs_fileoff_t last_fsb;
165 xfs_filblks_t map_len;
166 int nimaps;
167 xfs_bmbt_irec_t imap;
168
169 /*
170 * Figure out if there are any blocks beyond the end
171 * of the file. If not, then there is nothing to do.
172 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000173 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000175 if (last_fsb <= end_fsb)
Jesper Juhl014c2542006-01-15 02:37:08 +0100176 return 0;
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000177 map_len = last_fsb - end_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 nimaps = 1;
180 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000181 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000182 NULL, 0, &imap, &nimaps, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 xfs_iunlock(ip, XFS_ILOCK_SHARED);
184
185 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100186 (imap.br_startblock != HOLESTARTBLOCK ||
187 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /*
189 * Attach the dquots to the inode up front.
190 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200191 error = xfs_qm_dqattach(ip, 0);
192 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100193 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /*
196 * There are blocks after the end of file.
197 * Free them up now by truncating the file to
198 * its current size.
199 */
200 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
201
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000202 if (flags & XFS_FREE_EOF_TRYLOCK) {
203 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
204 xfs_trans_cancel(tp, 0);
205 return 0;
206 }
207 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000208 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 error = xfs_trans_reserve(tp, 0,
212 XFS_ITRUNCATE_LOG_RES(mp),
213 0, XFS_TRANS_PERM_LOG_RES,
214 XFS_ITRUNCATE_LOG_COUNT);
215 if (error) {
216 ASSERT(XFS_FORCED_SHUTDOWN(mp));
217 xfs_trans_cancel(tp, 0);
218 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100219 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
222 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000223 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200225 error = xfs_itruncate_data(&tp, ip, ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (error) {
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200227 /*
228 * If we get an error at this point we simply don't
229 * bother truncating the file.
230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 xfs_trans_cancel(tp,
232 (XFS_TRANS_RELEASE_LOG_RES |
233 XFS_TRANS_ABORT));
234 } else {
235 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000236 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000238 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100240 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/*
244 * Free a symlink that has blocks associated with it.
245 */
246STATIC int
247xfs_inactive_symlink_rmt(
248 xfs_inode_t *ip,
249 xfs_trans_t **tpp)
250{
251 xfs_buf_t *bp;
252 int committed;
253 int done;
254 int error;
255 xfs_fsblock_t first_block;
256 xfs_bmap_free_t free_list;
257 int i;
258 xfs_mount_t *mp;
259 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
260 int nmaps;
261 xfs_trans_t *ntp;
262 int size;
263 xfs_trans_t *tp;
264
265 tp = *tpp;
266 mp = ip->i_mount;
267 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
268 /*
269 * We're freeing a symlink that has some
270 * blocks allocated to it. Free the
271 * blocks here. We know that we've got
272 * either 1 or 2 extents and that we can
273 * free them all in one bunmapi call.
274 */
275 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
276 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
277 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
278 ASSERT(XFS_FORCED_SHUTDOWN(mp));
279 xfs_trans_cancel(tp, 0);
280 *tpp = NULL;
281 return error;
282 }
283 /*
284 * Lock the inode, fix the size, and join it to the transaction.
285 * Hold it so in the normal path, we still have it locked for
286 * the second transaction. In the error paths we need it
287 * held so the cancel won't rele it, see below.
288 */
289 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
290 size = (int)ip->i_d.di_size;
291 ip->i_d.di_size = 0;
Christoph Hellwig898621d2010-06-24 11:36:58 +1000292 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
294 /*
295 * Find the block(s) so we can inval and unmap them.
296 */
297 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600298 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800299 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
301 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000302 &free_list)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 goto error0;
304 /*
305 * Invalidate the block(s).
306 */
307 for (i = 0; i < nmaps; i++) {
308 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
309 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
310 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000311 if (!bp) {
312 error = ENOMEM;
313 goto error1;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 xfs_trans_binval(tp, bp);
316 }
317 /*
318 * Unmap the dead block(s) to the free_list.
319 */
320 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000321 &first_block, &free_list, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 goto error1;
323 ASSERT(done);
324 /*
325 * Commit the first transaction. This logs the EFI and the inode.
326 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100327 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 goto error1;
329 /*
330 * The transaction must have been committed, since there were
331 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
332 * The new tp has the extent freeing and EFDs.
333 */
334 ASSERT(committed);
335 /*
336 * The first xact was committed, so add the inode to the new one.
337 * Mark it dirty so it will be logged and moved forward in the log as
338 * part of every commit.
339 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000340 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
342 /*
343 * Get a new, empty transaction to return to our caller.
344 */
345 ntp = xfs_trans_dup(tp);
346 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000347 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * If we get an error on the commit here or on the reserve below,
349 * we need to unlock the inode since the new transaction doesn't
350 * have the inode attached.
351 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000352 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 tp = ntp;
354 if (error) {
355 ASSERT(XFS_FORCED_SHUTDOWN(mp));
356 goto error0;
357 }
358 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100359 * transaction commit worked ok so we can drop the extra ticket
360 * reference that we gained in xfs_trans_dup()
361 */
362 xfs_log_ticket_put(tp->t_ticket);
363
364 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * Remove the memory for extent descriptions (just bookkeeping).
366 */
367 if (ip->i_df.if_bytes)
368 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
369 ASSERT(ip->i_df.if_bytes == 0);
370 /*
371 * Put an itruncate log reservation in the new transaction
372 * for our caller.
373 */
374 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
375 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
376 ASSERT(XFS_FORCED_SHUTDOWN(mp));
377 goto error0;
378 }
379 /*
380 * Return with the inode locked but not joined to the transaction.
381 */
382 *tpp = tp;
383 return 0;
384
385 error1:
386 xfs_bmap_cancel(&free_list);
387 error0:
388 /*
389 * Have to come here with the inode locked and either
390 * (held and in the transaction) or (not in the transaction).
391 * If the inode isn't held then cancel would iput it, but
392 * that's wrong since this is inactive and the vnode ref
393 * count is 0 already.
394 * Cancel won't do anything to the inode if held, but it still
395 * needs to be locked until the cancel is done, if it was
396 * joined to the transaction.
397 */
398 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
399 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
400 *tpp = NULL;
401 return error;
402
403}
404
405STATIC int
406xfs_inactive_symlink_local(
407 xfs_inode_t *ip,
408 xfs_trans_t **tpp)
409{
410 int error;
411
412 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
413 /*
414 * We're freeing a symlink which fit into
415 * the inode. Just free the memory used
416 * to hold the old symlink.
417 */
418 error = xfs_trans_reserve(*tpp, 0,
419 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
420 0, XFS_TRANS_PERM_LOG_RES,
421 XFS_ITRUNCATE_LOG_COUNT);
422
423 if (error) {
424 xfs_trans_cancel(*tpp, 0);
425 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100426 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
429
430 /*
431 * Zero length symlinks _can_ exist.
432 */
433 if (ip->i_df.if_bytes > 0) {
434 xfs_idata_realloc(ip,
435 -(ip->i_df.if_bytes),
436 XFS_DATA_FORK);
437 ASSERT(ip->i_df.if_bytes == 0);
438 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442STATIC int
443xfs_inactive_attrs(
444 xfs_inode_t *ip,
445 xfs_trans_t **tpp)
446{
447 xfs_trans_t *tp;
448 int error;
449 xfs_mount_t *mp;
450
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000451 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 tp = *tpp;
453 mp = ip->i_mount;
454 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +1000455 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +1000457 if (error)
458 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +1000461 if (error)
462 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
465 error = xfs_trans_reserve(tp, 0,
466 XFS_IFREE_LOG_RES(mp),
467 0, XFS_TRANS_PERM_LOG_RES,
468 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +1000469 if (error)
470 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000473 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
475
476 ASSERT(ip->i_d.di_anextents == 0);
477
478 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +0100479 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +1000480
481error_cancel:
482 ASSERT(XFS_FORCED_SHUTDOWN(mp));
483 xfs_trans_cancel(tp, 0);
484error_unlock:
485 *tpp = NULL;
486 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
487 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Christoph Hellwig993386c2007-08-28 16:12:30 +1000490int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000492 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000494 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int error;
496
Christoph Hellwig42173f62008-04-22 17:33:25 +1000497 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000501 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503
David Chinner2a82b8b2007-07-11 11:09:12 +1000504 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000505 int truncated;
506
David Chinner2a82b8b2007-07-11 11:09:12 +1000507 /*
508 * If we are using filestreams, and we have an unlinked
509 * file that we are processing the last close on, then nothing
510 * will be able to reopen and write to this file. Purge this
511 * inode from the filestreams cache so that it doesn't delay
512 * teardown of the inode.
513 */
514 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
515 xfs_filestream_deassociate(ip);
516
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000517 /*
518 * If we previously truncated this file and removed old data
519 * in the process, we want to initiate "early" writeout on
520 * the last close. This is an attempt to combat the notorious
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300521 * NULL files problem which is particularly noticeable from a
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000522 * truncate down, buffered (re-)write (delalloc), followed by
523 * a crash. What we are effectively doing here is
524 * significantly reducing the time window where we'd otherwise
525 * be exposed to that problem.
526 */
Christoph Hellwig09262b42007-08-29 11:44:50 +1000527 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Dave Chinnerdf4368a2011-06-23 01:35:00 +0000528 if (truncated) {
529 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
530 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
531 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
532 }
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000533 }
534
Dave Chinner6e857562010-12-23 12:02:31 +1100535 if (ip->i_d.di_nlink == 0)
536 return 0;
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000537
Al Viroabbede12011-07-26 02:31:30 -0400538 if ((S_ISREG(ip->i_d.di_mode) &&
Dave Chinner6e857562010-12-23 12:02:31 +1100539 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
540 ip->i_delayed_blks > 0)) &&
541 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
542 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
543
544 /*
545 * If we can't get the iolock just skip truncating the blocks
546 * past EOF because we could deadlock with the mmap_sem
547 * otherwise. We'll get another chance to drop them once the
548 * last reference to the inode is dropped, so we'll never leak
549 * blocks permanently.
550 *
551 * Further, check if the inode is being opened, written and
552 * closed frequently and we have delayed allocation blocks
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300553 * outstanding (e.g. streaming writes from the NFS server),
Dave Chinner6e857562010-12-23 12:02:31 +1100554 * truncating the blocks past EOF will cause fragmentation to
555 * occur.
556 *
557 * In this case don't do the truncation, either, but we have to
558 * be careful how we detect this case. Blocks beyond EOF show
559 * up as i_delayed_blks even when the inode is clean, so we
560 * need to truncate them away first before checking for a dirty
561 * release. Hence on the first dirty close we will still remove
562 * the speculative allocation, but after that we will leave it
563 * in place.
564 */
565 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
566 return 0;
567
568 error = xfs_free_eofblocks(mp, ip,
569 XFS_FREE_EOF_TRYLOCK);
570 if (error)
571 return error;
572
573 /* delalloc blocks after truncation means it really is dirty */
574 if (ip->i_delayed_blks)
575 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578}
579
580/*
581 * xfs_inactive
582 *
583 * This is called when the vnode reference count for the vnode
584 * goes to zero. If the file has been unlinked, then it must
585 * now be truncated. Also, we clear all of the read-ahead state
586 * kept for the inode here since the file is now closed.
587 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000588int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000590 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000592 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 xfs_fsblock_t first_block;
594 int committed;
595 xfs_trans_t *tp;
596 xfs_mount_t *mp;
597 int error;
598 int truncate;
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
601 * If the inode is already free, then there can be nothing
602 * to clean up here.
603 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +0100604 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ASSERT(ip->i_df.if_real_bytes == 0);
606 ASSERT(ip->i_df.if_broot_bytes == 0);
607 return VN_INACTIVE_CACHE;
608 }
609
610 /*
611 * Only do a truncate if it's a regular file with
612 * some actual space in it. It's OK to look at the
613 * inode's fields without the lock because we're the
614 * only one with a reference to the inode.
615 */
616 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000617 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
618 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Al Viroabbede12011-07-26 02:31:30 -0400619 S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 mp = ip->i_mount;
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 error = 0;
624
625 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000626 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goto out;
628
629 if (ip->i_d.di_nlink != 0) {
Al Viroabbede12011-07-26 02:31:30 -0400630 if ((S_ISREG(ip->i_d.di_mode) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +1000631 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100632 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +1100633 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
634 (!(ip->i_d.di_flags &
635 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
636 (ip->i_delayed_blks != 0)))) {
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000637 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000638 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100639 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 goto out;
642 }
643
644 ASSERT(ip->i_d.di_nlink == 0);
645
Christoph Hellwig7d095252009-06-08 15:33:32 +0200646 error = xfs_qm_dqattach(ip, 0);
647 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100648 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
651 if (truncate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 xfs_ilock(ip, XFS_IOLOCK_EXCL);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 error = xfs_trans_reserve(tp, 0,
655 XFS_ITRUNCATE_LOG_RES(mp),
656 0, XFS_TRANS_PERM_LOG_RES,
657 XFS_ITRUNCATE_LOG_COUNT);
658 if (error) {
659 /* Don't call itruncate_cleanup */
660 ASSERT(XFS_FORCED_SHUTDOWN(mp));
661 xfs_trans_cancel(tp, 0);
662 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100663 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
666 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000667 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200669 error = xfs_itruncate_data(&tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (error) {
671 xfs_trans_cancel(tp,
672 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
673 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100674 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Al Viroabbede12011-07-26 02:31:30 -0400676 } else if (S_ISLNK(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /*
679 * If we get an error while cleaning up a
680 * symlink we bail out.
681 */
682 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
683 xfs_inactive_symlink_rmt(ip, &tp) :
684 xfs_inactive_symlink_local(ip, &tp);
685
686 if (error) {
687 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100688 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Christoph Hellwig898621d2010-06-24 11:36:58 +1000691 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 } else {
693 error = xfs_trans_reserve(tp, 0,
694 XFS_IFREE_LOG_RES(mp),
695 0, XFS_TRANS_PERM_LOG_RES,
696 XFS_INACTIVE_LOG_COUNT);
697 if (error) {
698 ASSERT(XFS_FORCED_SHUTDOWN(mp));
699 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +0100700 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000704 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 /*
708 * If there are attributes associated with the file
709 * then blow them away now. The code calls a routine
710 * that recursively deconstructs the attribute fork.
711 * We need to just commit the current transaction
712 * because we can't use it for xfs_attr_inactive().
713 */
714 if (ip->i_d.di_anextents > 0) {
715 error = xfs_inactive_attrs(ip, &tp);
716 /*
717 * If we got an error, the transaction is already
718 * cancelled, and the inode is unlocked. Just get out.
719 */
720 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100721 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 } else if (ip->i_afp) {
723 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
724 }
725
726 /*
727 * Free the inode.
728 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600729 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 error = xfs_ifree(tp, ip, &free_list);
731 if (error) {
732 /*
733 * If we fail to free the inode, shut down. The cancel
734 * might do that, we need to make sure. Otherwise the
735 * inode might be lost for a long time or forever.
736 */
737 if (!XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100738 xfs_notice(mp, "%s: xfs_ifree returned error %d",
739 __func__, error);
Nathan Scott7d04a332006-06-09 14:58:38 +1000740 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
743 } else {
744 /*
745 * Credit the quota account(s). The inode is gone.
746 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200747 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /*
David Chinner78e9da72008-04-10 12:24:17 +1000750 * Just ignore errors at this point. There is nothing we can
751 * do except to try to keep going. Make sure it's not a silent
752 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
David Chinner78e9da72008-04-10 12:24:17 +1000754 error = xfs_bmap_finish(&tp, &free_list, &committed);
755 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100756 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
757 __func__, error);
David Chinner78e9da72008-04-10 12:24:17 +1000758 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
759 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100760 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
761 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * Release the dquots held by inode, if any.
766 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200767 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
769
770 out:
771 return VN_INACTIVE_CACHE;
772}
773
Barry Naujok384f3ce2008-05-21 16:58:22 +1000774/*
775 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
776 * is allowed, otherwise it has to be an exact match. If a CI match is found,
777 * ci_name->name will point to a the actual name (caller must free) or
778 * will be set to NULL if an exact match is found.
779 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000780int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000782 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000783 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000784 xfs_inode_t **ipp,
785 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000787 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int error;
789 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000791 trace_xfs_lookup(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
794 return XFS_ERROR(EIO);
795
796 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000797 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000799
800 if (error)
801 goto out;
802
Dave Chinner7b6259e2010-06-24 11:35:17 +1000803 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000804 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000805 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000806
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000807 return 0;
808
Barry Naujok384f3ce2008-05-21 16:58:22 +1000809out_free_name:
810 if (ci_name)
811 kmem_free(ci_name->name);
812out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000813 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return error;
815}
816
Christoph Hellwig993386c2007-08-28 16:12:30 +1000817int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000819 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000820 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000821 mode_t mode,
822 xfs_dev_t rdev,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000823 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100825 int is_dir = S_ISDIR(mode);
826 struct xfs_mount *mp = dp->i_mount;
827 struct xfs_inode *ip = NULL;
828 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000829 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 xfs_bmap_free_t free_list;
831 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000832 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 uint cancel_flags;
834 int committed;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000835 prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100836 struct xfs_dquot *udqp = NULL;
837 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100839 uint log_res;
840 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000842 trace_xfs_create(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100844 if (XFS_FORCED_SHUTDOWN(mp))
845 return XFS_ERROR(EIO);
846
Nathan Scott365ca832005-06-21 15:39:12 +1000847 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000848 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000850 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /*
853 * Make sure that we have allocated dquot(s) on disk.
854 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200855 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100856 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000858 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100860 if (is_dir) {
861 rdev = 0;
862 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
863 log_res = XFS_MKDIR_LOG_RES(mp);
864 log_count = XFS_MKDIR_LOG_COUNT;
865 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
866 } else {
867 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
868 log_res = XFS_CREATE_LOG_RES(mp);
869 log_count = XFS_CREATE_LOG_COUNT;
870 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /*
876 * Initially assume that the file does not exist and
877 * reserve the resources for that case. If that is not
878 * the case we'll drop the one we have and get a more
879 * appropriate transaction later.
880 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100881 error = xfs_trans_reserve(tp, resblks, log_res, 0,
882 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +0200884 /* flush outstanding delalloc blocks and retry */
885 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -0500886 error = xfs_trans_reserve(tp, resblks, log_res, 0,
887 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +0200888 }
889 if (error == ENOSPC) {
890 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100892 error = xfs_trans_reserve(tp, 0, log_res, 0,
893 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 if (error) {
896 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100897 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000900 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000901 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100903 /*
904 * Check for directory link count overflow.
905 */
906 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
907 error = XFS_ERROR(EMLINK);
908 goto out_trans_cancel;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100911 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /*
914 * Reserve disk quota and the inode.
915 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200916 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100918 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Barry Naujok556b8b12008-04-10 12:22:07 +1000920 error = xfs_dir_canenter(tp, dp, name, resblks);
921 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100922 goto out_trans_cancel;
923
924 /*
925 * A newly created regular or special file just has one directory
926 * entry pointing to them, but a directory also the "." entry
927 * pointing to itself.
928 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000929 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100930 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (error) {
932 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100933 goto out_trans_cancel;
934 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +1000938 * Now we join the directory inode to the transaction. We do not do it
939 * earlier because xfs_dir_ialloc might commit the previous transaction
940 * (and release all the locks). An error from here on will result in
941 * the transaction cancel unlocking dp so don't do it explicitly in the
942 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000944 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000945 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Barry Naujok556b8b12008-04-10 12:22:07 +1000947 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000948 &first_block, &free_list, resblks ?
949 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (error) {
951 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100952 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000954 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
956
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100957 if (is_dir) {
958 error = xfs_dir_init(tp, ip, dp);
959 if (error)
960 goto out_bmap_cancel;
961
962 error = xfs_bumplink(tp, dp);
963 if (error)
964 goto out_bmap_cancel;
965 }
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /*
968 * If this is a synchronous mount, make sure that the
969 * create transaction goes to disk before returning to
970 * the user.
971 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100972 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * Attach the dquot(s) to the inodes and modify them incore.
977 * These ids of the inode couldn't have changed since the new
978 * inode has been locked ever since it was created.
979 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200980 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100982 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100983 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000984 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000986 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000987 if (error)
988 goto out_release_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Christoph Hellwig7d095252009-06-08 15:33:32 +0200990 xfs_qm_dqrele(udqp);
991 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100993 *ipp = ip;
Christoph Hellwig288699f2010-06-23 18:11:15 +1000994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100996 out_bmap_cancel:
997 xfs_bmap_cancel(&free_list);
998 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001000 out_trans_cancel:
1001 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001002 out_release_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * Wait until after the current transaction is aborted to
1005 * release the inode. This prevents recursive transactions
1006 * and deadlocks from xfs_inactive.
1007 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001008 if (ip)
1009 IRELE(ip);
1010
1011 xfs_qm_dqrele(udqp);
1012 xfs_qm_dqrele(gdqp);
1013
1014 if (unlock_dp_on_error)
1015 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1016 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020int xfs_locked_n;
1021int xfs_small_retries;
1022int xfs_middle_retries;
1023int xfs_lots_retries;
1024int xfs_lock_delays;
1025#endif
1026
1027/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001028 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1029 * a different value
1030 */
1031static inline int
1032xfs_lock_inumorder(int lock_mode, int subclass)
1033{
1034 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001035 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001036 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001037 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001038
1039 return lock_mode;
1040}
1041
1042/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * The following routine will lock n inodes in exclusive mode.
1044 * We assume the caller calls us with the inodes in i_ino order.
1045 *
1046 * We need to detect deadlock where an inode that we lock
1047 * is in the AIL and we start waiting for another inode that is locked
1048 * by a thread in a long running transaction (such as truncate). This can
1049 * result in deadlock since the long running trans might need to wait
1050 * for the inode we just locked in order to push the tail and free space
1051 * in the log.
1052 */
1053void
1054xfs_lock_inodes(
1055 xfs_inode_t **ips,
1056 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 uint lock_mode)
1058{
1059 int attempts = 0, i, j, try_lock;
1060 xfs_log_item_t *lp;
1061
1062 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1063
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001064 try_lock = 0;
1065 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067again:
1068 for (; i < inodes; i++) {
1069 ASSERT(ips[i]);
1070
1071 if (i && (ips[i] == ips[i-1])) /* Already locked */
1072 continue;
1073
1074 /*
1075 * If try_lock is not set yet, make sure all locked inodes
1076 * are not in the AIL.
1077 * If any are, set try_lock to be used later.
1078 */
1079
1080 if (!try_lock) {
1081 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1082 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1083 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1084 try_lock++;
1085 }
1086 }
1087 }
1088
1089 /*
1090 * If any of the previous locks we have locked is in the AIL,
1091 * we must TRY to get the second and subsequent locks. If
1092 * we can't get any, we must release all we have
1093 * and try again.
1094 */
1095
1096 if (try_lock) {
1097 /* try_lock must be 0 if i is 0. */
1098 /*
1099 * try_lock means we have an inode locked
1100 * that is in the AIL.
1101 */
1102 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001103 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 attempts++;
1105
1106 /*
1107 * Unlock all previous guys and try again.
1108 * xfs_iunlock will try to push the tail
1109 * if the inode is in the AIL.
1110 */
1111
1112 for(j = i - 1; j >= 0; j--) {
1113
1114 /*
1115 * Check to see if we've already
1116 * unlocked this one.
1117 * Not the first one going back,
1118 * and the inode ptr is the same.
1119 */
1120 if ((j != (i - 1)) && ips[j] ==
1121 ips[j+1])
1122 continue;
1123
1124 xfs_iunlock(ips[j], lock_mode);
1125 }
1126
1127 if ((attempts % 5) == 0) {
1128 delay(1); /* Don't just spin the CPU */
1129#ifdef DEBUG
1130 xfs_lock_delays++;
1131#endif
1132 }
1133 i = 0;
1134 try_lock = 0;
1135 goto again;
1136 }
1137 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001138 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 }
1141
1142#ifdef DEBUG
1143 if (attempts) {
1144 if (attempts < 5) xfs_small_retries++;
1145 else if (attempts < 100) xfs_middle_retries++;
1146 else xfs_lots_retries++;
1147 } else {
1148 xfs_locked_n++;
1149 }
1150#endif
1151}
1152
David Chinnerf9114eb2008-09-17 16:51:21 +10001153/*
1154 * xfs_lock_two_inodes() can only be used to lock one type of lock
1155 * at a time - the iolock or the ilock, but not both at once. If
1156 * we lock both at once, lockdep will report false positives saying
1157 * we have violated locking orders.
1158 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001159void
1160xfs_lock_two_inodes(
1161 xfs_inode_t *ip0,
1162 xfs_inode_t *ip1,
1163 uint lock_mode)
1164{
1165 xfs_inode_t *temp;
1166 int attempts = 0;
1167 xfs_log_item_t *lp;
1168
David Chinnerf9114eb2008-09-17 16:51:21 +10001169 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1170 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001171 ASSERT(ip0->i_ino != ip1->i_ino);
1172
1173 if (ip0->i_ino > ip1->i_ino) {
1174 temp = ip0;
1175 ip0 = ip1;
1176 ip1 = temp;
1177 }
1178
1179 again:
1180 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1181
1182 /*
1183 * If the first lock we have locked is in the AIL, we must TRY to get
1184 * the second lock. If we can't get it, we must release the first one
1185 * and try again.
1186 */
1187 lp = (xfs_log_item_t *)ip0->i_itemp;
1188 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1189 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1190 xfs_iunlock(ip0, lock_mode);
1191 if ((++attempts % 5) == 0)
1192 delay(1); /* Don't just spin the CPU */
1193 goto again;
1194 }
1195 } else {
1196 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1197 }
1198}
1199
Christoph Hellwig993386c2007-08-28 16:12:30 +10001200int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001202 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001203 struct xfs_name *name,
1204 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001206 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001208 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int error = 0;
1210 xfs_bmap_free_t free_list;
1211 xfs_fsblock_t first_block;
1212 int cancel_flags;
1213 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int link_zero;
1215 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001216 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001218 trace_xfs_remove(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (XFS_FORCED_SHUTDOWN(mp))
1221 return XFS_ERROR(EIO);
1222
Christoph Hellwig7d095252009-06-08 15:33:32 +02001223 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001224 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Christoph Hellwig7d095252009-06-08 15:33:32 +02001227 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001228 if (error)
1229 goto std_return;
1230
1231 if (is_dir) {
1232 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1233 log_count = XFS_DEFAULT_LOG_COUNT;
1234 } else {
1235 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1236 log_count = XFS_REMOVE_LOG_COUNT;
1237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /*
1241 * We try to get the real space reservation first,
1242 * allowing for directory btree deletion(s) implying
1243 * possible bmap insert(s). If we can't get the space
1244 * reservation then we use 0 instead, and avoid the bmap
1245 * btree insert(s) in the directory code by, if the bmap
1246 * insert tries to happen, instead trimming the LAST
1247 * block from the directory.
1248 */
1249 resblks = XFS_REMOVE_SPACE_RES(mp);
1250 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001251 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (error == ENOSPC) {
1253 resblks = 0;
1254 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001255 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 if (error) {
1258 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001259 cancel_flags = 0;
1260 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001263 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Christoph Hellwig898621d2010-06-24 11:36:58 +10001265 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1266 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001269 * If we're removing a directory perform some additional validation.
1270 */
1271 if (is_dir) {
1272 ASSERT(ip->i_d.di_nlink >= 2);
1273 if (ip->i_d.di_nlink != 2) {
1274 error = XFS_ERROR(ENOTEMPTY);
1275 goto out_trans_cancel;
1276 }
1277 if (!xfs_dir_isempty(ip)) {
1278 error = XFS_ERROR(ENOTEMPTY);
1279 goto out_trans_cancel;
1280 }
1281 }
1282
Eric Sandeen9d87c312009-01-14 23:22:07 -06001283 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001284 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001285 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (error) {
1287 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001288 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001290 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001292 if (is_dir) {
1293 /*
1294 * Drop the link from ip's "..".
1295 */
1296 error = xfs_droplink(tp, dp);
1297 if (error)
1298 goto out_bmap_cancel;
1299
1300 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001301 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001302 */
1303 error = xfs_droplink(tp, ip);
1304 if (error)
1305 goto out_bmap_cancel;
1306 } else {
1307 /*
1308 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001309 * inode here. For a directory this is done implicitly
1310 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001311 */
1312 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001315 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001316 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001317 */
1318 error = xfs_droplink(tp, ip);
1319 if (error)
1320 goto out_bmap_cancel;
1321
1322 /*
1323 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * we are in the transaction.
1325 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001326 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * If this is a synchronous mount, make sure that the
1330 * remove transaction goes to disk before returning to
1331 * the user.
1332 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001333 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001336 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001337 if (error)
1338 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001340 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001341 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001345 * If we are using filestreams, kill the stream association.
1346 * If the file is still open it may get a new one but that
1347 * will get killed on last close in xfs_close() so we don't
1348 * have to worry about that.
1349 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001350 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001351 xfs_filestream_deassociate(ip);
1352
Christoph Hellwig288699f2010-06-23 18:11:15 +10001353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001355 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 xfs_bmap_cancel(&free_list);
1357 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001358 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001360 std_return:
1361 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Christoph Hellwig993386c2007-08-28 16:12:30 +10001364int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001366 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001367 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10001368 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001370 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int error;
1373 xfs_bmap_free_t free_list;
1374 xfs_fsblock_t first_block;
1375 int cancel_flags;
1376 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001379 trace_xfs_link(tdp, target_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001381 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (XFS_FORCED_SHUTDOWN(mp))
1384 return XFS_ERROR(EIO);
1385
Christoph Hellwig7d095252009-06-08 15:33:32 +02001386 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01001387 if (error)
1388 goto std_return;
1389
Christoph Hellwig7d095252009-06-08 15:33:32 +02001390 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (error)
1392 goto std_return;
1393
1394 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1395 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001396 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1398 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1399 if (error == ENOSPC) {
1400 resblks = 0;
1401 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1402 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1403 }
1404 if (error) {
1405 cancel_flags = 0;
1406 goto error_return;
1407 }
1408
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001409 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Christoph Hellwig898621d2010-06-24 11:36:58 +10001411 xfs_trans_ijoin_ref(tp, sip, XFS_ILOCK_EXCL);
1412 xfs_trans_ijoin_ref(tp, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /*
1415 * If the source has too many links, we can't make any more to it.
1416 */
1417 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1418 error = XFS_ERROR(EMLINK);
1419 goto error_return;
1420 }
1421
Nathan Scott365ca832005-06-21 15:39:12 +10001422 /*
1423 * If we are using project inheritance, we only allow hard link
1424 * creation in our tree when the project IDs are the same; else
1425 * the tree quota mechanism could be circumvented.
1426 */
1427 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001428 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10001429 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10001430 goto error_return;
1431 }
1432
Barry Naujok556b8b12008-04-10 12:22:07 +10001433 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1434 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto error_return;
1436
Eric Sandeen9d87c312009-01-14 23:22:07 -06001437 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Barry Naujok556b8b12008-04-10 12:22:07 +10001439 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1440 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (error)
1442 goto abort_return;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001443 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1445
1446 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001447 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /*
1451 * If this is a synchronous mount, make sure that the
1452 * link transaction goes to disk before returning to
1453 * the user.
1454 */
1455 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1456 xfs_trans_set_sync(tp);
1457 }
1458
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001459 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (error) {
1461 xfs_bmap_cancel(&free_list);
1462 goto abort_return;
1463 }
1464
Christoph Hellwig288699f2010-06-23 18:11:15 +10001465 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 abort_return:
1468 cancel_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 error_return:
1470 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001471 std_return:
1472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001474
Christoph Hellwig993386c2007-08-28 16:12:30 +10001475int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001477 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001478 struct xfs_name *link_name,
1479 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001480 mode_t mode,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001481 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001483 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 xfs_inode_t *ip;
1486 int error;
1487 int pathlen;
1488 xfs_bmap_free_t free_list;
1489 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001490 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 uint cancel_flags;
1492 int committed;
1493 xfs_fileoff_t first_fsb;
1494 xfs_filblks_t fs_blocks;
1495 int nmaps;
1496 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1497 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10001498 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int byte_cnt;
1500 int n;
1501 xfs_buf_t *bp;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001502 prid_t prid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct xfs_dquot *udqp, *gdqp;
1504 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Christoph Hellwig3937be52008-03-06 13:46:19 +11001506 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 error = 0;
1508 ip = NULL;
1509 tp = NULL;
1510
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001511 trace_xfs_symlink(dp, link_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (XFS_FORCED_SHUTDOWN(mp))
1514 return XFS_ERROR(EIO);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /*
1517 * Check component lengths of the target path name.
1518 */
1519 pathlen = strlen(target_path);
1520 if (pathlen >= MAXPATHLEN) /* total string too long */
1521 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001524 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001525 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001527 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 /*
1530 * Make sure that we have allocated dquot(s) on disk.
1531 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001532 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1534 if (error)
1535 goto std_return;
1536
1537 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1538 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1539 /*
1540 * The symlink will fit into the inode data fork?
1541 * There can't be any attributes so we get the whole variable part.
1542 */
1543 if (pathlen <= XFS_LITINO(mp))
1544 fs_blocks = 0;
1545 else
1546 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10001547 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
1549 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1550 if (error == ENOSPC && fs_blocks == 0) {
1551 resblks = 0;
1552 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
1553 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1554 }
1555 if (error) {
1556 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto error_return;
1558 }
1559
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001560 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001561 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 /*
1564 * Check whether the directory allows new symlinks or not.
1565 */
1566 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
1567 error = XFS_ERROR(EPERM);
1568 goto error_return;
1569 }
1570
1571 /*
1572 * Reserve disk quota : blocks and inode.
1573 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001574 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (error)
1576 goto error_return;
1577
1578 /*
1579 * Check for ability to enter directory entry, if no space reserved.
1580 */
Barry Naujok556b8b12008-04-10 12:22:07 +10001581 error = xfs_dir_canenter(tp, dp, link_name, resblks);
1582 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto error_return;
1584 /*
1585 * Initialize the bmap freelist prior to calling either
1586 * bmapi or the directory create code.
1587 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001588 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /*
1591 * Allocate an inode for the symlink.
1592 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001593 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
1594 prid, resblks > 0, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (error) {
1596 if (error == ENOSPC)
1597 goto error_return;
1598 goto error1;
1599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Christoph Hellwig993386c2007-08-28 16:12:30 +10001601 /*
1602 * An error after we've joined dp to the transaction will result in the
1603 * transaction cancel unlocking dp so don't do it explicitly in the
1604 * error path.
1605 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001606 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001607 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 /*
1610 * Also attach the dquot(s) to it, if applicable.
1611 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001612 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 if (resblks)
1615 resblks -= XFS_IALLOC_SPACE_RES(mp);
1616 /*
1617 * If the symlink will fit into the inode, write it inline.
1618 */
1619 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
1620 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
1621 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
1622 ip->i_d.di_size = pathlen;
1623
1624 /*
1625 * The inode was initially created in extent format.
1626 */
1627 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
1628 ip->i_df.if_flags |= XFS_IFINLINE;
1629
1630 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
1631 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
1632
1633 } else {
1634 first_fsb = 0;
1635 nmaps = SYMLINK_MAPS;
1636
1637 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
1638 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
1639 &first_block, resblks, mval, &nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001640 &free_list);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001641 if (error)
1642 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 if (resblks)
1645 resblks -= fs_blocks;
1646 ip->i_d.di_size = pathlen;
1647 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1648
1649 cur_chunk = target_path;
1650 for (n = 0; n < nmaps; n++) {
1651 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1652 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1653 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
1654 BTOBB(byte_cnt), 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001655 if (!bp) {
1656 error = ENOMEM;
1657 goto error2;
1658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (pathlen < byte_cnt) {
1660 byte_cnt = pathlen;
1661 }
1662 pathlen -= byte_cnt;
1663
Chandra Seetharaman62926042011-07-22 23:40:15 +00001664 memcpy(bp->b_addr, cur_chunk, byte_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 cur_chunk += byte_cnt;
1666
1667 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
1668 }
1669 }
1670
1671 /*
1672 * Create the directory entry for the symlink.
1673 */
Barry Naujok556b8b12008-04-10 12:22:07 +10001674 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
1675 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001676 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001677 goto error2;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001678 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1680
1681 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * If this is a synchronous mount, make sure that the
1683 * symlink transaction goes to disk before returning to
1684 * the user.
1685 */
1686 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1687 xfs_trans_set_sync(tp);
1688 }
1689
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001690 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (error) {
1692 goto error2;
1693 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001694 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001695 xfs_qm_dqrele(udqp);
1696 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Christoph Hellwig288699f2010-06-23 18:11:15 +10001698 *ipp = ip;
1699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 error2:
1702 IRELE(ip);
1703 error1:
1704 xfs_bmap_cancel(&free_list);
1705 cancel_flags |= XFS_TRANS_ABORT;
1706 error_return:
1707 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001708 xfs_qm_dqrele(udqp);
1709 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Christoph Hellwig993386c2007-08-28 16:12:30 +10001711 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001713 std_return:
1714 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717int
Christoph Hellwig993386c2007-08-28 16:12:30 +10001718xfs_set_dmattrs(
1719 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10001721 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001723 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int error;
1726
1727 if (!capable(CAP_SYS_ADMIN))
1728 return XFS_ERROR(EPERM);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (XFS_FORCED_SHUTDOWN(mp))
1731 return XFS_ERROR(EIO);
1732
1733 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1734 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1735 if (error) {
1736 xfs_trans_cancel(tp, 0);
1737 return error;
1738 }
1739 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001740 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Christoph Hellwig613d7042007-10-11 17:44:08 +10001742 ip->i_d.di_dmevmask = evmask;
1743 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001746 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 return error;
1749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751/*
1752 * xfs_alloc_file_space()
1753 * This routine allocates disk space for the given file.
1754 *
1755 * If alloc_type == 0, this request is for an ALLOCSP type
1756 * request which will change the file size. In this case, no
1757 * DMAPI event will be generated by the call. A TRUNCATE event
1758 * will be generated later by xfs_setattr.
1759 *
1760 * If alloc_type != 0, this request is for a RESVSP type
1761 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1762 * lower block boundary byte address is less than the file's
1763 * length.
1764 *
1765 * RETURNS:
1766 * 0 on success
1767 * errno on error
1768 *
1769 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001770STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771xfs_alloc_file_space(
1772 xfs_inode_t *ip,
1773 xfs_off_t offset,
1774 xfs_off_t len,
1775 int alloc_type,
1776 int attr_flags)
1777{
Nathan Scottdd9f4382006-01-11 15:28:28 +11001778 xfs_mount_t *mp = ip->i_mount;
1779 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 xfs_filblks_t allocated_fsb;
1781 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001782 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001784 xfs_fsblock_t firstfsb;
1785 int nimaps;
1786 int bmapi_flag;
1787 int quota_flag;
1788 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001790 xfs_bmbt_irec_t imaps[1], *imapp;
1791 xfs_bmap_free_t free_list;
1792 uint qblocks, resblks, resrtextents;
1793 int committed;
1794 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001796 trace_xfs_alloc_file_space(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (XFS_FORCED_SHUTDOWN(mp))
1799 return XFS_ERROR(EIO);
1800
Christoph Hellwig7d095252009-06-08 15:33:32 +02001801 error = xfs_qm_dqattach(ip, 0);
1802 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return error;
1804
1805 if (len <= 0)
1806 return XFS_ERROR(EINVAL);
1807
David Chinner957d0eb2007-06-18 16:50:37 +10001808 rt = XFS_IS_REALTIME_INODE(ip);
1809 extsz = xfs_get_extsz_hint(ip);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11001813 nimaps = 1;
Dave Chinner44722352010-08-24 12:02:11 +10001814 bmapi_flag = XFS_BMAPI_WRITE | alloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1816 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001819 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001822 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Nathan Scottdd9f4382006-01-11 15:28:28 +11001824 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11001825 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11001826 */
1827 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001829 do_div(s, extsz);
1830 s *= extsz;
1831 e = startoffset_fsb + allocatesize_fsb;
1832 if ((temp = do_mod(startoffset_fsb, extsz)))
1833 e += temp;
1834 if ((temp = do_mod(e, extsz)))
1835 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001837 s = 0;
1838 e = allocatesize_fsb;
1839 }
1840
Dave Chinner72656c42010-09-03 12:19:33 +10001841 /*
1842 * The transaction reservation is limited to a 32-bit block
1843 * count, hence we need to limit the number of blocks we are
1844 * trying to reserve to avoid an overflow. We can't allocate
1845 * more than @nimaps extents, and an extent is limited on disk
1846 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1847 */
1848 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
Nathan Scottdd9f4382006-01-11 15:28:28 +11001849 if (unlikely(rt)) {
Dave Chinner72656c42010-09-03 12:19:33 +10001850 resrtextents = qblocks = resblks;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001851 resrtextents /= mp->m_sb.sb_rextsize;
1852 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1853 quota_flag = XFS_QMOPT_RES_RTBLKS;
1854 } else {
1855 resrtextents = 0;
Dave Chinner72656c42010-09-03 12:19:33 +10001856 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001857 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859
1860 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001861 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 */
1863 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001864 error = xfs_trans_reserve(tp, resblks,
1865 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 XFS_TRANS_PERM_LOG_RES,
1867 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001869 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
1871 if (error) {
1872 /*
1873 * Free the transaction structure.
1874 */
1875 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1876 xfs_trans_cancel(tp, 0);
1877 break;
1878 }
1879 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001880 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1881 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (error)
1883 goto error1;
1884
Christoph Hellwig898621d2010-06-24 11:36:58 +10001885 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001888 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001890 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001891 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11001892 allocatesize_fsb, bmapi_flag,
1893 &firstfsb, 0, imapp, &nimaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001894 &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (error) {
1896 goto error0;
1897 }
1898
1899 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001900 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001902 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (error) {
1904 goto error0;
1905 }
1906
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001907 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1909 if (error) {
1910 break;
1911 }
1912
1913 allocated_fsb = imapp->br_blockcount;
1914
Nathan Scottdd9f4382006-01-11 15:28:28 +11001915 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 error = XFS_ERROR(ENOSPC);
1917 break;
1918 }
1919
1920 startoffset_fsb += allocated_fsb;
1921 allocatesize_fsb -= allocated_fsb;
1922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 return error;
1925
Nathan Scottdd9f4382006-01-11 15:28:28 +11001926error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001928 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001929
1930error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1932 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001933 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
1936/*
1937 * Zero file bytes between startoff and endoff inclusive.
1938 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001939 *
1940 * This function is used by xfs_free_file_space() to zero
1941 * partial blocks when the range to free is not block aligned.
1942 * When unreserving space with boundaries that are not block
1943 * aligned we round up the start and round down the end
1944 * boundaries and then use this function to zero the parts of
1945 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
1947STATIC int
1948xfs_zero_remaining_bytes(
1949 xfs_inode_t *ip,
1950 xfs_off_t startoff,
1951 xfs_off_t endoff)
1952{
1953 xfs_bmbt_irec_t imap;
1954 xfs_fileoff_t offset_fsb;
1955 xfs_off_t lastoffset;
1956 xfs_off_t offset;
1957 xfs_buf_t *bp;
1958 xfs_mount_t *mp = ip->i_mount;
1959 int nimap;
1960 int error = 0;
1961
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001962 /*
1963 * Avoid doing I/O beyond eof - it's not necessary
1964 * since nothing can read beyond eof. The space will
1965 * be zeroed when the file is extended anyway.
1966 */
1967 if (startoff >= ip->i_size)
1968 return 0;
1969
1970 if (endoff > ip->i_size)
1971 endoff = ip->i_size;
1972
Dave Chinner686865f2010-09-24 20:07:47 +10001973 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1974 mp->m_rtdev_targp : mp->m_ddev_targp,
1975 mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11001976 if (!bp)
1977 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001979 xfs_buf_unlock(bp);
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1982 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1983 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001984 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001985 NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (error || nimap < 1)
1987 break;
1988 ASSERT(imap.br_blockcount >= 1);
1989 ASSERT(imap.br_startoff == offset_fsb);
1990 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1991 if (lastoffset > endoff)
1992 lastoffset = endoff;
1993 if (imap.br_startblock == HOLESTARTBLOCK)
1994 continue;
1995 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1996 if (imap.br_state == XFS_EXT_UNWRITTEN)
1997 continue;
1998 XFS_BUF_UNDONE(bp);
1999 XFS_BUF_UNWRITE(bp);
2000 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002001 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002003 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002004 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2006 mp, bp, XFS_BUF_ADDR(bp));
2007 break;
2008 }
Chandra Seetharaman62926042011-07-22 23:40:15 +00002009 memset(bp->b_addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2011 0, lastoffset - offset + 1);
2012 XFS_BUF_UNDONE(bp);
2013 XFS_BUF_UNREAD(bp);
2014 XFS_BUF_WRITE(bp);
2015 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002016 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002017 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2019 mp, bp, XFS_BUF_ADDR(bp));
2020 break;
2021 }
2022 }
2023 xfs_buf_free(bp);
2024 return error;
2025}
2026
2027/*
2028 * xfs_free_file_space()
2029 * This routine frees disk space for the given file.
2030 *
2031 * This routine is only called by xfs_change_file_space
2032 * for an UNRESVSP type call.
2033 *
2034 * RETURNS:
2035 * 0 on success
2036 * errno on error
2037 *
2038 */
2039STATIC int
2040xfs_free_file_space(
2041 xfs_inode_t *ip,
2042 xfs_off_t offset,
2043 xfs_off_t len,
2044 int attr_flags)
2045{
2046 int committed;
2047 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 xfs_fileoff_t endoffset_fsb;
2049 int error;
2050 xfs_fsblock_t firstfsb;
2051 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 xfs_bmbt_irec_t imap;
2053 xfs_off_t ioffset;
2054 xfs_extlen_t mod=0;
2055 xfs_mount_t *mp;
2056 int nimap;
2057 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002058 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 int rt;
2060 xfs_fileoff_t startoffset_fsb;
2061 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002062 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 mp = ip->i_mount;
2065
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002066 trace_xfs_free_file_space(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002067
Christoph Hellwig7d095252009-06-08 15:33:32 +02002068 error = xfs_qm_dqattach(ip, 0);
2069 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return error;
2071
2072 error = 0;
2073 if (len <= 0) /* if nothing being freed */
2074 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002075 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002077 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002079 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002080 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002081 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002083 /* wait for the completion of any pending DIOs */
Christoph Hellwig4a06fd22011-08-23 08:28:13 +00002084 inode_dio_wait(VFS_I(ip));
Yingping Lu9fa80462006-03-22 12:44:35 +11002085 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002086
Tim Shimmine6a4b372007-11-23 16:30:42 +11002087 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002089
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002090 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002091 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002092 if (error)
2093 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002094 }
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /*
2097 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002098 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2100 * will take care of it for us.
2101 */
Eric Sandeen62118702008-03-06 13:44:28 +11002102 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002104 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002105 1, 0, NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (error)
2107 goto out_unlock_iolock;
2108 ASSERT(nimap == 0 || nimap == 1);
2109 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2110 xfs_daddr_t block;
2111
2112 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2113 block = imap.br_startblock;
2114 mod = do_div(block, mp->m_sb.sb_rextsize);
2115 if (mod)
2116 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2117 }
2118 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002119 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002120 1, 0, NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (error)
2122 goto out_unlock_iolock;
2123 ASSERT(nimap == 0 || nimap == 1);
2124 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2125 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2126 mod++;
2127 if (mod && (mod != mp->m_sb.sb_rextsize))
2128 endoffset_fsb -= mod;
2129 }
2130 }
2131 if ((done = (endoffset_fsb <= startoffset_fsb)))
2132 /*
2133 * One contiguous piece to clear
2134 */
2135 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2136 else {
2137 /*
2138 * Some full blocks, possibly two pieces to clear
2139 */
2140 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2141 error = xfs_zero_remaining_bytes(ip, offset,
2142 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2143 if (!error &&
2144 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2145 error = xfs_zero_remaining_bytes(ip,
2146 XFS_FSB_TO_B(mp, endoffset_fsb),
2147 offset + len - 1);
2148 }
2149
2150 /*
2151 * free file space until done or until there is an error
2152 */
2153 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2154 while (!error && !done) {
2155
2156 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002157 * allocate and setup the transaction. Allow this
2158 * transaction to dip into the reserve blocks to ensure
2159 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
2161 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002162 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 error = xfs_trans_reserve(tp,
2164 resblks,
2165 XFS_WRITE_LOG_RES(mp),
2166 0,
2167 XFS_TRANS_PERM_LOG_RES,
2168 XFS_WRITE_LOG_COUNT);
2169
2170 /*
2171 * check for running out of space
2172 */
2173 if (error) {
2174 /*
2175 * Free the transaction structure.
2176 */
2177 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2178 xfs_trans_cancel(tp, 0);
2179 break;
2180 }
2181 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002182 error = xfs_trans_reserve_quota(tp, mp,
2183 ip->i_udquot, ip->i_gdquot,
2184 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (error)
2186 goto error1;
2187
Christoph Hellwig898621d2010-06-24 11:36:58 +10002188 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 /*
2191 * issue the bunmapi() call to free the blocks
2192 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002193 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002194 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 endoffset_fsb - startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002196 0, 2, &firstfsb, &free_list, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (error) {
2198 goto error0;
2199 }
2200
2201 /*
2202 * complete the transaction
2203 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002204 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (error) {
2206 goto error0;
2207 }
2208
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002209 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2211 }
2212
2213 out_unlock_iolock:
2214 if (need_iolock)
2215 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2216 return error;
2217
2218 error0:
2219 xfs_bmap_cancel(&free_list);
2220 error1:
2221 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2222 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2223 XFS_ILOCK_EXCL);
2224 return error;
2225}
2226
2227/*
2228 * xfs_change_file_space()
2229 * This routine allocates or frees disk space for the given file.
2230 * The user specified parameters are checked for alignment and size
2231 * limitations.
2232 *
2233 * RETURNS:
2234 * 0 on success
2235 * errno on error
2236 *
2237 */
2238int
2239xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002240 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int cmd,
2242 xfs_flock64_t *bf,
2243 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 int attr_flags)
2245{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002246 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 int clrprealloc;
2248 int error;
2249 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 int setprealloc;
2251 xfs_off_t startoffset;
2252 xfs_off_t llen;
2253 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002254 struct iattr iattr;
Dave Chinner44722352010-08-24 12:02:11 +10002255 int prealloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Christoph Hellwig993386c2007-08-28 16:12:30 +10002257 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return XFS_ERROR(EINVAL);
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 switch (bf->l_whence) {
2261 case 0: /*SEEK_SET*/
2262 break;
2263 case 1: /*SEEK_CUR*/
2264 bf->l_start += offset;
2265 break;
2266 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002267 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 break;
2269 default:
2270 return XFS_ERROR(EINVAL);
2271 }
2272
2273 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2274
2275 if ( (bf->l_start < 0)
2276 || (bf->l_start > XFS_MAXIOFFSET(mp))
2277 || (bf->l_start + llen < 0)
2278 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2279 return XFS_ERROR(EINVAL);
2280
2281 bf->l_whence = 0;
2282
2283 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002284 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 /*
2287 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2288 * file space.
2289 * These calls do NOT zero the data space allocated to the file,
2290 * nor do they change the file size.
2291 *
2292 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2293 * space.
2294 * These calls cause the new file data to be zeroed and the file
2295 * size to be changed.
2296 */
2297 setprealloc = clrprealloc = 0;
Dave Chinner44722352010-08-24 12:02:11 +10002298 prealloc_type = XFS_BMAPI_PREALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 switch (cmd) {
Dave Chinner44722352010-08-24 12:02:11 +10002301 case XFS_IOC_ZERO_RANGE:
2302 prealloc_type |= XFS_BMAPI_CONVERT;
2303 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2304 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 case XFS_IOC_RESVSP:
2306 case XFS_IOC_RESVSP64:
2307 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
Dave Chinner44722352010-08-24 12:02:11 +10002308 prealloc_type, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (error)
2310 return error;
2311 setprealloc = 1;
2312 break;
2313
2314 case XFS_IOC_UNRESVSP:
2315 case XFS_IOC_UNRESVSP64:
2316 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2317 attr_flags)))
2318 return error;
2319 break;
2320
2321 case XFS_IOC_ALLOCSP:
2322 case XFS_IOC_ALLOCSP64:
2323 case XFS_IOC_FREESP:
2324 case XFS_IOC_FREESP64:
2325 if (startoffset > fsize) {
2326 error = xfs_alloc_file_space(ip, fsize,
2327 startoffset - fsize, 0, attr_flags);
2328 if (error)
2329 break;
2330 }
2331
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002332 iattr.ia_valid = ATTR_SIZE;
2333 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02002335 error = xfs_setattr_size(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 if (error)
2338 return error;
2339
2340 clrprealloc = 1;
2341 break;
2342
2343 default:
2344 ASSERT(0);
2345 return XFS_ERROR(EINVAL);
2346 }
2347
2348 /*
2349 * update the inode timestamp, mode, and prealloc flag bits
2350 */
2351 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2352
2353 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2354 0, 0, 0))) {
2355 /* ASSERT(0); */
2356 xfs_trans_cancel(tp, 0);
2357 return error;
2358 }
2359
2360 xfs_ilock(ip, XFS_ILOCK_EXCL);
2361
Christoph Hellwig898621d2010-06-24 11:36:58 +10002362 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002364 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ip->i_d.di_mode &= ~S_ISUID;
2366
2367 /*
2368 * Note that we don't have to worry about mandatory
2369 * file locking being disabled here because we only
2370 * clear the S_ISGID bit if the Group execute bit is
2371 * on, but if it was on then mandatory locking wouldn't
2372 * have been enabled.
2373 */
2374 if (ip->i_d.di_mode & S_IXGRP)
2375 ip->i_d.di_mode &= ~S_ISGID;
2376
Dave Chinnerdcd79a12010-09-28 12:27:25 +10002377 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379 if (setprealloc)
2380 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2381 else if (clrprealloc)
2382 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2383
2384 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinner82878892011-03-26 09:13:08 +11002385 if (attr_flags & XFS_ATTR_SYNC)
2386 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002388 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2391
2392 return error;
2393}