blob: ebdb88840a47817b5704df2aaa0fefb8b11fd96e [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
Dave Chinner5c8ed202011-09-18 20:40:45 +000075 error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, pathlen), mval, &nmaps,
76 0);
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) {
Christoph Hellwig901796a2011-10-10 16:52:49 +000090 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwig804c83c2007-08-28 13:59:03 +100091 xfs_buf_relse(bp);
92 goto out;
93 }
94 if (pathlen < byte_cnt)
95 byte_cnt = pathlen;
96 pathlen -= byte_cnt;
97
Chandra Seetharaman62926042011-07-22 23:40:15 +000098 memcpy(link, bp->b_addr, byte_cnt);
Christoph Hellwig804c83c2007-08-28 13:59:03 +100099 xfs_buf_relse(bp);
100 }
101
102 link[ip->i_d.di_size] = '\0';
103 error = 0;
104
105 out:
106 return error;
107}
108
Christoph Hellwig993386c2007-08-28 16:12:30 +1000109int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000111 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000112 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000114 xfs_mount_t *mp = ip->i_mount;
Carlos Maiolinob52a3602011-11-07 16:10:24 +0000115 xfs_fsize_t pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000118 trace_xfs_readlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (XFS_FORCED_SHUTDOWN(mp))
121 return XFS_ERROR(EIO);
122
123 xfs_ilock(ip, XFS_ILOCK_SHARED);
124
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000125 pathlen = ip->i_d.di_size;
126 if (!pathlen)
127 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Carlos Maiolinob52a3602011-11-07 16:10:24 +0000129 if (pathlen < 0 || pathlen > MAXPATHLEN) {
130 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
131 __func__, (unsigned long long) ip->i_ino,
132 (long long) pathlen);
133 ASSERT(0);
Jan Kara9b025eb2012-01-11 18:52:10 +0000134 error = XFS_ERROR(EFSCORRUPTED);
135 goto out;
Carlos Maiolinob52a3602011-11-07 16:10:24 +0000136 }
137
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000140 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
141 link[pathlen] = '\0';
142 } else {
143 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000146 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return error;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000152 * Flags for xfs_free_eofblocks
153 */
154#define XFS_FREE_EOF_TRYLOCK (1<<0)
155
156/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000157 * This is called by xfs_inactive to free any blocks beyond eof
158 * when the link count isn't zero and by xfs_dm_punch_hole() when
159 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500161STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000162xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000164 xfs_inode_t *ip,
165 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 xfs_trans_t *tp;
168 int error;
169 xfs_fileoff_t end_fsb;
170 xfs_fileoff_t last_fsb;
171 xfs_filblks_t map_len;
172 int nimaps;
173 xfs_bmbt_irec_t imap;
174
175 /*
176 * Figure out if there are any blocks beyond the end
177 * of the file. If not, then there is nothing to do.
178 */
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000179 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000181 if (last_fsb <= end_fsb)
Jesper Juhl014c2542006-01-15 02:37:08 +0100182 return 0;
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000183 map_len = last_fsb - end_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 nimaps = 1;
186 xfs_ilock(ip, XFS_ILOCK_SHARED);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000187 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 xfs_iunlock(ip, XFS_ILOCK_SHARED);
189
190 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100191 (imap.br_startblock != HOLESTARTBLOCK ||
192 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * Attach the dquots to the inode up front.
195 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200196 error = xfs_qm_dqattach(ip, 0);
197 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100198 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 /*
201 * There are blocks after the end of file.
202 * Free them up now by truncating the file to
203 * its current size.
204 */
205 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
206
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000207 if (flags & XFS_FREE_EOF_TRYLOCK) {
208 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
209 xfs_trans_cancel(tp, 0);
210 return 0;
211 }
212 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000213 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 error = xfs_trans_reserve(tp, 0,
217 XFS_ITRUNCATE_LOG_RES(mp),
218 0, XFS_TRANS_PERM_LOG_RES,
219 XFS_ITRUNCATE_LOG_COUNT);
220 if (error) {
221 ASSERT(XFS_FORCED_SHUTDOWN(mp));
222 xfs_trans_cancel(tp, 0);
223 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100224 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
227 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000228 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000230 /*
231 * Do not update the on-disk file size. If we update the
232 * on-disk file size and then the system crashes before the
233 * contents of the file are flushed to disk then the files
234 * may be full of holes (ie NULL files bug).
235 */
236 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000237 XFS_ISIZE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (error) {
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200239 /*
240 * If we get an error at this point we simply don't
241 * bother truncating the file.
242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 xfs_trans_cancel(tp,
244 (XFS_TRANS_RELEASE_LOG_RES |
245 XFS_TRANS_ABORT));
246 } else {
247 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000248 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000250 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100252 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/*
256 * Free a symlink that has blocks associated with it.
257 */
258STATIC int
259xfs_inactive_symlink_rmt(
260 xfs_inode_t *ip,
261 xfs_trans_t **tpp)
262{
263 xfs_buf_t *bp;
264 int committed;
265 int done;
266 int error;
267 xfs_fsblock_t first_block;
268 xfs_bmap_free_t free_list;
269 int i;
270 xfs_mount_t *mp;
271 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
272 int nmaps;
273 xfs_trans_t *ntp;
274 int size;
275 xfs_trans_t *tp;
276
277 tp = *tpp;
278 mp = ip->i_mount;
279 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
280 /*
281 * We're freeing a symlink that has some
282 * blocks allocated to it. Free the
283 * blocks here. We know that we've got
284 * either 1 or 2 extents and that we can
285 * free them all in one bunmapi call.
286 */
287 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
288 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
289 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
290 ASSERT(XFS_FORCED_SHUTDOWN(mp));
291 xfs_trans_cancel(tp, 0);
292 *tpp = NULL;
293 return error;
294 }
295 /*
296 * Lock the inode, fix the size, and join it to the transaction.
297 * Hold it so in the normal path, we still have it locked for
298 * the second transaction. In the error paths we need it
299 * held so the cancel won't rele it, see below.
300 */
301 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
302 size = (int)ip->i_d.di_size;
303 ip->i_d.di_size = 0;
Christoph Hellwigddc34152011-09-19 15:00:54 +0000304 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
306 /*
307 * Find the block(s) so we can inval and unmap them.
308 */
309 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600310 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800311 nmaps = ARRAY_SIZE(mval);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000312 error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, size),
313 mval, &nmaps, 0);
314 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto error0;
316 /*
317 * Invalidate the block(s).
318 */
319 for (i = 0; i < nmaps; i++) {
320 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
321 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
322 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000323 if (!bp) {
324 error = ENOMEM;
325 goto error1;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 xfs_trans_binval(tp, bp);
328 }
329 /*
330 * Unmap the dead block(s) to the free_list.
331 */
332 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000333 &first_block, &free_list, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto error1;
335 ASSERT(done);
336 /*
337 * Commit the first transaction. This logs the EFI and the inode.
338 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100339 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto error1;
341 /*
342 * The transaction must have been committed, since there were
343 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
344 * The new tp has the extent freeing and EFDs.
345 */
346 ASSERT(committed);
347 /*
348 * The first xact was committed, so add the inode to the new one.
349 * Mark it dirty so it will be logged and moved forward in the log as
350 * part of every commit.
351 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000352 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
354 /*
355 * Get a new, empty transaction to return to our caller.
356 */
357 ntp = xfs_trans_dup(tp);
358 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000359 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * If we get an error on the commit here or on the reserve below,
361 * we need to unlock the inode since the new transaction doesn't
362 * have the inode attached.
363 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000364 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 tp = ntp;
366 if (error) {
367 ASSERT(XFS_FORCED_SHUTDOWN(mp));
368 goto error0;
369 }
370 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100371 * transaction commit worked ok so we can drop the extra ticket
372 * reference that we gained in xfs_trans_dup()
373 */
374 xfs_log_ticket_put(tp->t_ticket);
375
376 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * Remove the memory for extent descriptions (just bookkeeping).
378 */
379 if (ip->i_df.if_bytes)
380 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
381 ASSERT(ip->i_df.if_bytes == 0);
382 /*
383 * Put an itruncate log reservation in the new transaction
384 * for our caller.
385 */
386 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
387 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
388 ASSERT(XFS_FORCED_SHUTDOWN(mp));
389 goto error0;
390 }
391 /*
392 * Return with the inode locked but not joined to the transaction.
393 */
394 *tpp = tp;
395 return 0;
396
397 error1:
398 xfs_bmap_cancel(&free_list);
399 error0:
400 /*
401 * Have to come here with the inode locked and either
402 * (held and in the transaction) or (not in the transaction).
403 * If the inode isn't held then cancel would iput it, but
404 * that's wrong since this is inactive and the vnode ref
405 * count is 0 already.
406 * Cancel won't do anything to the inode if held, but it still
407 * needs to be locked until the cancel is done, if it was
408 * joined to the transaction.
409 */
410 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
411 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
412 *tpp = NULL;
413 return error;
414
415}
416
417STATIC int
418xfs_inactive_symlink_local(
419 xfs_inode_t *ip,
420 xfs_trans_t **tpp)
421{
422 int error;
423
424 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
425 /*
426 * We're freeing a symlink which fit into
427 * the inode. Just free the memory used
428 * to hold the old symlink.
429 */
430 error = xfs_trans_reserve(*tpp, 0,
431 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
432 0, XFS_TRANS_PERM_LOG_RES,
433 XFS_ITRUNCATE_LOG_COUNT);
434
435 if (error) {
436 xfs_trans_cancel(*tpp, 0);
437 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100438 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
441
442 /*
443 * Zero length symlinks _can_ exist.
444 */
445 if (ip->i_df.if_bytes > 0) {
446 xfs_idata_realloc(ip,
447 -(ip->i_df.if_bytes),
448 XFS_DATA_FORK);
449 ASSERT(ip->i_df.if_bytes == 0);
450 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454STATIC int
455xfs_inactive_attrs(
456 xfs_inode_t *ip,
457 xfs_trans_t **tpp)
458{
459 xfs_trans_t *tp;
460 int error;
461 xfs_mount_t *mp;
462
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000463 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 tp = *tpp;
465 mp = ip->i_mount;
466 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +1000467 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +1000469 if (error)
470 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +1000473 if (error)
474 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
477 error = xfs_trans_reserve(tp, 0,
478 XFS_IFREE_LOG_RES(mp),
479 0, XFS_TRANS_PERM_LOG_RES,
480 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +1000481 if (error)
482 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000485 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
487
488 ASSERT(ip->i_d.di_anextents == 0);
489
490 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +0100491 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +1000492
493error_cancel:
494 ASSERT(XFS_FORCED_SHUTDOWN(mp));
495 xfs_trans_cancel(tp, 0);
496error_unlock:
497 *tpp = NULL;
498 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
499 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Christoph Hellwig993386c2007-08-28 16:12:30 +1000502int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000504 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000506 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int error;
508
Christoph Hellwig42173f62008-04-22 17:33:25 +1000509 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000513 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515
David Chinner2a82b8b2007-07-11 11:09:12 +1000516 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000517 int truncated;
518
David Chinner2a82b8b2007-07-11 11:09:12 +1000519 /*
520 * If we are using filestreams, and we have an unlinked
521 * file that we are processing the last close on, then nothing
522 * will be able to reopen and write to this file. Purge this
523 * inode from the filestreams cache so that it doesn't delay
524 * teardown of the inode.
525 */
526 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
527 xfs_filestream_deassociate(ip);
528
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000529 /*
530 * If we previously truncated this file and removed old data
531 * in the process, we want to initiate "early" writeout on
532 * the last close. This is an attempt to combat the notorious
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300533 * NULL files problem which is particularly noticeable from a
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000534 * truncate down, buffered (re-)write (delalloc), followed by
535 * a crash. What we are effectively doing here is
536 * significantly reducing the time window where we'd otherwise
537 * be exposed to that problem.
538 */
Christoph Hellwig09262b42007-08-29 11:44:50 +1000539 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Dave Chinnerdf4368a2011-06-23 01:35:00 +0000540 if (truncated) {
541 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
542 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
543 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
544 }
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000545 }
546
Dave Chinner6e857562010-12-23 12:02:31 +1100547 if (ip->i_d.di_nlink == 0)
548 return 0;
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000549
Al Viroabbede12011-07-26 02:31:30 -0400550 if ((S_ISREG(ip->i_d.di_mode) &&
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000551 (VFS_I(ip)->i_size > 0 ||
552 (VN_CACHED(VFS_I(ip)) > 0 || ip->i_delayed_blks > 0)) &&
Dave Chinner6e857562010-12-23 12:02:31 +1100553 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
554 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
555
556 /*
557 * If we can't get the iolock just skip truncating the blocks
558 * past EOF because we could deadlock with the mmap_sem
559 * otherwise. We'll get another chance to drop them once the
560 * last reference to the inode is dropped, so we'll never leak
561 * blocks permanently.
562 *
563 * Further, check if the inode is being opened, written and
564 * closed frequently and we have delayed allocation blocks
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300565 * outstanding (e.g. streaming writes from the NFS server),
Dave Chinner6e857562010-12-23 12:02:31 +1100566 * truncating the blocks past EOF will cause fragmentation to
567 * occur.
568 *
569 * In this case don't do the truncation, either, but we have to
570 * be careful how we detect this case. Blocks beyond EOF show
571 * up as i_delayed_blks even when the inode is clean, so we
572 * need to truncate them away first before checking for a dirty
573 * release. Hence on the first dirty close we will still remove
574 * the speculative allocation, but after that we will leave it
575 * in place.
576 */
577 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
578 return 0;
579
580 error = xfs_free_eofblocks(mp, ip,
581 XFS_FREE_EOF_TRYLOCK);
582 if (error)
583 return error;
584
585 /* delalloc blocks after truncation means it really is dirty */
586 if (ip->i_delayed_blks)
587 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590}
591
592/*
593 * xfs_inactive
594 *
595 * This is called when the vnode reference count for the vnode
596 * goes to zero. If the file has been unlinked, then it must
597 * now be truncated. Also, we clear all of the read-ahead state
598 * kept for the inode here since the file is now closed.
599 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000600int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000602 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000604 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 xfs_fsblock_t first_block;
606 int committed;
607 xfs_trans_t *tp;
608 xfs_mount_t *mp;
609 int error;
610 int truncate;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * If the inode is already free, then there can be nothing
614 * to clean up here.
615 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +0100616 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ASSERT(ip->i_df.if_real_bytes == 0);
618 ASSERT(ip->i_df.if_broot_bytes == 0);
619 return VN_INACTIVE_CACHE;
620 }
621
622 /*
623 * Only do a truncate if it's a regular file with
624 * some actual space in it. It's OK to look at the
625 * inode's fields without the lock because we're the
626 * only one with a reference to the inode.
627 */
628 truncate = ((ip->i_d.di_nlink == 0) &&
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000629 ((ip->i_d.di_size != 0) || XFS_ISIZE(ip) != 0 ||
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000630 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Al Viroabbede12011-07-26 02:31:30 -0400631 S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 mp = ip->i_mount;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 error = 0;
636
637 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000638 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto out;
640
641 if (ip->i_d.di_nlink != 0) {
Al Viroabbede12011-07-26 02:31:30 -0400642 if ((S_ISREG(ip->i_d.di_mode) &&
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000643 (VFS_I(ip)->i_size > 0 ||
644 (VN_CACHED(VFS_I(ip)) > 0 || ip->i_delayed_blks > 0)) &&
645 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
646 (!(ip->i_d.di_flags &
Nathan Scottdd9f4382006-01-11 15:28:28 +1100647 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000648 ip->i_delayed_blks != 0))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000649 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000650 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100651 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 goto out;
654 }
655
656 ASSERT(ip->i_d.di_nlink == 0);
657
Christoph Hellwig7d095252009-06-08 15:33:32 +0200658 error = xfs_qm_dqattach(ip, 0);
659 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100660 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
663 if (truncate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 xfs_ilock(ip, XFS_IOLOCK_EXCL);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 error = xfs_trans_reserve(tp, 0,
667 XFS_ITRUNCATE_LOG_RES(mp),
668 0, XFS_TRANS_PERM_LOG_RES,
669 XFS_ITRUNCATE_LOG_COUNT);
670 if (error) {
671 /* Don't call itruncate_cleanup */
672 ASSERT(XFS_FORCED_SHUTDOWN(mp));
673 xfs_trans_cancel(tp, 0);
674 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100675 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000679 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000681 ip->i_d.di_size = 0;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000682 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
683
684 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (error) {
686 xfs_trans_cancel(tp,
687 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
688 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100689 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000691
692 ASSERT(ip->i_d.di_nextents == 0);
Al Viroabbede12011-07-26 02:31:30 -0400693 } else if (S_ISLNK(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /*
696 * If we get an error while cleaning up a
697 * symlink we bail out.
698 */
699 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
700 xfs_inactive_symlink_rmt(ip, &tp) :
701 xfs_inactive_symlink_local(ip, &tp);
702
703 if (error) {
704 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100705 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
Christoph Hellwigddc34152011-09-19 15:00:54 +0000708 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 } else {
710 error = xfs_trans_reserve(tp, 0,
711 XFS_IFREE_LOG_RES(mp),
712 0, XFS_TRANS_PERM_LOG_RES,
713 XFS_INACTIVE_LOG_COUNT);
714 if (error) {
715 ASSERT(XFS_FORCED_SHUTDOWN(mp));
716 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +0100717 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000721 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
724 /*
725 * If there are attributes associated with the file
726 * then blow them away now. The code calls a routine
727 * that recursively deconstructs the attribute fork.
728 * We need to just commit the current transaction
729 * because we can't use it for xfs_attr_inactive().
730 */
731 if (ip->i_d.di_anextents > 0) {
732 error = xfs_inactive_attrs(ip, &tp);
733 /*
734 * If we got an error, the transaction is already
735 * cancelled, and the inode is unlocked. Just get out.
736 */
737 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100738 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 } else if (ip->i_afp) {
740 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
741 }
742
743 /*
744 * Free the inode.
745 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600746 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 error = xfs_ifree(tp, ip, &free_list);
748 if (error) {
749 /*
750 * If we fail to free the inode, shut down. The cancel
751 * might do that, we need to make sure. Otherwise the
752 * inode might be lost for a long time or forever.
753 */
754 if (!XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100755 xfs_notice(mp, "%s: xfs_ifree returned error %d",
756 __func__, error);
Nathan Scott7d04a332006-06-09 14:58:38 +1000757 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
760 } else {
761 /*
762 * Credit the quota account(s). The inode is gone.
763 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200764 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /*
David Chinner78e9da72008-04-10 12:24:17 +1000767 * Just ignore errors at this point. There is nothing we can
768 * do except to try to keep going. Make sure it's not a silent
769 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
David Chinner78e9da72008-04-10 12:24:17 +1000771 error = xfs_bmap_finish(&tp, &free_list, &committed);
772 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100773 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
774 __func__, error);
David Chinner78e9da72008-04-10 12:24:17 +1000775 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
776 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100777 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
778 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * Release the dquots held by inode, if any.
783 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200784 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
786
787 out:
788 return VN_INACTIVE_CACHE;
789}
790
Barry Naujok384f3ce2008-05-21 16:58:22 +1000791/*
792 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
793 * is allowed, otherwise it has to be an exact match. If a CI match is found,
794 * ci_name->name will point to a the actual name (caller must free) or
795 * will be set to NULL if an exact match is found.
796 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000797int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000799 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000800 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000801 xfs_inode_t **ipp,
802 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000804 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int error;
806 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000808 trace_xfs_lookup(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
811 return XFS_ERROR(EIO);
812
813 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000814 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000816
817 if (error)
818 goto out;
819
Dave Chinner7b6259e2010-06-24 11:35:17 +1000820 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000821 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000822 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000823
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000824 return 0;
825
Barry Naujok384f3ce2008-05-21 16:58:22 +1000826out_free_name:
827 if (ci_name)
828 kmem_free(ci_name->name);
829out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000830 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return error;
832}
833
Christoph Hellwig993386c2007-08-28 16:12:30 +1000834int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000836 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000837 struct xfs_name *name,
Al Viro576b1d62011-07-26 02:50:15 -0400838 umode_t mode,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000839 xfs_dev_t rdev,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000840 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100842 int is_dir = S_ISDIR(mode);
843 struct xfs_mount *mp = dp->i_mount;
844 struct xfs_inode *ip = NULL;
845 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000846 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 xfs_bmap_free_t free_list;
848 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000849 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 uint cancel_flags;
851 int committed;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000852 prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100853 struct xfs_dquot *udqp = NULL;
854 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100856 uint log_res;
857 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000859 trace_xfs_create(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100861 if (XFS_FORCED_SHUTDOWN(mp))
862 return XFS_ERROR(EIO);
863
Nathan Scott365ca832005-06-21 15:39:12 +1000864 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000865 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000867 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /*
870 * Make sure that we have allocated dquot(s) on disk.
871 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200872 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100873 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000875 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100877 if (is_dir) {
878 rdev = 0;
879 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
880 log_res = XFS_MKDIR_LOG_RES(mp);
881 log_count = XFS_MKDIR_LOG_COUNT;
882 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
883 } else {
884 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
885 log_res = XFS_CREATE_LOG_RES(mp);
886 log_count = XFS_CREATE_LOG_COUNT;
887 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /*
893 * Initially assume that the file does not exist and
894 * reserve the resources for that case. If that is not
895 * the case we'll drop the one we have and get a more
896 * appropriate transaction later.
897 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100898 error = xfs_trans_reserve(tp, resblks, log_res, 0,
899 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +0200901 /* flush outstanding delalloc blocks and retry */
902 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -0500903 error = xfs_trans_reserve(tp, resblks, log_res, 0,
904 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +0200905 }
906 if (error == ENOSPC) {
907 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100909 error = xfs_trans_reserve(tp, 0, log_res, 0,
910 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 if (error) {
913 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100914 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000917 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000918 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100920 /*
921 * Check for directory link count overflow.
922 */
923 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
924 error = XFS_ERROR(EMLINK);
925 goto out_trans_cancel;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100928 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 /*
931 * Reserve disk quota and the inode.
932 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200933 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100935 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Barry Naujok556b8b12008-04-10 12:22:07 +1000937 error = xfs_dir_canenter(tp, dp, name, resblks);
938 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100939 goto out_trans_cancel;
940
941 /*
942 * A newly created regular or special file just has one directory
943 * entry pointing to them, but a directory also the "." entry
944 * pointing to itself.
945 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000946 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100947 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (error) {
949 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100950 goto out_trans_cancel;
951 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +1000955 * Now we join the directory inode to the transaction. We do not do it
956 * earlier because xfs_dir_ialloc might commit the previous transaction
957 * (and release all the locks). An error from here on will result in
958 * the transaction cancel unlocking dp so don't do it explicitly in the
959 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000961 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000962 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Barry Naujok556b8b12008-04-10 12:22:07 +1000964 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000965 &first_block, &free_list, resblks ?
966 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (error) {
968 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100969 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000971 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
973
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100974 if (is_dir) {
975 error = xfs_dir_init(tp, ip, dp);
976 if (error)
977 goto out_bmap_cancel;
978
979 error = xfs_bumplink(tp, dp);
980 if (error)
981 goto out_bmap_cancel;
982 }
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * If this is a synchronous mount, make sure that the
986 * create transaction goes to disk before returning to
987 * the user.
988 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100989 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /*
993 * Attach the dquot(s) to the inodes and modify them incore.
994 * These ids of the inode couldn't have changed since the new
995 * inode has been locked ever since it was created.
996 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200997 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100999 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001000 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001001 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001003 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001004 if (error)
1005 goto out_release_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Christoph Hellwig7d095252009-06-08 15:33:32 +02001007 xfs_qm_dqrele(udqp);
1008 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001010 *ipp = ip;
Christoph Hellwig288699f2010-06-23 18:11:15 +10001011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001013 out_bmap_cancel:
1014 xfs_bmap_cancel(&free_list);
1015 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001017 out_trans_cancel:
1018 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001019 out_release_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * Wait until after the current transaction is aborted to
1022 * release the inode. This prevents recursive transactions
1023 * and deadlocks from xfs_inactive.
1024 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001025 if (ip)
1026 IRELE(ip);
1027
1028 xfs_qm_dqrele(udqp);
1029 xfs_qm_dqrele(gdqp);
1030
1031 if (unlock_dp_on_error)
1032 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1033 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037int xfs_locked_n;
1038int xfs_small_retries;
1039int xfs_middle_retries;
1040int xfs_lots_retries;
1041int xfs_lock_delays;
1042#endif
1043
1044/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001045 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1046 * a different value
1047 */
1048static inline int
1049xfs_lock_inumorder(int lock_mode, int subclass)
1050{
1051 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001052 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001053 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001054 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001055
1056 return lock_mode;
1057}
1058
1059/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 * The following routine will lock n inodes in exclusive mode.
1061 * We assume the caller calls us with the inodes in i_ino order.
1062 *
1063 * We need to detect deadlock where an inode that we lock
1064 * is in the AIL and we start waiting for another inode that is locked
1065 * by a thread in a long running transaction (such as truncate). This can
1066 * result in deadlock since the long running trans might need to wait
1067 * for the inode we just locked in order to push the tail and free space
1068 * in the log.
1069 */
1070void
1071xfs_lock_inodes(
1072 xfs_inode_t **ips,
1073 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 uint lock_mode)
1075{
1076 int attempts = 0, i, j, try_lock;
1077 xfs_log_item_t *lp;
1078
1079 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1080
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001081 try_lock = 0;
1082 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084again:
1085 for (; i < inodes; i++) {
1086 ASSERT(ips[i]);
1087
1088 if (i && (ips[i] == ips[i-1])) /* Already locked */
1089 continue;
1090
1091 /*
1092 * If try_lock is not set yet, make sure all locked inodes
1093 * are not in the AIL.
1094 * If any are, set try_lock to be used later.
1095 */
1096
1097 if (!try_lock) {
1098 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1099 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1100 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1101 try_lock++;
1102 }
1103 }
1104 }
1105
1106 /*
1107 * If any of the previous locks we have locked is in the AIL,
1108 * we must TRY to get the second and subsequent locks. If
1109 * we can't get any, we must release all we have
1110 * and try again.
1111 */
1112
1113 if (try_lock) {
1114 /* try_lock must be 0 if i is 0. */
1115 /*
1116 * try_lock means we have an inode locked
1117 * that is in the AIL.
1118 */
1119 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001120 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 attempts++;
1122
1123 /*
1124 * Unlock all previous guys and try again.
1125 * xfs_iunlock will try to push the tail
1126 * if the inode is in the AIL.
1127 */
1128
1129 for(j = i - 1; j >= 0; j--) {
1130
1131 /*
1132 * Check to see if we've already
1133 * unlocked this one.
1134 * Not the first one going back,
1135 * and the inode ptr is the same.
1136 */
1137 if ((j != (i - 1)) && ips[j] ==
1138 ips[j+1])
1139 continue;
1140
1141 xfs_iunlock(ips[j], lock_mode);
1142 }
1143
1144 if ((attempts % 5) == 0) {
1145 delay(1); /* Don't just spin the CPU */
1146#ifdef DEBUG
1147 xfs_lock_delays++;
1148#endif
1149 }
1150 i = 0;
1151 try_lock = 0;
1152 goto again;
1153 }
1154 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001155 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 }
1158
1159#ifdef DEBUG
1160 if (attempts) {
1161 if (attempts < 5) xfs_small_retries++;
1162 else if (attempts < 100) xfs_middle_retries++;
1163 else xfs_lots_retries++;
1164 } else {
1165 xfs_locked_n++;
1166 }
1167#endif
1168}
1169
David Chinnerf9114eb2008-09-17 16:51:21 +10001170/*
1171 * xfs_lock_two_inodes() can only be used to lock one type of lock
1172 * at a time - the iolock or the ilock, but not both at once. If
1173 * we lock both at once, lockdep will report false positives saying
1174 * we have violated locking orders.
1175 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001176void
1177xfs_lock_two_inodes(
1178 xfs_inode_t *ip0,
1179 xfs_inode_t *ip1,
1180 uint lock_mode)
1181{
1182 xfs_inode_t *temp;
1183 int attempts = 0;
1184 xfs_log_item_t *lp;
1185
David Chinnerf9114eb2008-09-17 16:51:21 +10001186 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1187 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001188 ASSERT(ip0->i_ino != ip1->i_ino);
1189
1190 if (ip0->i_ino > ip1->i_ino) {
1191 temp = ip0;
1192 ip0 = ip1;
1193 ip1 = temp;
1194 }
1195
1196 again:
1197 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1198
1199 /*
1200 * If the first lock we have locked is in the AIL, we must TRY to get
1201 * the second lock. If we can't get it, we must release the first one
1202 * and try again.
1203 */
1204 lp = (xfs_log_item_t *)ip0->i_itemp;
1205 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1206 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1207 xfs_iunlock(ip0, lock_mode);
1208 if ((++attempts % 5) == 0)
1209 delay(1); /* Don't just spin the CPU */
1210 goto again;
1211 }
1212 } else {
1213 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1214 }
1215}
1216
Christoph Hellwig993386c2007-08-28 16:12:30 +10001217int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001219 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001220 struct xfs_name *name,
1221 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001223 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001225 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 int error = 0;
1227 xfs_bmap_free_t free_list;
1228 xfs_fsblock_t first_block;
1229 int cancel_flags;
1230 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int link_zero;
1232 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001233 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001235 trace_xfs_remove(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (XFS_FORCED_SHUTDOWN(mp))
1238 return XFS_ERROR(EIO);
1239
Christoph Hellwig7d095252009-06-08 15:33:32 +02001240 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001241 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Christoph Hellwig7d095252009-06-08 15:33:32 +02001244 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001245 if (error)
1246 goto std_return;
1247
1248 if (is_dir) {
1249 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1250 log_count = XFS_DEFAULT_LOG_COUNT;
1251 } else {
1252 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1253 log_count = XFS_REMOVE_LOG_COUNT;
1254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /*
1258 * We try to get the real space reservation first,
1259 * allowing for directory btree deletion(s) implying
1260 * possible bmap insert(s). If we can't get the space
1261 * reservation then we use 0 instead, and avoid the bmap
1262 * btree insert(s) in the directory code by, if the bmap
1263 * insert tries to happen, instead trimming the LAST
1264 * block from the directory.
1265 */
1266 resblks = XFS_REMOVE_SPACE_RES(mp);
1267 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001268 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (error == ENOSPC) {
1270 resblks = 0;
1271 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001272 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274 if (error) {
1275 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001276 cancel_flags = 0;
1277 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001280 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Christoph Hellwigddc34152011-09-19 15:00:54 +00001282 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1283 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001286 * If we're removing a directory perform some additional validation.
1287 */
1288 if (is_dir) {
1289 ASSERT(ip->i_d.di_nlink >= 2);
1290 if (ip->i_d.di_nlink != 2) {
1291 error = XFS_ERROR(ENOTEMPTY);
1292 goto out_trans_cancel;
1293 }
1294 if (!xfs_dir_isempty(ip)) {
1295 error = XFS_ERROR(ENOTEMPTY);
1296 goto out_trans_cancel;
1297 }
1298 }
1299
Eric Sandeen9d87c312009-01-14 23:22:07 -06001300 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001301 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001302 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (error) {
1304 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001305 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001307 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001309 if (is_dir) {
1310 /*
1311 * Drop the link from ip's "..".
1312 */
1313 error = xfs_droplink(tp, dp);
1314 if (error)
1315 goto out_bmap_cancel;
1316
1317 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001318 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001319 */
1320 error = xfs_droplink(tp, ip);
1321 if (error)
1322 goto out_bmap_cancel;
1323 } else {
1324 /*
1325 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001326 * inode here. For a directory this is done implicitly
1327 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001328 */
1329 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001332 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001333 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001334 */
1335 error = xfs_droplink(tp, ip);
1336 if (error)
1337 goto out_bmap_cancel;
1338
1339 /*
1340 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * we are in the transaction.
1342 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001343 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * If this is a synchronous mount, make sure that the
1347 * remove transaction goes to disk before returning to
1348 * the user.
1349 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001350 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001353 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001354 if (error)
1355 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001357 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001358 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001362 * If we are using filestreams, kill the stream association.
1363 * If the file is still open it may get a new one but that
1364 * will get killed on last close in xfs_close() so we don't
1365 * have to worry about that.
1366 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001367 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001368 xfs_filestream_deassociate(ip);
1369
Christoph Hellwig288699f2010-06-23 18:11:15 +10001370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001372 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 xfs_bmap_cancel(&free_list);
1374 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001375 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001377 std_return:
1378 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Christoph Hellwig993386c2007-08-28 16:12:30 +10001381int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001383 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001384 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10001385 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001387 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int error;
1390 xfs_bmap_free_t free_list;
1391 xfs_fsblock_t first_block;
1392 int cancel_flags;
1393 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001396 trace_xfs_link(tdp, target_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001398 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (XFS_FORCED_SHUTDOWN(mp))
1401 return XFS_ERROR(EIO);
1402
Christoph Hellwig7d095252009-06-08 15:33:32 +02001403 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01001404 if (error)
1405 goto std_return;
1406
Christoph Hellwig7d095252009-06-08 15:33:32 +02001407 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (error)
1409 goto std_return;
1410
1411 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1412 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001413 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1415 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1416 if (error == ENOSPC) {
1417 resblks = 0;
1418 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1419 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1420 }
1421 if (error) {
1422 cancel_flags = 0;
1423 goto error_return;
1424 }
1425
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001426 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Christoph Hellwigddc34152011-09-19 15:00:54 +00001428 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1429 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /*
1432 * If the source has too many links, we can't make any more to it.
1433 */
1434 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1435 error = XFS_ERROR(EMLINK);
1436 goto error_return;
1437 }
1438
Nathan Scott365ca832005-06-21 15:39:12 +10001439 /*
1440 * If we are using project inheritance, we only allow hard link
1441 * creation in our tree when the project IDs are the same; else
1442 * the tree quota mechanism could be circumvented.
1443 */
1444 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001445 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10001446 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10001447 goto error_return;
1448 }
1449
Barry Naujok556b8b12008-04-10 12:22:07 +10001450 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1451 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 goto error_return;
1453
Eric Sandeen9d87c312009-01-14 23:22:07 -06001454 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Barry Naujok556b8b12008-04-10 12:22:07 +10001456 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1457 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (error)
1459 goto abort_return;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001460 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1462
1463 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001464 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 /*
1468 * If this is a synchronous mount, make sure that the
1469 * link transaction goes to disk before returning to
1470 * the user.
1471 */
1472 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1473 xfs_trans_set_sync(tp);
1474 }
1475
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001476 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (error) {
1478 xfs_bmap_cancel(&free_list);
1479 goto abort_return;
1480 }
1481
Christoph Hellwig288699f2010-06-23 18:11:15 +10001482 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 abort_return:
1485 cancel_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 error_return:
1487 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001488 std_return:
1489 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001491
Christoph Hellwig993386c2007-08-28 16:12:30 +10001492int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001494 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001495 struct xfs_name *link_name,
1496 const char *target_path,
Al Viro576b1d62011-07-26 02:50:15 -04001497 umode_t mode,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001498 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001500 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 xfs_inode_t *ip;
1503 int error;
1504 int pathlen;
1505 xfs_bmap_free_t free_list;
1506 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001507 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 uint cancel_flags;
1509 int committed;
1510 xfs_fileoff_t first_fsb;
1511 xfs_filblks_t fs_blocks;
1512 int nmaps;
1513 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1514 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10001515 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int byte_cnt;
1517 int n;
1518 xfs_buf_t *bp;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001519 prid_t prid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 struct xfs_dquot *udqp, *gdqp;
1521 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Christoph Hellwig3937be52008-03-06 13:46:19 +11001523 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 error = 0;
1525 ip = NULL;
1526 tp = NULL;
1527
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001528 trace_xfs_symlink(dp, link_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (XFS_FORCED_SHUTDOWN(mp))
1531 return XFS_ERROR(EIO);
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /*
1534 * Check component lengths of the target path name.
1535 */
1536 pathlen = strlen(target_path);
1537 if (pathlen >= MAXPATHLEN) /* total string too long */
1538 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001541 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001542 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001544 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 /*
1547 * Make sure that we have allocated dquot(s) on disk.
1548 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001549 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1551 if (error)
1552 goto std_return;
1553
1554 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1555 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1556 /*
1557 * The symlink will fit into the inode data fork?
1558 * There can't be any attributes so we get the whole variable part.
1559 */
1560 if (pathlen <= XFS_LITINO(mp))
1561 fs_blocks = 0;
1562 else
1563 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10001564 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
1566 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1567 if (error == ENOSPC && fs_blocks == 0) {
1568 resblks = 0;
1569 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
1570 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1571 }
1572 if (error) {
1573 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 goto error_return;
1575 }
1576
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001577 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001578 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /*
1581 * Check whether the directory allows new symlinks or not.
1582 */
1583 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
1584 error = XFS_ERROR(EPERM);
1585 goto error_return;
1586 }
1587
1588 /*
1589 * Reserve disk quota : blocks and inode.
1590 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001591 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (error)
1593 goto error_return;
1594
1595 /*
1596 * Check for ability to enter directory entry, if no space reserved.
1597 */
Barry Naujok556b8b12008-04-10 12:22:07 +10001598 error = xfs_dir_canenter(tp, dp, link_name, resblks);
1599 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 goto error_return;
1601 /*
1602 * Initialize the bmap freelist prior to calling either
1603 * bmapi or the directory create code.
1604 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001605 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /*
1608 * Allocate an inode for the symlink.
1609 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001610 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
1611 prid, resblks > 0, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (error) {
1613 if (error == ENOSPC)
1614 goto error_return;
1615 goto error1;
1616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Christoph Hellwig993386c2007-08-28 16:12:30 +10001618 /*
1619 * An error after we've joined dp to the transaction will result in the
1620 * transaction cancel unlocking dp so don't do it explicitly in the
1621 * error path.
1622 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001623 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001624 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /*
1627 * Also attach the dquot(s) to it, if applicable.
1628 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001629 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if (resblks)
1632 resblks -= XFS_IALLOC_SPACE_RES(mp);
1633 /*
1634 * If the symlink will fit into the inode, write it inline.
1635 */
1636 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
1637 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
1638 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
1639 ip->i_d.di_size = pathlen;
1640
1641 /*
1642 * The inode was initially created in extent format.
1643 */
1644 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
1645 ip->i_df.if_flags |= XFS_IFINLINE;
1646
1647 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
1648 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
1649
1650 } else {
1651 first_fsb = 0;
1652 nmaps = SYMLINK_MAPS;
1653
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001654 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
1655 XFS_BMAPI_METADATA, &first_block, resblks,
1656 mval, &nmaps, &free_list);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001657 if (error)
1658 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 if (resblks)
1661 resblks -= fs_blocks;
1662 ip->i_d.di_size = pathlen;
1663 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1664
1665 cur_chunk = target_path;
1666 for (n = 0; n < nmaps; n++) {
1667 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1668 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1669 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
1670 BTOBB(byte_cnt), 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001671 if (!bp) {
1672 error = ENOMEM;
1673 goto error2;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (pathlen < byte_cnt) {
1676 byte_cnt = pathlen;
1677 }
1678 pathlen -= byte_cnt;
1679
Chandra Seetharaman62926042011-07-22 23:40:15 +00001680 memcpy(bp->b_addr, cur_chunk, byte_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 cur_chunk += byte_cnt;
1682
1683 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
1684 }
1685 }
1686
1687 /*
1688 * Create the directory entry for the symlink.
1689 */
Barry Naujok556b8b12008-04-10 12:22:07 +10001690 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
1691 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001692 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001693 goto error2;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001694 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1696
1697 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 * If this is a synchronous mount, make sure that the
1699 * symlink transaction goes to disk before returning to
1700 * the user.
1701 */
1702 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1703 xfs_trans_set_sync(tp);
1704 }
1705
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001706 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (error) {
1708 goto error2;
1709 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001710 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001711 xfs_qm_dqrele(udqp);
1712 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Christoph Hellwig288699f2010-06-23 18:11:15 +10001714 *ipp = ip;
1715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 error2:
1718 IRELE(ip);
1719 error1:
1720 xfs_bmap_cancel(&free_list);
1721 cancel_flags |= XFS_TRANS_ABORT;
1722 error_return:
1723 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001724 xfs_qm_dqrele(udqp);
1725 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Christoph Hellwig993386c2007-08-28 16:12:30 +10001727 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001729 std_return:
1730 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733int
Christoph Hellwig993386c2007-08-28 16:12:30 +10001734xfs_set_dmattrs(
1735 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10001737 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001739 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int error;
1742
1743 if (!capable(CAP_SYS_ADMIN))
1744 return XFS_ERROR(EPERM);
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (XFS_FORCED_SHUTDOWN(mp))
1747 return XFS_ERROR(EIO);
1748
1749 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1750 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1751 if (error) {
1752 xfs_trans_cancel(tp, 0);
1753 return error;
1754 }
1755 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001756 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Christoph Hellwig613d7042007-10-11 17:44:08 +10001758 ip->i_d.di_dmevmask = evmask;
1759 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001762 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 return error;
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/*
1768 * xfs_alloc_file_space()
1769 * This routine allocates disk space for the given file.
1770 *
1771 * If alloc_type == 0, this request is for an ALLOCSP type
1772 * request which will change the file size. In this case, no
1773 * DMAPI event will be generated by the call. A TRUNCATE event
1774 * will be generated later by xfs_setattr.
1775 *
1776 * If alloc_type != 0, this request is for a RESVSP type
1777 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1778 * lower block boundary byte address is less than the file's
1779 * length.
1780 *
1781 * RETURNS:
1782 * 0 on success
1783 * errno on error
1784 *
1785 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001786STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787xfs_alloc_file_space(
1788 xfs_inode_t *ip,
1789 xfs_off_t offset,
1790 xfs_off_t len,
1791 int alloc_type,
1792 int attr_flags)
1793{
Nathan Scottdd9f4382006-01-11 15:28:28 +11001794 xfs_mount_t *mp = ip->i_mount;
1795 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 xfs_filblks_t allocated_fsb;
1797 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001798 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001800 xfs_fsblock_t firstfsb;
1801 int nimaps;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001802 int quota_flag;
1803 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001805 xfs_bmbt_irec_t imaps[1], *imapp;
1806 xfs_bmap_free_t free_list;
1807 uint qblocks, resblks, resrtextents;
1808 int committed;
1809 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001811 trace_xfs_alloc_file_space(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 if (XFS_FORCED_SHUTDOWN(mp))
1814 return XFS_ERROR(EIO);
1815
Christoph Hellwig7d095252009-06-08 15:33:32 +02001816 error = xfs_qm_dqattach(ip, 0);
1817 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return error;
1819
1820 if (len <= 0)
1821 return XFS_ERROR(EINVAL);
1822
David Chinner957d0eb2007-06-18 16:50:37 +10001823 rt = XFS_IS_REALTIME_INODE(ip);
1824 extsz = xfs_get_extsz_hint(ip);
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11001828 nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1830 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001833 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001836 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Nathan Scottdd9f4382006-01-11 15:28:28 +11001838 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11001839 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11001840 */
1841 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001843 do_div(s, extsz);
1844 s *= extsz;
1845 e = startoffset_fsb + allocatesize_fsb;
1846 if ((temp = do_mod(startoffset_fsb, extsz)))
1847 e += temp;
1848 if ((temp = do_mod(e, extsz)))
1849 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001851 s = 0;
1852 e = allocatesize_fsb;
1853 }
1854
Dave Chinner72656c42010-09-03 12:19:33 +10001855 /*
1856 * The transaction reservation is limited to a 32-bit block
1857 * count, hence we need to limit the number of blocks we are
1858 * trying to reserve to avoid an overflow. We can't allocate
1859 * more than @nimaps extents, and an extent is limited on disk
1860 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1861 */
1862 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
Nathan Scottdd9f4382006-01-11 15:28:28 +11001863 if (unlikely(rt)) {
Dave Chinner72656c42010-09-03 12:19:33 +10001864 resrtextents = qblocks = resblks;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001865 resrtextents /= mp->m_sb.sb_rextsize;
1866 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1867 quota_flag = XFS_QMOPT_RES_RTBLKS;
1868 } else {
1869 resrtextents = 0;
Dave Chinner72656c42010-09-03 12:19:33 +10001870 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001871 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873
1874 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001875 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
1877 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001878 error = xfs_trans_reserve(tp, resblks,
1879 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 XFS_TRANS_PERM_LOG_RES,
1881 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001883 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
1885 if (error) {
1886 /*
1887 * Free the transaction structure.
1888 */
1889 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1890 xfs_trans_cancel(tp, 0);
1891 break;
1892 }
1893 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001894 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1895 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (error)
1897 goto error1;
1898
Christoph Hellwigddc34152011-09-19 15:00:54 +00001899 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Eric Sandeen9d87c312009-01-14 23:22:07 -06001901 xfs_bmap_init(&free_list, &firstfsb);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001902 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1903 allocatesize_fsb, alloc_type, &firstfsb,
1904 0, imapp, &nimaps, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (error) {
1906 goto error0;
1907 }
1908
1909 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001910 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001912 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (error) {
1914 goto error0;
1915 }
1916
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001917 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1919 if (error) {
1920 break;
1921 }
1922
1923 allocated_fsb = imapp->br_blockcount;
1924
Nathan Scottdd9f4382006-01-11 15:28:28 +11001925 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 error = XFS_ERROR(ENOSPC);
1927 break;
1928 }
1929
1930 startoffset_fsb += allocated_fsb;
1931 allocatesize_fsb -= allocated_fsb;
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 return error;
1935
Nathan Scottdd9f4382006-01-11 15:28:28 +11001936error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001938 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001939
1940error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1942 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946/*
1947 * Zero file bytes between startoff and endoff inclusive.
1948 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001949 *
1950 * This function is used by xfs_free_file_space() to zero
1951 * partial blocks when the range to free is not block aligned.
1952 * When unreserving space with boundaries that are not block
1953 * aligned we round up the start and round down the end
1954 * boundaries and then use this function to zero the parts of
1955 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 */
1957STATIC int
1958xfs_zero_remaining_bytes(
1959 xfs_inode_t *ip,
1960 xfs_off_t startoff,
1961 xfs_off_t endoff)
1962{
1963 xfs_bmbt_irec_t imap;
1964 xfs_fileoff_t offset_fsb;
1965 xfs_off_t lastoffset;
1966 xfs_off_t offset;
1967 xfs_buf_t *bp;
1968 xfs_mount_t *mp = ip->i_mount;
1969 int nimap;
1970 int error = 0;
1971
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001972 /*
1973 * Avoid doing I/O beyond eof - it's not necessary
1974 * since nothing can read beyond eof. The space will
1975 * be zeroed when the file is extended anyway.
1976 */
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001977 if (startoff >= XFS_ISIZE(ip))
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001978 return 0;
1979
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001980 if (endoff > XFS_ISIZE(ip))
1981 endoff = XFS_ISIZE(ip);
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001982
Dave Chinner686865f2010-09-24 20:07:47 +10001983 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1984 mp->m_rtdev_targp : mp->m_ddev_targp,
1985 mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11001986 if (!bp)
1987 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001989 xfs_buf_unlock(bp);
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1992 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1993 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00001994 error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (error || nimap < 1)
1996 break;
1997 ASSERT(imap.br_blockcount >= 1);
1998 ASSERT(imap.br_startoff == offset_fsb);
1999 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2000 if (lastoffset > endoff)
2001 lastoffset = endoff;
2002 if (imap.br_startblock == HOLESTARTBLOCK)
2003 continue;
2004 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2005 if (imap.br_state == XFS_EXT_UNWRITTEN)
2006 continue;
2007 XFS_BUF_UNDONE(bp);
2008 XFS_BUF_UNWRITE(bp);
2009 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002010 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002012 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002013 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00002014 xfs_buf_ioerror_alert(bp,
2015 "xfs_zero_remaining_bytes(read)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 break;
2017 }
Chandra Seetharaman62926042011-07-22 23:40:15 +00002018 memset(bp->b_addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2020 0, lastoffset - offset + 1);
2021 XFS_BUF_UNDONE(bp);
2022 XFS_BUF_UNREAD(bp);
2023 XFS_BUF_WRITE(bp);
2024 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002025 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002026 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00002027 xfs_buf_ioerror_alert(bp,
2028 "xfs_zero_remaining_bytes(write)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
2030 }
2031 }
2032 xfs_buf_free(bp);
2033 return error;
2034}
2035
2036/*
2037 * xfs_free_file_space()
2038 * This routine frees disk space for the given file.
2039 *
2040 * This routine is only called by xfs_change_file_space
2041 * for an UNRESVSP type call.
2042 *
2043 * RETURNS:
2044 * 0 on success
2045 * errno on error
2046 *
2047 */
2048STATIC int
2049xfs_free_file_space(
2050 xfs_inode_t *ip,
2051 xfs_off_t offset,
2052 xfs_off_t len,
2053 int attr_flags)
2054{
2055 int committed;
2056 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 xfs_fileoff_t endoffset_fsb;
2058 int error;
2059 xfs_fsblock_t firstfsb;
2060 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 xfs_bmbt_irec_t imap;
2062 xfs_off_t ioffset;
2063 xfs_extlen_t mod=0;
2064 xfs_mount_t *mp;
2065 int nimap;
2066 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002067 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int rt;
2069 xfs_fileoff_t startoffset_fsb;
2070 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002071 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 mp = ip->i_mount;
2074
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002075 trace_xfs_free_file_space(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002076
Christoph Hellwig7d095252009-06-08 15:33:32 +02002077 error = xfs_qm_dqattach(ip, 0);
2078 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return error;
2080
2081 error = 0;
2082 if (len <= 0) /* if nothing being freed */
2083 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002084 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002086 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002088 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002089 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002090 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002092 /* wait for the completion of any pending DIOs */
Christoph Hellwig4a06fd22011-08-23 08:28:13 +00002093 inode_dio_wait(VFS_I(ip));
Yingping Lu9fa80462006-03-22 12:44:35 +11002094 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002095
Tim Shimmine6a4b372007-11-23 16:30:42 +11002096 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002098
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002099 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002100 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002101 if (error)
2102 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002103 }
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /*
2106 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002107 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2109 * will take care of it for us.
2110 */
Eric Sandeen62118702008-03-06 13:44:28 +11002111 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00002113 error = xfs_bmapi_read(ip, startoffset_fsb, 1,
2114 &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (error)
2116 goto out_unlock_iolock;
2117 ASSERT(nimap == 0 || nimap == 1);
2118 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2119 xfs_daddr_t block;
2120
2121 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2122 block = imap.br_startblock;
2123 mod = do_div(block, mp->m_sb.sb_rextsize);
2124 if (mod)
2125 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2126 }
2127 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00002128 error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
2129 &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (error)
2131 goto out_unlock_iolock;
2132 ASSERT(nimap == 0 || nimap == 1);
2133 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2134 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2135 mod++;
2136 if (mod && (mod != mp->m_sb.sb_rextsize))
2137 endoffset_fsb -= mod;
2138 }
2139 }
2140 if ((done = (endoffset_fsb <= startoffset_fsb)))
2141 /*
2142 * One contiguous piece to clear
2143 */
2144 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2145 else {
2146 /*
2147 * Some full blocks, possibly two pieces to clear
2148 */
2149 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2150 error = xfs_zero_remaining_bytes(ip, offset,
2151 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2152 if (!error &&
2153 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2154 error = xfs_zero_remaining_bytes(ip,
2155 XFS_FSB_TO_B(mp, endoffset_fsb),
2156 offset + len - 1);
2157 }
2158
2159 /*
2160 * free file space until done or until there is an error
2161 */
2162 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2163 while (!error && !done) {
2164
2165 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002166 * allocate and setup the transaction. Allow this
2167 * transaction to dip into the reserve blocks to ensure
2168 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 */
2170 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002171 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 error = xfs_trans_reserve(tp,
2173 resblks,
2174 XFS_WRITE_LOG_RES(mp),
2175 0,
2176 XFS_TRANS_PERM_LOG_RES,
2177 XFS_WRITE_LOG_COUNT);
2178
2179 /*
2180 * check for running out of space
2181 */
2182 if (error) {
2183 /*
2184 * Free the transaction structure.
2185 */
2186 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2187 xfs_trans_cancel(tp, 0);
2188 break;
2189 }
2190 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002191 error = xfs_trans_reserve_quota(tp, mp,
2192 ip->i_udquot, ip->i_gdquot,
2193 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (error)
2195 goto error1;
2196
Christoph Hellwigddc34152011-09-19 15:00:54 +00002197 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 /*
2200 * issue the bunmapi() call to free the blocks
2201 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002202 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002203 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 endoffset_fsb - startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002205 0, 2, &firstfsb, &free_list, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (error) {
2207 goto error0;
2208 }
2209
2210 /*
2211 * complete the transaction
2212 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002213 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (error) {
2215 goto error0;
2216 }
2217
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002218 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2220 }
2221
2222 out_unlock_iolock:
2223 if (need_iolock)
2224 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2225 return error;
2226
2227 error0:
2228 xfs_bmap_cancel(&free_list);
2229 error1:
2230 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2231 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2232 XFS_ILOCK_EXCL);
2233 return error;
2234}
2235
2236/*
2237 * xfs_change_file_space()
2238 * This routine allocates or frees disk space for the given file.
2239 * The user specified parameters are checked for alignment and size
2240 * limitations.
2241 *
2242 * RETURNS:
2243 * 0 on success
2244 * errno on error
2245 *
2246 */
2247int
2248xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002249 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 int cmd,
2251 xfs_flock64_t *bf,
2252 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 int attr_flags)
2254{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002255 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int clrprealloc;
2257 int error;
2258 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int setprealloc;
2260 xfs_off_t startoffset;
2261 xfs_off_t llen;
2262 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002263 struct iattr iattr;
Dave Chinner44722352010-08-24 12:02:11 +10002264 int prealloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Christoph Hellwig993386c2007-08-28 16:12:30 +10002266 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return XFS_ERROR(EINVAL);
2268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 switch (bf->l_whence) {
2270 case 0: /*SEEK_SET*/
2271 break;
2272 case 1: /*SEEK_CUR*/
2273 bf->l_start += offset;
2274 break;
2275 case 2: /*SEEK_END*/
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00002276 bf->l_start += XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 break;
2278 default:
2279 return XFS_ERROR(EINVAL);
2280 }
2281
2282 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2283
2284 if ( (bf->l_start < 0)
2285 || (bf->l_start > XFS_MAXIOFFSET(mp))
2286 || (bf->l_start + llen < 0)
2287 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2288 return XFS_ERROR(EINVAL);
2289
2290 bf->l_whence = 0;
2291
2292 startoffset = bf->l_start;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00002293 fsize = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /*
2296 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2297 * file space.
2298 * These calls do NOT zero the data space allocated to the file,
2299 * nor do they change the file size.
2300 *
2301 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2302 * space.
2303 * These calls cause the new file data to be zeroed and the file
2304 * size to be changed.
2305 */
2306 setprealloc = clrprealloc = 0;
Dave Chinner44722352010-08-24 12:02:11 +10002307 prealloc_type = XFS_BMAPI_PREALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 switch (cmd) {
Dave Chinner44722352010-08-24 12:02:11 +10002310 case XFS_IOC_ZERO_RANGE:
2311 prealloc_type |= XFS_BMAPI_CONVERT;
2312 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2313 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 case XFS_IOC_RESVSP:
2315 case XFS_IOC_RESVSP64:
2316 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
Dave Chinner44722352010-08-24 12:02:11 +10002317 prealloc_type, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (error)
2319 return error;
2320 setprealloc = 1;
2321 break;
2322
2323 case XFS_IOC_UNRESVSP:
2324 case XFS_IOC_UNRESVSP64:
2325 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2326 attr_flags)))
2327 return error;
2328 break;
2329
2330 case XFS_IOC_ALLOCSP:
2331 case XFS_IOC_ALLOCSP64:
2332 case XFS_IOC_FREESP:
2333 case XFS_IOC_FREESP64:
2334 if (startoffset > fsize) {
2335 error = xfs_alloc_file_space(ip, fsize,
2336 startoffset - fsize, 0, attr_flags);
2337 if (error)
2338 break;
2339 }
2340
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002341 iattr.ia_valid = ATTR_SIZE;
2342 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02002344 error = xfs_setattr_size(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 if (error)
2347 return error;
2348
2349 clrprealloc = 1;
2350 break;
2351
2352 default:
2353 ASSERT(0);
2354 return XFS_ERROR(EINVAL);
2355 }
2356
2357 /*
2358 * update the inode timestamp, mode, and prealloc flag bits
2359 */
2360 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2361
2362 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2363 0, 0, 0))) {
2364 /* ASSERT(0); */
2365 xfs_trans_cancel(tp, 0);
2366 return error;
2367 }
2368
2369 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00002370 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002372 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 ip->i_d.di_mode &= ~S_ISUID;
2374
2375 /*
2376 * Note that we don't have to worry about mandatory
2377 * file locking being disabled here because we only
2378 * clear the S_ISGID bit if the Group execute bit is
2379 * on, but if it was on then mandatory locking wouldn't
2380 * have been enabled.
2381 */
2382 if (ip->i_d.di_mode & S_IXGRP)
2383 ip->i_d.di_mode &= ~S_ISGID;
2384
Dave Chinnerdcd79a12010-09-28 12:27:25 +10002385 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387 if (setprealloc)
2388 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2389 else if (clrprealloc)
2390 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2391
2392 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinner82878892011-03-26 09:13:08 +11002393 if (attr_flags & XFS_ATTR_SYNC)
2394 xfs_trans_set_sync(tp);
Christoph Hellwig23bb0be2011-09-18 20:47:51 +00002395 return xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}