blob: 9e8266981f473b868cfa5dafe972db526cdc9c69 [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"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
31#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_dinode.h"
33#include "xfs_inode.h"
34#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_ialloc.h"
37#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_bmap.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020039#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_attr.h"
41#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_quota.h"
44#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110045#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100048#include "xfs_filestream.h"
Christoph Hellwig993386c12007-08-28 16:12:30 +100049#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Nathan Scott7d4fb402006-06-09 15:27:16 +100053 * The maximum pathlen is 1024 bytes. Since the minimum file system
54 * blocksize is 512 bytes, we can get a max of 2 extents back from
55 * bmapi.
56 */
57#define SYMLINK_MAPS 2
58
Christoph Hellwig804c83c2007-08-28 13:59:03 +100059STATIC int
60xfs_readlink_bmap(
61 xfs_inode_t *ip,
62 char *link)
63{
64 xfs_mount_t *mp = ip->i_mount;
65 int pathlen = ip->i_d.di_size;
66 int nmaps = SYMLINK_MAPS;
67 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
68 xfs_daddr_t d;
69 int byte_cnt;
70 int n;
71 xfs_buf_t *bp;
72 int error = 0;
73
Dave Chinner5c8ed202011-09-18 20:40:45 +000074 error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, pathlen), mval, &nmaps,
75 0);
Christoph Hellwig804c83c2007-08-28 13:59:03 +100076 if (error)
77 goto out;
78
79 for (n = 0; n < nmaps; n++) {
80 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
81 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
82
Christoph Hellwig6ad112b2009-11-24 18:02:23 +000083 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
Dave Chinneraa5c1582012-04-23 15:58:56 +100084 XBF_MAPPED);
Chandra Seetharamanac4d6882011-08-03 02:18:29 +000085 if (!bp)
86 return XFS_ERROR(ENOMEM);
Chandra Seetharamane5702802011-08-03 02:18:34 +000087 error = bp->b_error;
Christoph Hellwig804c83c2007-08-28 13:59:03 +100088 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +000089 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwig804c83c2007-08-28 13:59:03 +100090 xfs_buf_relse(bp);
91 goto out;
92 }
93 if (pathlen < byte_cnt)
94 byte_cnt = pathlen;
95 pathlen -= byte_cnt;
96
Chandra Seetharaman62926042011-07-22 23:40:15 +000097 memcpy(link, bp->b_addr, byte_cnt);
Christoph Hellwig804c83c2007-08-28 13:59:03 +100098 xfs_buf_relse(bp);
99 }
100
101 link[ip->i_d.di_size] = '\0';
102 error = 0;
103
104 out:
105 return error;
106}
107
Christoph Hellwig993386c12007-08-28 16:12:30 +1000108int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109xfs_readlink(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000110 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000111 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000113 xfs_mount_t *mp = ip->i_mount;
Carlos Maiolinob52a3602011-11-07 16:10:24 +0000114 xfs_fsize_t pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000117 trace_xfs_readlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (XFS_FORCED_SHUTDOWN(mp))
120 return XFS_ERROR(EIO);
121
122 xfs_ilock(ip, XFS_ILOCK_SHARED);
123
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000124 pathlen = ip->i_d.di_size;
125 if (!pathlen)
126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Carlos Maiolinob52a3602011-11-07 16:10:24 +0000128 if (pathlen < 0 || pathlen > MAXPATHLEN) {
129 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
130 __func__, (unsigned long long) ip->i_ino,
131 (long long) pathlen);
132 ASSERT(0);
Jan Kara9b025eb2012-01-11 18:52:10 +0000133 error = XFS_ERROR(EFSCORRUPTED);
134 goto out;
Carlos Maiolinob52a3602011-11-07 16:10:24 +0000135 }
136
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000139 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
140 link[pathlen] = '\0';
141 } else {
142 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000145 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return error;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000151 * Flags for xfs_free_eofblocks
152 */
153#define XFS_FREE_EOF_TRYLOCK (1<<0)
154
155/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000156 * This is called by xfs_inactive to free any blocks beyond eof
157 * when the link count isn't zero and by xfs_dm_punch_hole() when
158 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500160STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000161xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000163 xfs_inode_t *ip,
164 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 xfs_trans_t *tp;
167 int error;
168 xfs_fileoff_t end_fsb;
169 xfs_fileoff_t last_fsb;
170 xfs_filblks_t map_len;
171 int nimaps;
172 xfs_bmbt_irec_t imap;
173
174 /*
175 * Figure out if there are any blocks beyond the end
176 * of the file. If not, then there is nothing to do.
177 */
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000178 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000180 if (last_fsb <= end_fsb)
Jesper Juhl014c2542006-01-15 02:37:08 +0100181 return 0;
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000182 map_len = last_fsb - end_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 nimaps = 1;
185 xfs_ilock(ip, XFS_ILOCK_SHARED);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000186 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 xfs_iunlock(ip, XFS_ILOCK_SHARED);
188
189 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100190 (imap.br_startblock != HOLESTARTBLOCK ||
191 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Attach the dquots to the inode up front.
194 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200195 error = xfs_qm_dqattach(ip, 0);
196 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100197 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /*
200 * There are blocks after the end of file.
201 * Free them up now by truncating the file to
202 * its current size.
203 */
204 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
205
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000206 if (flags & XFS_FREE_EOF_TRYLOCK) {
207 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
208 xfs_trans_cancel(tp, 0);
209 return 0;
210 }
211 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000212 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 error = xfs_trans_reserve(tp, 0,
216 XFS_ITRUNCATE_LOG_RES(mp),
217 0, XFS_TRANS_PERM_LOG_RES,
218 XFS_ITRUNCATE_LOG_COUNT);
219 if (error) {
220 ASSERT(XFS_FORCED_SHUTDOWN(mp));
221 xfs_trans_cancel(tp, 0);
222 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100223 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000227 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000229 /*
230 * Do not update the on-disk file size. If we update the
231 * on-disk file size and then the system crashes before the
232 * contents of the file are flushed to disk then the files
233 * may be full of holes (ie NULL files bug).
234 */
235 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000236 XFS_ISIZE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (error) {
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200238 /*
239 * If we get an error at this point we simply don't
240 * bother truncating the file.
241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfs_trans_cancel(tp,
243 (XFS_TRANS_RELEASE_LOG_RES |
244 XFS_TRANS_ABORT));
245 } else {
246 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000247 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000249 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100251 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/*
255 * Free a symlink that has blocks associated with it.
256 */
257STATIC int
258xfs_inactive_symlink_rmt(
259 xfs_inode_t *ip,
260 xfs_trans_t **tpp)
261{
262 xfs_buf_t *bp;
263 int committed;
264 int done;
265 int error;
266 xfs_fsblock_t first_block;
267 xfs_bmap_free_t free_list;
268 int i;
269 xfs_mount_t *mp;
270 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
271 int nmaps;
272 xfs_trans_t *ntp;
273 int size;
274 xfs_trans_t *tp;
275
276 tp = *tpp;
277 mp = ip->i_mount;
278 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
279 /*
280 * We're freeing a symlink that has some
281 * blocks allocated to it. Free the
282 * blocks here. We know that we've got
283 * either 1 or 2 extents and that we can
284 * free them all in one bunmapi call.
285 */
286 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
287 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
288 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
289 ASSERT(XFS_FORCED_SHUTDOWN(mp));
290 xfs_trans_cancel(tp, 0);
291 *tpp = NULL;
292 return error;
293 }
294 /*
295 * Lock the inode, fix the size, and join it to the transaction.
296 * Hold it so in the normal path, we still have it locked for
297 * the second transaction. In the error paths we need it
298 * held so the cancel won't rele it, see below.
299 */
300 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
301 size = (int)ip->i_d.di_size;
302 ip->i_d.di_size = 0;
Christoph Hellwigddc34152011-09-19 15:00:54 +0000303 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
305 /*
306 * Find the block(s) so we can inval and unmap them.
307 */
308 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600309 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800310 nmaps = ARRAY_SIZE(mval);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000311 error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, size),
312 mval, &nmaps, 0);
313 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto error0;
315 /*
316 * Invalidate the block(s).
317 */
318 for (i = 0; i < nmaps; i++) {
319 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
320 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
321 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000322 if (!bp) {
323 error = ENOMEM;
324 goto error1;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 xfs_trans_binval(tp, bp);
327 }
328 /*
329 * Unmap the dead block(s) to the free_list.
330 */
331 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000332 &first_block, &free_list, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 goto error1;
334 ASSERT(done);
335 /*
336 * Commit the first transaction. This logs the EFI and the inode.
337 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100338 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 goto error1;
340 /*
341 * The transaction must have been committed, since there were
342 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
343 * The new tp has the extent freeing and EFDs.
344 */
345 ASSERT(committed);
346 /*
347 * The first xact was committed, so add the inode to the new one.
348 * Mark it dirty so it will be logged and moved forward in the log as
349 * part of every commit.
350 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000351 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
353 /*
354 * Get a new, empty transaction to return to our caller.
355 */
356 ntp = xfs_trans_dup(tp);
357 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000358 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * If we get an error on the commit here or on the reserve below,
360 * we need to unlock the inode since the new transaction doesn't
361 * have the inode attached.
362 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000363 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 tp = ntp;
365 if (error) {
366 ASSERT(XFS_FORCED_SHUTDOWN(mp));
367 goto error0;
368 }
369 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100370 * transaction commit worked ok so we can drop the extra ticket
371 * reference that we gained in xfs_trans_dup()
372 */
373 xfs_log_ticket_put(tp->t_ticket);
374
375 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * Remove the memory for extent descriptions (just bookkeeping).
377 */
378 if (ip->i_df.if_bytes)
379 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
380 ASSERT(ip->i_df.if_bytes == 0);
381 /*
382 * Put an itruncate log reservation in the new transaction
383 * for our caller.
384 */
385 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
386 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
387 ASSERT(XFS_FORCED_SHUTDOWN(mp));
388 goto error0;
389 }
390 /*
391 * Return with the inode locked but not joined to the transaction.
392 */
393 *tpp = tp;
394 return 0;
395
396 error1:
397 xfs_bmap_cancel(&free_list);
398 error0:
399 /*
400 * Have to come here with the inode locked and either
401 * (held and in the transaction) or (not in the transaction).
402 * If the inode isn't held then cancel would iput it, but
403 * that's wrong since this is inactive and the vnode ref
404 * count is 0 already.
405 * Cancel won't do anything to the inode if held, but it still
406 * needs to be locked until the cancel is done, if it was
407 * joined to the transaction.
408 */
409 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
410 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
411 *tpp = NULL;
412 return error;
413
414}
415
416STATIC int
417xfs_inactive_symlink_local(
418 xfs_inode_t *ip,
419 xfs_trans_t **tpp)
420{
421 int error;
422
423 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
424 /*
425 * We're freeing a symlink which fit into
426 * the inode. Just free the memory used
427 * to hold the old symlink.
428 */
429 error = xfs_trans_reserve(*tpp, 0,
430 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
431 0, XFS_TRANS_PERM_LOG_RES,
432 XFS_ITRUNCATE_LOG_COUNT);
433
434 if (error) {
435 xfs_trans_cancel(*tpp, 0);
436 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100437 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
440
441 /*
442 * Zero length symlinks _can_ exist.
443 */
444 if (ip->i_df.if_bytes > 0) {
445 xfs_idata_realloc(ip,
446 -(ip->i_df.if_bytes),
447 XFS_DATA_FORK);
448 ASSERT(ip->i_df.if_bytes == 0);
449 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453STATIC int
454xfs_inactive_attrs(
455 xfs_inode_t *ip,
456 xfs_trans_t **tpp)
457{
458 xfs_trans_t *tp;
459 int error;
460 xfs_mount_t *mp;
461
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000462 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 tp = *tpp;
464 mp = ip->i_mount;
465 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +1000466 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +1000468 if (error)
469 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +1000472 if (error)
473 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
476 error = xfs_trans_reserve(tp, 0,
477 XFS_IFREE_LOG_RES(mp),
478 0, XFS_TRANS_PERM_LOG_RES,
479 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +1000480 if (error)
481 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000484 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
486
487 ASSERT(ip->i_d.di_anextents == 0);
488
489 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +0100490 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +1000491
492error_cancel:
493 ASSERT(XFS_FORCED_SHUTDOWN(mp));
494 xfs_trans_cancel(tp, 0);
495error_unlock:
496 *tpp = NULL;
497 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
498 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Christoph Hellwig993386c12007-08-28 16:12:30 +1000501int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502xfs_release(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000503 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Christoph Hellwig993386c12007-08-28 16:12:30 +1000505 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int error;
507
Christoph Hellwig42173f62008-04-22 17:33:25 +1000508 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000512 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
514
David Chinner2a82b8b2007-07-11 11:09:12 +1000515 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000516 int truncated;
517
David Chinner2a82b8b2007-07-11 11:09:12 +1000518 /*
519 * If we are using filestreams, and we have an unlinked
520 * file that we are processing the last close on, then nothing
521 * will be able to reopen and write to this file. Purge this
522 * inode from the filestreams cache so that it doesn't delay
523 * teardown of the inode.
524 */
525 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
526 xfs_filestream_deassociate(ip);
527
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000528 /*
529 * If we previously truncated this file and removed old data
530 * in the process, we want to initiate "early" writeout on
531 * the last close. This is an attempt to combat the notorious
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300532 * NULL files problem which is particularly noticeable from a
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000533 * truncate down, buffered (re-)write (delalloc), followed by
534 * a crash. What we are effectively doing here is
535 * significantly reducing the time window where we'd otherwise
536 * be exposed to that problem.
537 */
Christoph Hellwig09262b432007-08-29 11:44:50 +1000538 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Dave Chinnerdf4368a2011-06-23 01:35:00 +0000539 if (truncated) {
540 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
541 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
542 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
543 }
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000544 }
545
Dave Chinner6e857562010-12-23 12:02:31 +1100546 if (ip->i_d.di_nlink == 0)
547 return 0;
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000548
Al Viroabbede12011-07-26 02:31:30 -0400549 if ((S_ISREG(ip->i_d.di_mode) &&
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000550 (VFS_I(ip)->i_size > 0 ||
551 (VN_CACHED(VFS_I(ip)) > 0 || ip->i_delayed_blks > 0)) &&
Dave Chinner6e857562010-12-23 12:02:31 +1100552 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
553 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
554
555 /*
556 * If we can't get the iolock just skip truncating the blocks
557 * past EOF because we could deadlock with the mmap_sem
558 * otherwise. We'll get another chance to drop them once the
559 * last reference to the inode is dropped, so we'll never leak
560 * blocks permanently.
561 *
562 * Further, check if the inode is being opened, written and
563 * closed frequently and we have delayed allocation blocks
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300564 * outstanding (e.g. streaming writes from the NFS server),
Dave Chinner6e857562010-12-23 12:02:31 +1100565 * truncating the blocks past EOF will cause fragmentation to
566 * occur.
567 *
568 * In this case don't do the truncation, either, but we have to
569 * be careful how we detect this case. Blocks beyond EOF show
570 * up as i_delayed_blks even when the inode is clean, so we
571 * need to truncate them away first before checking for a dirty
572 * release. Hence on the first dirty close we will still remove
573 * the speculative allocation, but after that we will leave it
574 * in place.
575 */
576 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
577 return 0;
578
579 error = xfs_free_eofblocks(mp, ip,
580 XFS_FREE_EOF_TRYLOCK);
581 if (error)
582 return error;
583
584 /* delalloc blocks after truncation means it really is dirty */
585 if (ip->i_delayed_blks)
586 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 0;
589}
590
591/*
592 * xfs_inactive
593 *
594 * This is called when the vnode reference count for the vnode
595 * goes to zero. If the file has been unlinked, then it must
596 * now be truncated. Also, we clear all of the read-ahead state
597 * kept for the inode here since the file is now closed.
598 */
Christoph Hellwig993386c12007-08-28 16:12:30 +1000599int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600xfs_inactive(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000601 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000603 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 xfs_fsblock_t first_block;
605 int committed;
606 xfs_trans_t *tp;
607 xfs_mount_t *mp;
608 int error;
609 int truncate;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * If the inode is already free, then there can be nothing
613 * to clean up here.
614 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +0100615 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ASSERT(ip->i_df.if_real_bytes == 0);
617 ASSERT(ip->i_df.if_broot_bytes == 0);
618 return VN_INACTIVE_CACHE;
619 }
620
621 /*
622 * Only do a truncate if it's a regular file with
623 * some actual space in it. It's OK to look at the
624 * inode's fields without the lock because we're the
625 * only one with a reference to the inode.
626 */
627 truncate = ((ip->i_d.di_nlink == 0) &&
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000628 ((ip->i_d.di_size != 0) || XFS_ISIZE(ip) != 0 ||
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000629 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Al Viroabbede12011-07-26 02:31:30 -0400630 S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 mp = ip->i_mount;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 error = 0;
635
636 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000637 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 goto out;
639
640 if (ip->i_d.di_nlink != 0) {
Al Viroabbede12011-07-26 02:31:30 -0400641 if ((S_ISREG(ip->i_d.di_mode) &&
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000642 (VFS_I(ip)->i_size > 0 ||
643 (VN_CACHED(VFS_I(ip)) > 0 || ip->i_delayed_blks > 0)) &&
644 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
645 (!(ip->i_d.di_flags &
Nathan Scottdd9f4382006-01-11 15:28:28 +1100646 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000647 ip->i_delayed_blks != 0))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000648 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000649 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100650 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 goto out;
653 }
654
655 ASSERT(ip->i_d.di_nlink == 0);
656
Christoph Hellwig7d095252009-06-08 15:33:32 +0200657 error = xfs_qm_dqattach(ip, 0);
658 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100659 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
662 if (truncate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 xfs_ilock(ip, XFS_IOLOCK_EXCL);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 error = xfs_trans_reserve(tp, 0,
666 XFS_ITRUNCATE_LOG_RES(mp),
667 0, XFS_TRANS_PERM_LOG_RES,
668 XFS_ITRUNCATE_LOG_COUNT);
669 if (error) {
670 /* Don't call itruncate_cleanup */
671 ASSERT(XFS_FORCED_SHUTDOWN(mp));
672 xfs_trans_cancel(tp, 0);
673 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100674 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000678 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000680 ip->i_d.di_size = 0;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000681 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
682
683 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (error) {
685 xfs_trans_cancel(tp,
686 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
687 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100688 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000690
691 ASSERT(ip->i_d.di_nextents == 0);
Al Viroabbede12011-07-26 02:31:30 -0400692 } else if (S_ISLNK(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /*
695 * If we get an error while cleaning up a
696 * symlink we bail out.
697 */
698 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
699 xfs_inactive_symlink_rmt(ip, &tp) :
700 xfs_inactive_symlink_local(ip, &tp);
701
702 if (error) {
703 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100704 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Christoph Hellwigddc34152011-09-19 15:00:54 +0000707 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 } else {
709 error = xfs_trans_reserve(tp, 0,
710 XFS_IFREE_LOG_RES(mp),
711 0, XFS_TRANS_PERM_LOG_RES,
712 XFS_INACTIVE_LOG_COUNT);
713 if (error) {
714 ASSERT(XFS_FORCED_SHUTDOWN(mp));
715 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +0100716 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000720 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 /*
724 * If there are attributes associated with the file
725 * then blow them away now. The code calls a routine
726 * that recursively deconstructs the attribute fork.
727 * We need to just commit the current transaction
728 * because we can't use it for xfs_attr_inactive().
729 */
730 if (ip->i_d.di_anextents > 0) {
731 error = xfs_inactive_attrs(ip, &tp);
732 /*
733 * If we got an error, the transaction is already
734 * cancelled, and the inode is unlocked. Just get out.
735 */
736 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100737 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 } else if (ip->i_afp) {
739 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
740 }
741
742 /*
743 * Free the inode.
744 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600745 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 error = xfs_ifree(tp, ip, &free_list);
747 if (error) {
748 /*
749 * If we fail to free the inode, shut down. The cancel
750 * might do that, we need to make sure. Otherwise the
751 * inode might be lost for a long time or forever.
752 */
753 if (!XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100754 xfs_notice(mp, "%s: xfs_ifree returned error %d",
755 __func__, error);
Nathan Scott7d04a332006-06-09 14:58:38 +1000756 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
759 } else {
760 /*
761 * Credit the quota account(s). The inode is gone.
762 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200763 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /*
David Chinner78e9da72008-04-10 12:24:17 +1000766 * Just ignore errors at this point. There is nothing we can
767 * do except to try to keep going. Make sure it's not a silent
768 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
David Chinner78e9da72008-04-10 12:24:17 +1000770 error = xfs_bmap_finish(&tp, &free_list, &committed);
771 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100772 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
773 __func__, error);
David Chinner78e9da72008-04-10 12:24:17 +1000774 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
775 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100776 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
777 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * Release the dquots held by inode, if any.
782 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200783 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
785
786 out:
787 return VN_INACTIVE_CACHE;
788}
789
Barry Naujok384f3ce2008-05-21 16:58:22 +1000790/*
791 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
792 * is allowed, otherwise it has to be an exact match. If a CI match is found,
793 * ci_name->name will point to a the actual name (caller must free) or
794 * will be set to NULL if an exact match is found.
795 */
Christoph Hellwig993386c12007-08-28 16:12:30 +1000796int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797xfs_lookup(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000798 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000799 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000800 xfs_inode_t **ipp,
801 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000803 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int error;
805 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000807 trace_xfs_lookup(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
810 return XFS_ERROR(EIO);
811
812 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000813 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000815
816 if (error)
817 goto out;
818
Dave Chinner7b6259e2010-06-24 11:35:17 +1000819 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000820 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000821 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000822
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000823 return 0;
824
Barry Naujok384f3ce2008-05-21 16:58:22 +1000825out_free_name:
826 if (ci_name)
827 kmem_free(ci_name->name);
828out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000829 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return error;
831}
832
Christoph Hellwig993386c12007-08-28 16:12:30 +1000833int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834xfs_create(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000835 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000836 struct xfs_name *name,
Al Viro576b1d62011-07-26 02:50:15 -0400837 umode_t mode,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000838 xfs_dev_t rdev,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000839 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100841 int is_dir = S_ISDIR(mode);
842 struct xfs_mount *mp = dp->i_mount;
843 struct xfs_inode *ip = NULL;
844 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000845 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 xfs_bmap_free_t free_list;
847 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +1000848 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 uint cancel_flags;
850 int committed;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000851 prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100852 struct xfs_dquot *udqp = NULL;
853 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100855 uint log_res;
856 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000858 trace_xfs_create(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100860 if (XFS_FORCED_SHUTDOWN(mp))
861 return XFS_ERROR(EIO);
862
Nathan Scott365ca832005-06-21 15:39:12 +1000863 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000864 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000866 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /*
869 * Make sure that we have allocated dquot(s) on disk.
870 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200871 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100872 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000874 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100876 if (is_dir) {
877 rdev = 0;
878 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
879 log_res = XFS_MKDIR_LOG_RES(mp);
880 log_count = XFS_MKDIR_LOG_COUNT;
881 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
882 } else {
883 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
884 log_res = XFS_CREATE_LOG_RES(mp);
885 log_count = XFS_CREATE_LOG_COUNT;
886 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * Initially assume that the file does not exist and
893 * reserve the resources for that case. If that is not
894 * the case we'll drop the one we have and get a more
895 * appropriate transaction later.
896 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100897 error = xfs_trans_reserve(tp, resblks, log_res, 0,
898 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +0200900 /* flush outstanding delalloc blocks and retry */
901 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -0500902 error = xfs_trans_reserve(tp, resblks, log_res, 0,
903 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +0200904 }
905 if (error == ENOSPC) {
906 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100908 error = xfs_trans_reserve(tp, 0, log_res, 0,
909 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 if (error) {
912 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100913 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000916 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +1000917 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100919 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /*
922 * Reserve disk quota and the inode.
923 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200924 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100926 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Barry Naujok556b8b12008-04-10 12:22:07 +1000928 error = xfs_dir_canenter(tp, dp, name, resblks);
929 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100930 goto out_trans_cancel;
931
932 /*
933 * A newly created regular or special file just has one directory
934 * entry pointing to them, but a directory also the "." entry
935 * pointing to itself.
936 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000937 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100938 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (error) {
940 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100941 goto out_trans_cancel;
942 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /*
Christoph Hellwig993386c12007-08-28 16:12:30 +1000946 * Now we join the directory inode to the transaction. We do not do it
947 * earlier because xfs_dir_ialloc might commit the previous transaction
948 * (and release all the locks). An error from here on will result in
949 * the transaction cancel unlocking dp so don't do it explicitly in the
950 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000952 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +1000953 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Barry Naujok556b8b12008-04-10 12:22:07 +1000955 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000956 &first_block, &free_list, resblks ?
957 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (error) {
959 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100960 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000962 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
964
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100965 if (is_dir) {
966 error = xfs_dir_init(tp, ip, dp);
967 if (error)
968 goto out_bmap_cancel;
969
970 error = xfs_bumplink(tp, dp);
971 if (error)
972 goto out_bmap_cancel;
973 }
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * If this is a synchronous mount, make sure that the
977 * create transaction goes to disk before returning to
978 * the user.
979 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100980 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /*
984 * Attach the dquot(s) to the inodes and modify them incore.
985 * These ids of the inode couldn't have changed since the new
986 * inode has been locked ever since it was created.
987 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200988 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100990 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100991 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000992 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000994 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000995 if (error)
996 goto out_release_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Christoph Hellwig7d095252009-06-08 15:33:32 +0200998 xfs_qm_dqrele(udqp);
999 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001001 *ipp = ip;
Christoph Hellwig288699f2010-06-23 18:11:15 +10001002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001004 out_bmap_cancel:
1005 xfs_bmap_cancel(&free_list);
1006 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001008 out_trans_cancel:
1009 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001010 out_release_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /*
1012 * Wait until after the current transaction is aborted to
1013 * release the inode. This prevents recursive transactions
1014 * and deadlocks from xfs_inactive.
1015 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001016 if (ip)
1017 IRELE(ip);
1018
1019 xfs_qm_dqrele(udqp);
1020 xfs_qm_dqrele(gdqp);
1021
1022 if (unlock_dp_on_error)
1023 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1024 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028int xfs_locked_n;
1029int xfs_small_retries;
1030int xfs_middle_retries;
1031int xfs_lots_retries;
1032int xfs_lock_delays;
1033#endif
1034
1035/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001036 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1037 * a different value
1038 */
1039static inline int
1040xfs_lock_inumorder(int lock_mode, int subclass)
1041{
1042 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001043 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001044 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001045 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001046
1047 return lock_mode;
1048}
1049
1050/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * The following routine will lock n inodes in exclusive mode.
1052 * We assume the caller calls us with the inodes in i_ino order.
1053 *
1054 * We need to detect deadlock where an inode that we lock
1055 * is in the AIL and we start waiting for another inode that is locked
1056 * by a thread in a long running transaction (such as truncate). This can
1057 * result in deadlock since the long running trans might need to wait
1058 * for the inode we just locked in order to push the tail and free space
1059 * in the log.
1060 */
1061void
1062xfs_lock_inodes(
1063 xfs_inode_t **ips,
1064 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 uint lock_mode)
1066{
1067 int attempts = 0, i, j, try_lock;
1068 xfs_log_item_t *lp;
1069
1070 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1071
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001072 try_lock = 0;
1073 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075again:
1076 for (; i < inodes; i++) {
1077 ASSERT(ips[i]);
1078
1079 if (i && (ips[i] == ips[i-1])) /* Already locked */
1080 continue;
1081
1082 /*
1083 * If try_lock is not set yet, make sure all locked inodes
1084 * are not in the AIL.
1085 * If any are, set try_lock to be used later.
1086 */
1087
1088 if (!try_lock) {
1089 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1090 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1091 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1092 try_lock++;
1093 }
1094 }
1095 }
1096
1097 /*
1098 * If any of the previous locks we have locked is in the AIL,
1099 * we must TRY to get the second and subsequent locks. If
1100 * we can't get any, we must release all we have
1101 * and try again.
1102 */
1103
1104 if (try_lock) {
1105 /* try_lock must be 0 if i is 0. */
1106 /*
1107 * try_lock means we have an inode locked
1108 * that is in the AIL.
1109 */
1110 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001111 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 attempts++;
1113
1114 /*
1115 * Unlock all previous guys and try again.
1116 * xfs_iunlock will try to push the tail
1117 * if the inode is in the AIL.
1118 */
1119
1120 for(j = i - 1; j >= 0; j--) {
1121
1122 /*
1123 * Check to see if we've already
1124 * unlocked this one.
1125 * Not the first one going back,
1126 * and the inode ptr is the same.
1127 */
1128 if ((j != (i - 1)) && ips[j] ==
1129 ips[j+1])
1130 continue;
1131
1132 xfs_iunlock(ips[j], lock_mode);
1133 }
1134
1135 if ((attempts % 5) == 0) {
1136 delay(1); /* Don't just spin the CPU */
1137#ifdef DEBUG
1138 xfs_lock_delays++;
1139#endif
1140 }
1141 i = 0;
1142 try_lock = 0;
1143 goto again;
1144 }
1145 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001146 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 }
1149
1150#ifdef DEBUG
1151 if (attempts) {
1152 if (attempts < 5) xfs_small_retries++;
1153 else if (attempts < 100) xfs_middle_retries++;
1154 else xfs_lots_retries++;
1155 } else {
1156 xfs_locked_n++;
1157 }
1158#endif
1159}
1160
David Chinnerf9114eb2008-09-17 16:51:21 +10001161/*
1162 * xfs_lock_two_inodes() can only be used to lock one type of lock
1163 * at a time - the iolock or the ilock, but not both at once. If
1164 * we lock both at once, lockdep will report false positives saying
1165 * we have violated locking orders.
1166 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001167void
1168xfs_lock_two_inodes(
1169 xfs_inode_t *ip0,
1170 xfs_inode_t *ip1,
1171 uint lock_mode)
1172{
1173 xfs_inode_t *temp;
1174 int attempts = 0;
1175 xfs_log_item_t *lp;
1176
David Chinnerf9114eb2008-09-17 16:51:21 +10001177 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1178 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001179 ASSERT(ip0->i_ino != ip1->i_ino);
1180
1181 if (ip0->i_ino > ip1->i_ino) {
1182 temp = ip0;
1183 ip0 = ip1;
1184 ip1 = temp;
1185 }
1186
1187 again:
1188 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1189
1190 /*
1191 * If the first lock we have locked is in the AIL, we must TRY to get
1192 * the second lock. If we can't get it, we must release the first one
1193 * and try again.
1194 */
1195 lp = (xfs_log_item_t *)ip0->i_itemp;
1196 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1197 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1198 xfs_iunlock(ip0, lock_mode);
1199 if ((++attempts % 5) == 0)
1200 delay(1); /* Don't just spin the CPU */
1201 goto again;
1202 }
1203 } else {
1204 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1205 }
1206}
1207
Christoph Hellwig993386c12007-08-28 16:12:30 +10001208int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209xfs_remove(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001210 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001211 struct xfs_name *name,
1212 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001214 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001216 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int error = 0;
1218 xfs_bmap_free_t free_list;
1219 xfs_fsblock_t first_block;
1220 int cancel_flags;
1221 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int link_zero;
1223 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001224 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001226 trace_xfs_remove(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (XFS_FORCED_SHUTDOWN(mp))
1229 return XFS_ERROR(EIO);
1230
Christoph Hellwig7d095252009-06-08 15:33:32 +02001231 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001232 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Christoph Hellwig7d095252009-06-08 15:33:32 +02001235 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001236 if (error)
1237 goto std_return;
1238
1239 if (is_dir) {
1240 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1241 log_count = XFS_DEFAULT_LOG_COUNT;
1242 } else {
1243 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1244 log_count = XFS_REMOVE_LOG_COUNT;
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * We try to get the real space reservation first,
1250 * allowing for directory btree deletion(s) implying
1251 * possible bmap insert(s). If we can't get the space
1252 * reservation then we use 0 instead, and avoid the bmap
1253 * btree insert(s) in the directory code by, if the bmap
1254 * insert tries to happen, instead trimming the LAST
1255 * block from the directory.
1256 */
1257 resblks = XFS_REMOVE_SPACE_RES(mp);
1258 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001259 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (error == ENOSPC) {
1261 resblks = 0;
1262 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001263 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 if (error) {
1266 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001267 cancel_flags = 0;
1268 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001271 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Christoph Hellwigddc34152011-09-19 15:00:54 +00001273 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1274 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001277 * If we're removing a directory perform some additional validation.
1278 */
1279 if (is_dir) {
1280 ASSERT(ip->i_d.di_nlink >= 2);
1281 if (ip->i_d.di_nlink != 2) {
1282 error = XFS_ERROR(ENOTEMPTY);
1283 goto out_trans_cancel;
1284 }
1285 if (!xfs_dir_isempty(ip)) {
1286 error = XFS_ERROR(ENOTEMPTY);
1287 goto out_trans_cancel;
1288 }
1289 }
1290
Eric Sandeen9d87c312009-01-14 23:22:07 -06001291 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001292 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001293 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (error) {
1295 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001296 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001298 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001300 if (is_dir) {
1301 /*
1302 * Drop the link from ip's "..".
1303 */
1304 error = xfs_droplink(tp, dp);
1305 if (error)
1306 goto out_bmap_cancel;
1307
1308 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001309 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001310 */
1311 error = xfs_droplink(tp, ip);
1312 if (error)
1313 goto out_bmap_cancel;
1314 } else {
1315 /*
1316 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001317 * inode here. For a directory this is done implicitly
1318 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001319 */
1320 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001323 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001324 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001325 */
1326 error = xfs_droplink(tp, ip);
1327 if (error)
1328 goto out_bmap_cancel;
1329
1330 /*
1331 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * we are in the transaction.
1333 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001334 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * If this is a synchronous mount, make sure that the
1338 * remove transaction goes to disk before returning to
1339 * the user.
1340 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001341 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001344 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001345 if (error)
1346 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001348 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001349 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001353 * If we are using filestreams, kill the stream association.
1354 * If the file is still open it may get a new one but that
1355 * will get killed on last close in xfs_close() so we don't
1356 * have to worry about that.
1357 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001358 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001359 xfs_filestream_deassociate(ip);
1360
Christoph Hellwig288699f2010-06-23 18:11:15 +10001361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001363 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 xfs_bmap_cancel(&free_list);
1365 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001366 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001368 std_return:
1369 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Christoph Hellwig993386c12007-08-28 16:12:30 +10001372int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373xfs_link(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001374 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001375 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10001376 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001378 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int error;
1381 xfs_bmap_free_t free_list;
1382 xfs_fsblock_t first_block;
1383 int cancel_flags;
1384 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001387 trace_xfs_link(tdp, target_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001389 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (XFS_FORCED_SHUTDOWN(mp))
1392 return XFS_ERROR(EIO);
1393
Christoph Hellwig7d095252009-06-08 15:33:32 +02001394 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01001395 if (error)
1396 goto std_return;
1397
Christoph Hellwig7d095252009-06-08 15:33:32 +02001398 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (error)
1400 goto std_return;
1401
1402 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1403 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001404 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1406 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1407 if (error == ENOSPC) {
1408 resblks = 0;
1409 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1410 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1411 }
1412 if (error) {
1413 cancel_flags = 0;
1414 goto error_return;
1415 }
1416
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001417 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Christoph Hellwigddc34152011-09-19 15:00:54 +00001419 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1420 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /*
Nathan Scott365ca832005-06-21 15:39:12 +10001423 * If we are using project inheritance, we only allow hard link
1424 * creation in our tree when the project IDs are the same; else
1425 * the tree quota mechanism could be circumvented.
1426 */
1427 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001428 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10001429 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10001430 goto error_return;
1431 }
1432
Barry Naujok556b8b12008-04-10 12:22:07 +10001433 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1434 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto error_return;
1436
Eric Sandeen9d87c312009-01-14 23:22:07 -06001437 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Barry Naujok556b8b12008-04-10 12:22:07 +10001439 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1440 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (error)
1442 goto abort_return;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001443 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1445
1446 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001447 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /*
1451 * If this is a synchronous mount, make sure that the
1452 * link transaction goes to disk before returning to
1453 * the user.
1454 */
1455 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1456 xfs_trans_set_sync(tp);
1457 }
1458
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001459 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (error) {
1461 xfs_bmap_cancel(&free_list);
1462 goto abort_return;
1463 }
1464
Christoph Hellwig288699f2010-06-23 18:11:15 +10001465 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 abort_return:
1468 cancel_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 error_return:
1470 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001471 std_return:
1472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001474
Christoph Hellwig993386c12007-08-28 16:12:30 +10001475int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476xfs_symlink(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001477 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001478 struct xfs_name *link_name,
1479 const char *target_path,
Al Viro576b1d62011-07-26 02:50:15 -04001480 umode_t mode,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001481 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001483 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 xfs_inode_t *ip;
1486 int error;
1487 int pathlen;
1488 xfs_bmap_free_t free_list;
1489 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +10001490 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 uint cancel_flags;
1492 int committed;
1493 xfs_fileoff_t first_fsb;
1494 xfs_filblks_t fs_blocks;
1495 int nmaps;
1496 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1497 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10001498 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int byte_cnt;
1500 int n;
1501 xfs_buf_t *bp;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001502 prid_t prid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct xfs_dquot *udqp, *gdqp;
1504 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Christoph Hellwig3937be52008-03-06 13:46:19 +11001506 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 error = 0;
1508 ip = NULL;
1509 tp = NULL;
1510
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001511 trace_xfs_symlink(dp, link_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (XFS_FORCED_SHUTDOWN(mp))
1514 return XFS_ERROR(EIO);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /*
1517 * Check component lengths of the target path name.
1518 */
1519 pathlen = strlen(target_path);
1520 if (pathlen >= MAXPATHLEN) /* total string too long */
1521 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001524 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001525 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001527 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 /*
1530 * Make sure that we have allocated dquot(s) on disk.
1531 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001532 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1534 if (error)
1535 goto std_return;
1536
1537 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1538 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1539 /*
1540 * The symlink will fit into the inode data fork?
1541 * There can't be any attributes so we get the whole variable part.
1542 */
1543 if (pathlen <= XFS_LITINO(mp))
1544 fs_blocks = 0;
1545 else
1546 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10001547 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
1549 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1550 if (error == ENOSPC && fs_blocks == 0) {
1551 resblks = 0;
1552 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
1553 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1554 }
1555 if (error) {
1556 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto error_return;
1558 }
1559
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001560 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001561 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 /*
1564 * Check whether the directory allows new symlinks or not.
1565 */
1566 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
1567 error = XFS_ERROR(EPERM);
1568 goto error_return;
1569 }
1570
1571 /*
1572 * Reserve disk quota : blocks and inode.
1573 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001574 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (error)
1576 goto error_return;
1577
1578 /*
1579 * Check for ability to enter directory entry, if no space reserved.
1580 */
Barry Naujok556b8b12008-04-10 12:22:07 +10001581 error = xfs_dir_canenter(tp, dp, link_name, resblks);
1582 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto error_return;
1584 /*
1585 * Initialize the bmap freelist prior to calling either
1586 * bmapi or the directory create code.
1587 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001588 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /*
1591 * Allocate an inode for the symlink.
1592 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001593 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
1594 prid, resblks > 0, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (error) {
1596 if (error == ENOSPC)
1597 goto error_return;
1598 goto error1;
1599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Christoph Hellwig993386c12007-08-28 16:12:30 +10001601 /*
1602 * An error after we've joined dp to the transaction will result in the
1603 * transaction cancel unlocking dp so don't do it explicitly in the
1604 * error path.
1605 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001606 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001607 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 /*
1610 * Also attach the dquot(s) to it, if applicable.
1611 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001612 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 if (resblks)
1615 resblks -= XFS_IALLOC_SPACE_RES(mp);
1616 /*
1617 * If the symlink will fit into the inode, write it inline.
1618 */
1619 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
1620 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
1621 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
1622 ip->i_d.di_size = pathlen;
1623
1624 /*
1625 * The inode was initially created in extent format.
1626 */
1627 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
1628 ip->i_df.if_flags |= XFS_IFINLINE;
1629
1630 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
1631 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
1632
1633 } else {
1634 first_fsb = 0;
1635 nmaps = SYMLINK_MAPS;
1636
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001637 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
1638 XFS_BMAPI_METADATA, &first_block, resblks,
1639 mval, &nmaps, &free_list);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001640 if (error)
1641 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 if (resblks)
1644 resblks -= fs_blocks;
1645 ip->i_d.di_size = pathlen;
1646 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1647
1648 cur_chunk = target_path;
1649 for (n = 0; n < nmaps; n++) {
1650 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1651 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1652 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
1653 BTOBB(byte_cnt), 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001654 if (!bp) {
1655 error = ENOMEM;
1656 goto error2;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (pathlen < byte_cnt) {
1659 byte_cnt = pathlen;
1660 }
1661 pathlen -= byte_cnt;
1662
Chandra Seetharaman62926042011-07-22 23:40:15 +00001663 memcpy(bp->b_addr, cur_chunk, byte_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 cur_chunk += byte_cnt;
1665
1666 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
1667 }
1668 }
1669
1670 /*
1671 * Create the directory entry for the symlink.
1672 */
Barry Naujok556b8b12008-04-10 12:22:07 +10001673 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
1674 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001675 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001676 goto error2;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001677 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1679
1680 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * If this is a synchronous mount, make sure that the
1682 * symlink transaction goes to disk before returning to
1683 * the user.
1684 */
1685 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1686 xfs_trans_set_sync(tp);
1687 }
1688
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001689 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (error) {
1691 goto error2;
1692 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001693 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001694 xfs_qm_dqrele(udqp);
1695 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Christoph Hellwig288699f2010-06-23 18:11:15 +10001697 *ipp = ip;
1698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 error2:
1701 IRELE(ip);
1702 error1:
1703 xfs_bmap_cancel(&free_list);
1704 cancel_flags |= XFS_TRANS_ABORT;
1705 error_return:
1706 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001707 xfs_qm_dqrele(udqp);
1708 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Christoph Hellwig993386c12007-08-28 16:12:30 +10001710 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001712 std_return:
1713 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716int
Christoph Hellwig993386c12007-08-28 16:12:30 +10001717xfs_set_dmattrs(
1718 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 u_int evmask,
Christoph Hellwig993386c12007-08-28 16:12:30 +10001720 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001722 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 int error;
1725
1726 if (!capable(CAP_SYS_ADMIN))
1727 return XFS_ERROR(EPERM);
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (XFS_FORCED_SHUTDOWN(mp))
1730 return XFS_ERROR(EIO);
1731
1732 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1733 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1734 if (error) {
1735 xfs_trans_cancel(tp, 0);
1736 return error;
1737 }
1738 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001739 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Christoph Hellwig613d7042007-10-11 17:44:08 +10001741 ip->i_d.di_dmevmask = evmask;
1742 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001745 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 return error;
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750/*
1751 * xfs_alloc_file_space()
1752 * This routine allocates disk space for the given file.
1753 *
1754 * If alloc_type == 0, this request is for an ALLOCSP type
1755 * request which will change the file size. In this case, no
1756 * DMAPI event will be generated by the call. A TRUNCATE event
1757 * will be generated later by xfs_setattr.
1758 *
1759 * If alloc_type != 0, this request is for a RESVSP type
1760 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1761 * lower block boundary byte address is less than the file's
1762 * length.
1763 *
1764 * RETURNS:
1765 * 0 on success
1766 * errno on error
1767 *
1768 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001769STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770xfs_alloc_file_space(
1771 xfs_inode_t *ip,
1772 xfs_off_t offset,
1773 xfs_off_t len,
1774 int alloc_type,
1775 int attr_flags)
1776{
Nathan Scottdd9f4382006-01-11 15:28:28 +11001777 xfs_mount_t *mp = ip->i_mount;
1778 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 xfs_filblks_t allocated_fsb;
1780 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001781 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001783 xfs_fsblock_t firstfsb;
1784 int nimaps;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001785 int quota_flag;
1786 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001788 xfs_bmbt_irec_t imaps[1], *imapp;
1789 xfs_bmap_free_t free_list;
1790 uint qblocks, resblks, resrtextents;
1791 int committed;
1792 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001794 trace_xfs_alloc_file_space(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 if (XFS_FORCED_SHUTDOWN(mp))
1797 return XFS_ERROR(EIO);
1798
Christoph Hellwig7d095252009-06-08 15:33:32 +02001799 error = xfs_qm_dqattach(ip, 0);
1800 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return error;
1802
1803 if (len <= 0)
1804 return XFS_ERROR(EINVAL);
1805
David Chinner957d0eb2007-06-18 16:50:37 +10001806 rt = XFS_IS_REALTIME_INODE(ip);
1807 extsz = xfs_get_extsz_hint(ip);
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11001811 nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1813 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001816 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001819 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Nathan Scottdd9f4382006-01-11 15:28:28 +11001821 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11001822 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11001823 */
1824 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001826 do_div(s, extsz);
1827 s *= extsz;
1828 e = startoffset_fsb + allocatesize_fsb;
1829 if ((temp = do_mod(startoffset_fsb, extsz)))
1830 e += temp;
1831 if ((temp = do_mod(e, extsz)))
1832 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001834 s = 0;
1835 e = allocatesize_fsb;
1836 }
1837
Dave Chinner72656c42010-09-03 12:19:33 +10001838 /*
1839 * The transaction reservation is limited to a 32-bit block
1840 * count, hence we need to limit the number of blocks we are
1841 * trying to reserve to avoid an overflow. We can't allocate
1842 * more than @nimaps extents, and an extent is limited on disk
1843 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1844 */
1845 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
Nathan Scottdd9f4382006-01-11 15:28:28 +11001846 if (unlikely(rt)) {
Dave Chinner72656c42010-09-03 12:19:33 +10001847 resrtextents = qblocks = resblks;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001848 resrtextents /= mp->m_sb.sb_rextsize;
1849 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1850 quota_flag = XFS_QMOPT_RES_RTBLKS;
1851 } else {
1852 resrtextents = 0;
Dave Chinner72656c42010-09-03 12:19:33 +10001853 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001854 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856
1857 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001858 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 */
1860 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001861 error = xfs_trans_reserve(tp, resblks,
1862 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 XFS_TRANS_PERM_LOG_RES,
1864 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001866 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868 if (error) {
1869 /*
1870 * Free the transaction structure.
1871 */
1872 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1873 xfs_trans_cancel(tp, 0);
1874 break;
1875 }
1876 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001877 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1878 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (error)
1880 goto error1;
1881
Christoph Hellwigddc34152011-09-19 15:00:54 +00001882 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Eric Sandeen9d87c312009-01-14 23:22:07 -06001884 xfs_bmap_init(&free_list, &firstfsb);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001885 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1886 allocatesize_fsb, alloc_type, &firstfsb,
1887 0, imapp, &nimaps, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (error) {
1889 goto error0;
1890 }
1891
1892 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001893 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001895 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (error) {
1897 goto error0;
1898 }
1899
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001900 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1902 if (error) {
1903 break;
1904 }
1905
1906 allocated_fsb = imapp->br_blockcount;
1907
Nathan Scottdd9f4382006-01-11 15:28:28 +11001908 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 error = XFS_ERROR(ENOSPC);
1910 break;
1911 }
1912
1913 startoffset_fsb += allocated_fsb;
1914 allocatesize_fsb -= allocated_fsb;
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 return error;
1918
Nathan Scottdd9f4382006-01-11 15:28:28 +11001919error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001921 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001922
1923error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1925 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001926 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929/*
1930 * Zero file bytes between startoff and endoff inclusive.
1931 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001932 *
1933 * This function is used by xfs_free_file_space() to zero
1934 * partial blocks when the range to free is not block aligned.
1935 * When unreserving space with boundaries that are not block
1936 * aligned we round up the start and round down the end
1937 * boundaries and then use this function to zero the parts of
1938 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
1940STATIC int
1941xfs_zero_remaining_bytes(
1942 xfs_inode_t *ip,
1943 xfs_off_t startoff,
1944 xfs_off_t endoff)
1945{
1946 xfs_bmbt_irec_t imap;
1947 xfs_fileoff_t offset_fsb;
1948 xfs_off_t lastoffset;
1949 xfs_off_t offset;
1950 xfs_buf_t *bp;
1951 xfs_mount_t *mp = ip->i_mount;
1952 int nimap;
1953 int error = 0;
1954
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001955 /*
1956 * Avoid doing I/O beyond eof - it's not necessary
1957 * since nothing can read beyond eof. The space will
1958 * be zeroed when the file is extended anyway.
1959 */
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001960 if (startoff >= XFS_ISIZE(ip))
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001961 return 0;
1962
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001963 if (endoff > XFS_ISIZE(ip))
1964 endoff = XFS_ISIZE(ip);
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001965
Dave Chinner686865f2010-09-24 20:07:47 +10001966 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1967 mp->m_rtdev_targp : mp->m_ddev_targp,
Dave Chinneraa5c1582012-04-23 15:58:56 +10001968 BTOBB(mp->m_sb.sb_blocksize), 0);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11001969 if (!bp)
1970 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001972 xfs_buf_unlock(bp);
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1975 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1976 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00001977 error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (error || nimap < 1)
1979 break;
1980 ASSERT(imap.br_blockcount >= 1);
1981 ASSERT(imap.br_startoff == offset_fsb);
1982 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1983 if (lastoffset > endoff)
1984 lastoffset = endoff;
1985 if (imap.br_startblock == HOLESTARTBLOCK)
1986 continue;
1987 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1988 if (imap.br_state == XFS_EXT_UNWRITTEN)
1989 continue;
1990 XFS_BUF_UNDONE(bp);
1991 XFS_BUF_UNWRITE(bp);
1992 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001993 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001995 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10001996 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00001997 xfs_buf_ioerror_alert(bp,
1998 "xfs_zero_remaining_bytes(read)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 break;
2000 }
Chandra Seetharaman62926042011-07-22 23:40:15 +00002001 memset(bp->b_addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2003 0, lastoffset - offset + 1);
2004 XFS_BUF_UNDONE(bp);
2005 XFS_BUF_UNREAD(bp);
2006 XFS_BUF_WRITE(bp);
2007 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002008 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002009 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00002010 xfs_buf_ioerror_alert(bp,
2011 "xfs_zero_remaining_bytes(write)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 break;
2013 }
2014 }
2015 xfs_buf_free(bp);
2016 return error;
2017}
2018
2019/*
2020 * xfs_free_file_space()
2021 * This routine frees disk space for the given file.
2022 *
2023 * This routine is only called by xfs_change_file_space
2024 * for an UNRESVSP type call.
2025 *
2026 * RETURNS:
2027 * 0 on success
2028 * errno on error
2029 *
2030 */
2031STATIC int
2032xfs_free_file_space(
2033 xfs_inode_t *ip,
2034 xfs_off_t offset,
2035 xfs_off_t len,
2036 int attr_flags)
2037{
2038 int committed;
2039 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 xfs_fileoff_t endoffset_fsb;
2041 int error;
2042 xfs_fsblock_t firstfsb;
2043 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 xfs_bmbt_irec_t imap;
2045 xfs_off_t ioffset;
2046 xfs_extlen_t mod=0;
2047 xfs_mount_t *mp;
2048 int nimap;
2049 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002050 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int rt;
2052 xfs_fileoff_t startoffset_fsb;
2053 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002054 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 mp = ip->i_mount;
2057
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002058 trace_xfs_free_file_space(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002059
Christoph Hellwig7d095252009-06-08 15:33:32 +02002060 error = xfs_qm_dqattach(ip, 0);
2061 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return error;
2063
2064 error = 0;
2065 if (len <= 0) /* if nothing being freed */
2066 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002067 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002069 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002071 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002072 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002073 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002075 /* wait for the completion of any pending DIOs */
Christoph Hellwig4a06fd22011-08-23 08:28:13 +00002076 inode_dio_wait(VFS_I(ip));
Yingping Lu9fa80462006-03-22 12:44:35 +11002077 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002078
Tim Shimmine6a4b372007-11-23 16:30:42 +11002079 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002081
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002082 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002083 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf20942007-05-08 13:49:27 +10002084 if (error)
2085 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002086 }
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
2089 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002090 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2092 * will take care of it for us.
2093 */
Eric Sandeen62118702008-03-06 13:44:28 +11002094 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00002096 error = xfs_bmapi_read(ip, startoffset_fsb, 1,
2097 &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (error)
2099 goto out_unlock_iolock;
2100 ASSERT(nimap == 0 || nimap == 1);
2101 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2102 xfs_daddr_t block;
2103
2104 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2105 block = imap.br_startblock;
2106 mod = do_div(block, mp->m_sb.sb_rextsize);
2107 if (mod)
2108 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2109 }
2110 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00002111 error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
2112 &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (error)
2114 goto out_unlock_iolock;
2115 ASSERT(nimap == 0 || nimap == 1);
2116 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2117 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2118 mod++;
2119 if (mod && (mod != mp->m_sb.sb_rextsize))
2120 endoffset_fsb -= mod;
2121 }
2122 }
2123 if ((done = (endoffset_fsb <= startoffset_fsb)))
2124 /*
2125 * One contiguous piece to clear
2126 */
2127 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2128 else {
2129 /*
2130 * Some full blocks, possibly two pieces to clear
2131 */
2132 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2133 error = xfs_zero_remaining_bytes(ip, offset,
2134 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2135 if (!error &&
2136 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2137 error = xfs_zero_remaining_bytes(ip,
2138 XFS_FSB_TO_B(mp, endoffset_fsb),
2139 offset + len - 1);
2140 }
2141
2142 /*
2143 * free file space until done or until there is an error
2144 */
2145 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2146 while (!error && !done) {
2147
2148 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002149 * allocate and setup the transaction. Allow this
2150 * transaction to dip into the reserve blocks to ensure
2151 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 */
2153 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002154 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 error = xfs_trans_reserve(tp,
2156 resblks,
2157 XFS_WRITE_LOG_RES(mp),
2158 0,
2159 XFS_TRANS_PERM_LOG_RES,
2160 XFS_WRITE_LOG_COUNT);
2161
2162 /*
2163 * check for running out of space
2164 */
2165 if (error) {
2166 /*
2167 * Free the transaction structure.
2168 */
2169 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2170 xfs_trans_cancel(tp, 0);
2171 break;
2172 }
2173 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002174 error = xfs_trans_reserve_quota(tp, mp,
2175 ip->i_udquot, ip->i_gdquot,
2176 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (error)
2178 goto error1;
2179
Christoph Hellwigddc34152011-09-19 15:00:54 +00002180 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /*
2183 * issue the bunmapi() call to free the blocks
2184 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002185 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002186 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 endoffset_fsb - startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002188 0, 2, &firstfsb, &free_list, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (error) {
2190 goto error0;
2191 }
2192
2193 /*
2194 * complete the transaction
2195 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002196 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (error) {
2198 goto error0;
2199 }
2200
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002201 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2203 }
2204
2205 out_unlock_iolock:
2206 if (need_iolock)
2207 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2208 return error;
2209
2210 error0:
2211 xfs_bmap_cancel(&free_list);
2212 error1:
2213 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2214 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2215 XFS_ILOCK_EXCL);
2216 return error;
2217}
2218
2219/*
2220 * xfs_change_file_space()
2221 * This routine allocates or frees disk space for the given file.
2222 * The user specified parameters are checked for alignment and size
2223 * limitations.
2224 *
2225 * RETURNS:
2226 * 0 on success
2227 * errno on error
2228 *
2229 */
2230int
2231xfs_change_file_space(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002232 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int cmd,
2234 xfs_flock64_t *bf,
2235 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 int attr_flags)
2237{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002238 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 int clrprealloc;
2240 int error;
2241 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 int setprealloc;
2243 xfs_off_t startoffset;
2244 xfs_off_t llen;
2245 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002246 struct iattr iattr;
Dave Chinner44722352010-08-24 12:02:11 +10002247 int prealloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Christoph Hellwig993386c12007-08-28 16:12:30 +10002249 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return XFS_ERROR(EINVAL);
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 switch (bf->l_whence) {
2253 case 0: /*SEEK_SET*/
2254 break;
2255 case 1: /*SEEK_CUR*/
2256 bf->l_start += offset;
2257 break;
2258 case 2: /*SEEK_END*/
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00002259 bf->l_start += XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 break;
2261 default:
2262 return XFS_ERROR(EINVAL);
2263 }
2264
2265 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2266
2267 if ( (bf->l_start < 0)
2268 || (bf->l_start > XFS_MAXIOFFSET(mp))
2269 || (bf->l_start + llen < 0)
2270 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2271 return XFS_ERROR(EINVAL);
2272
2273 bf->l_whence = 0;
2274
2275 startoffset = bf->l_start;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00002276 fsize = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /*
2279 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2280 * file space.
2281 * These calls do NOT zero the data space allocated to the file,
2282 * nor do they change the file size.
2283 *
2284 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2285 * space.
2286 * These calls cause the new file data to be zeroed and the file
2287 * size to be changed.
2288 */
2289 setprealloc = clrprealloc = 0;
Dave Chinner44722352010-08-24 12:02:11 +10002290 prealloc_type = XFS_BMAPI_PREALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 switch (cmd) {
Dave Chinner44722352010-08-24 12:02:11 +10002293 case XFS_IOC_ZERO_RANGE:
2294 prealloc_type |= XFS_BMAPI_CONVERT;
2295 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2296 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 case XFS_IOC_RESVSP:
2298 case XFS_IOC_RESVSP64:
2299 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
Dave Chinner44722352010-08-24 12:02:11 +10002300 prealloc_type, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (error)
2302 return error;
2303 setprealloc = 1;
2304 break;
2305
2306 case XFS_IOC_UNRESVSP:
2307 case XFS_IOC_UNRESVSP64:
2308 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2309 attr_flags)))
2310 return error;
2311 break;
2312
2313 case XFS_IOC_ALLOCSP:
2314 case XFS_IOC_ALLOCSP64:
2315 case XFS_IOC_FREESP:
2316 case XFS_IOC_FREESP64:
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10002317 /*
2318 * These operations actually do IO when extending the file, but
2319 * the allocation is done seperately to the zeroing that is
2320 * done. This set of operations need to be serialised against
2321 * other IO operations, such as truncate and buffered IO. We
2322 * need to take the IOLOCK here to serialise the allocation and
2323 * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
2324 * truncate, direct IO) from racing against the transient
2325 * allocated but not written state we can have here.
2326 */
2327 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (startoffset > fsize) {
2329 error = xfs_alloc_file_space(ip, fsize,
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10002330 startoffset - fsize, 0,
2331 attr_flags | XFS_ATTR_NOLOCK);
2332 if (error) {
2333 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 break;
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10002335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002338 iattr.ia_valid = ATTR_SIZE;
2339 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10002341 error = xfs_setattr_size(ip, &iattr,
2342 attr_flags | XFS_ATTR_NOLOCK);
2343 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 if (error)
2346 return error;
2347
2348 clrprealloc = 1;
2349 break;
2350
2351 default:
2352 ASSERT(0);
2353 return XFS_ERROR(EINVAL);
2354 }
2355
2356 /*
2357 * update the inode timestamp, mode, and prealloc flag bits
2358 */
2359 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2360
2361 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2362 0, 0, 0))) {
2363 /* ASSERT(0); */
2364 xfs_trans_cancel(tp, 0);
2365 return error;
2366 }
2367
2368 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00002369 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002371 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 ip->i_d.di_mode &= ~S_ISUID;
2373
2374 /*
2375 * Note that we don't have to worry about mandatory
2376 * file locking being disabled here because we only
2377 * clear the S_ISGID bit if the Group execute bit is
2378 * on, but if it was on then mandatory locking wouldn't
2379 * have been enabled.
2380 */
2381 if (ip->i_d.di_mode & S_IXGRP)
2382 ip->i_d.di_mode &= ~S_ISGID;
2383
Dave Chinnerdcd79a12010-09-28 12:27:25 +10002384 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386 if (setprealloc)
2387 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2388 else if (clrprealloc)
2389 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2390
2391 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinner82878892011-03-26 09:13:08 +11002392 if (attr_flags & XFS_ATTR_SYNC)
2393 xfs_trans_set_sync(tp);
Christoph Hellwig23bb0be2011-09-18 20:47:51 +00002394 return xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}