blob: 45b8ac662aeedaed1b25a12540e444161f201a0c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
32#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_dinode.h"
34#include "xfs_inode.h"
35#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_ialloc.h"
38#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_bmap.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020040#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_attr.h"
42#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_quota.h"
45#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110046#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100049#include "xfs_filestream.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100050#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000051#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
Nathan Scott7d4fb402006-06-09 15:27:16 +100054 * The maximum pathlen is 1024 bytes. Since the minimum file system
55 * blocksize is 512 bytes, we can get a max of 2 extents back from
56 * bmapi.
57 */
58#define SYMLINK_MAPS 2
59
Christoph Hellwig804c83c2007-08-28 13:59:03 +100060STATIC int
61xfs_readlink_bmap(
62 xfs_inode_t *ip,
63 char *link)
64{
65 xfs_mount_t *mp = ip->i_mount;
66 int pathlen = ip->i_d.di_size;
67 int nmaps = SYMLINK_MAPS;
68 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
69 xfs_daddr_t d;
70 int byte_cnt;
71 int n;
72 xfs_buf_t *bp;
73 int error = 0;
74
75 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +100076 mval, &nmaps, NULL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +100077 if (error)
78 goto out;
79
80 for (n = 0; n < nmaps; n++) {
81 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
82 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
83
Christoph Hellwig6ad112b2009-11-24 18:02:23 +000084 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
85 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +100086 error = XFS_BUF_GETERROR(bp);
87 if (error) {
88 xfs_ioerror_alert("xfs_readlink",
89 ip->i_mount, bp, XFS_BUF_ADDR(bp));
90 xfs_buf_relse(bp);
91 goto out;
92 }
93 if (pathlen < byte_cnt)
94 byte_cnt = pathlen;
95 pathlen -= byte_cnt;
96
97 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
98 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 Hellwig993386c2007-08-28 16:12:30 +1000108int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109xfs_readlink(
Christoph Hellwig993386c2007-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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int 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
124 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000125 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000127 pathlen = ip->i_d.di_size;
128 if (!pathlen)
129 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000132 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
133 link[pathlen] = '\0';
134 } else {
135 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000138 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return error;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000144 * Flags for xfs_free_eofblocks
145 */
146#define XFS_FREE_EOF_TRYLOCK (1<<0)
147
148/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000149 * This is called by xfs_inactive to free any blocks beyond eof
150 * when the link count isn't zero and by xfs_dm_punch_hole() when
151 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500153STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000154xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000156 xfs_inode_t *ip,
157 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 xfs_trans_t *tp;
160 int error;
161 xfs_fileoff_t end_fsb;
162 xfs_fileoff_t last_fsb;
163 xfs_filblks_t map_len;
164 int nimaps;
165 xfs_bmbt_irec_t imap;
166
167 /*
168 * Figure out if there are any blocks beyond the end
169 * of the file. If not, then there is nothing to do.
170 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000171 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000173 if (last_fsb <= end_fsb)
Jesper Juhl014c2542006-01-15 02:37:08 +0100174 return 0;
Kulikov Vasiliy3f348852010-07-20 17:54:28 +1000175 map_len = last_fsb - end_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 nimaps = 1;
178 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000179 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000180 NULL, 0, &imap, &nimaps, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 xfs_iunlock(ip, XFS_ILOCK_SHARED);
182
183 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100184 (imap.br_startblock != HOLESTARTBLOCK ||
185 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /*
187 * Attach the dquots to the inode up front.
188 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200189 error = xfs_qm_dqattach(ip, 0);
190 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100191 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /*
194 * There are blocks after the end of file.
195 * Free them up now by truncating the file to
196 * its current size.
197 */
198 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
199
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000200 if (flags & XFS_FREE_EOF_TRYLOCK) {
201 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
202 xfs_trans_cancel(tp, 0);
203 return 0;
204 }
205 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000206 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 error = xfs_trans_reserve(tp, 0,
210 XFS_ITRUNCATE_LOG_RES(mp),
211 0, XFS_TRANS_PERM_LOG_RES,
212 XFS_ITRUNCATE_LOG_COUNT);
213 if (error) {
214 ASSERT(XFS_FORCED_SHUTDOWN(mp));
215 xfs_trans_cancel(tp, 0);
216 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100217 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
220 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000221 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000224 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 XFS_DATA_FORK,
226 0);
227 /*
228 * If we get an error at this point we
229 * simply don't bother truncating the file.
230 */
231 if (error) {
232 xfs_trans_cancel(tp,
233 (XFS_TRANS_RELEASE_LOG_RES |
234 XFS_TRANS_ABORT));
235 } else {
236 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000237 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000239 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100241 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244/*
245 * Free a symlink that has blocks associated with it.
246 */
247STATIC int
248xfs_inactive_symlink_rmt(
249 xfs_inode_t *ip,
250 xfs_trans_t **tpp)
251{
252 xfs_buf_t *bp;
253 int committed;
254 int done;
255 int error;
256 xfs_fsblock_t first_block;
257 xfs_bmap_free_t free_list;
258 int i;
259 xfs_mount_t *mp;
260 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
261 int nmaps;
262 xfs_trans_t *ntp;
263 int size;
264 xfs_trans_t *tp;
265
266 tp = *tpp;
267 mp = ip->i_mount;
268 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
269 /*
270 * We're freeing a symlink that has some
271 * blocks allocated to it. Free the
272 * blocks here. We know that we've got
273 * either 1 or 2 extents and that we can
274 * free them all in one bunmapi call.
275 */
276 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
277 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
278 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
279 ASSERT(XFS_FORCED_SHUTDOWN(mp));
280 xfs_trans_cancel(tp, 0);
281 *tpp = NULL;
282 return error;
283 }
284 /*
285 * Lock the inode, fix the size, and join it to the transaction.
286 * Hold it so in the normal path, we still have it locked for
287 * the second transaction. In the error paths we need it
288 * held so the cancel won't rele it, see below.
289 */
290 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
291 size = (int)ip->i_d.di_size;
292 ip->i_d.di_size = 0;
Christoph Hellwig898621d2010-06-24 11:36:58 +1000293 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
295 /*
296 * Find the block(s) so we can inval and unmap them.
297 */
298 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600299 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800300 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
302 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000303 &free_list)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 goto error0;
305 /*
306 * Invalidate the block(s).
307 */
308 for (i = 0; i < nmaps; i++) {
309 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
310 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
311 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
312 xfs_trans_binval(tp, bp);
313 }
314 /*
315 * Unmap the dead block(s) to the free_list.
316 */
317 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000318 &first_block, &free_list, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto error1;
320 ASSERT(done);
321 /*
322 * Commit the first transaction. This logs the EFI and the inode.
323 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100324 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto error1;
326 /*
327 * The transaction must have been committed, since there were
328 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
329 * The new tp has the extent freeing and EFDs.
330 */
331 ASSERT(committed);
332 /*
333 * The first xact was committed, so add the inode to the new one.
334 * Mark it dirty so it will be logged and moved forward in the log as
335 * part of every commit.
336 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000337 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
339 /*
340 * Get a new, empty transaction to return to our caller.
341 */
342 ntp = xfs_trans_dup(tp);
343 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000344 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * If we get an error on the commit here or on the reserve below,
346 * we need to unlock the inode since the new transaction doesn't
347 * have the inode attached.
348 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000349 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 tp = ntp;
351 if (error) {
352 ASSERT(XFS_FORCED_SHUTDOWN(mp));
353 goto error0;
354 }
355 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100356 * transaction commit worked ok so we can drop the extra ticket
357 * reference that we gained in xfs_trans_dup()
358 */
359 xfs_log_ticket_put(tp->t_ticket);
360
361 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * Remove the memory for extent descriptions (just bookkeeping).
363 */
364 if (ip->i_df.if_bytes)
365 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
366 ASSERT(ip->i_df.if_bytes == 0);
367 /*
368 * Put an itruncate log reservation in the new transaction
369 * for our caller.
370 */
371 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
372 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
373 ASSERT(XFS_FORCED_SHUTDOWN(mp));
374 goto error0;
375 }
376 /*
377 * Return with the inode locked but not joined to the transaction.
378 */
379 *tpp = tp;
380 return 0;
381
382 error1:
383 xfs_bmap_cancel(&free_list);
384 error0:
385 /*
386 * Have to come here with the inode locked and either
387 * (held and in the transaction) or (not in the transaction).
388 * If the inode isn't held then cancel would iput it, but
389 * that's wrong since this is inactive and the vnode ref
390 * count is 0 already.
391 * Cancel won't do anything to the inode if held, but it still
392 * needs to be locked until the cancel is done, if it was
393 * joined to the transaction.
394 */
395 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
396 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
397 *tpp = NULL;
398 return error;
399
400}
401
402STATIC int
403xfs_inactive_symlink_local(
404 xfs_inode_t *ip,
405 xfs_trans_t **tpp)
406{
407 int error;
408
409 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
410 /*
411 * We're freeing a symlink which fit into
412 * the inode. Just free the memory used
413 * to hold the old symlink.
414 */
415 error = xfs_trans_reserve(*tpp, 0,
416 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
417 0, XFS_TRANS_PERM_LOG_RES,
418 XFS_ITRUNCATE_LOG_COUNT);
419
420 if (error) {
421 xfs_trans_cancel(*tpp, 0);
422 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100423 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
426
427 /*
428 * Zero length symlinks _can_ exist.
429 */
430 if (ip->i_df.if_bytes > 0) {
431 xfs_idata_realloc(ip,
432 -(ip->i_df.if_bytes),
433 XFS_DATA_FORK);
434 ASSERT(ip->i_df.if_bytes == 0);
435 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439STATIC int
440xfs_inactive_attrs(
441 xfs_inode_t *ip,
442 xfs_trans_t **tpp)
443{
444 xfs_trans_t *tp;
445 int error;
446 xfs_mount_t *mp;
447
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000448 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 tp = *tpp;
450 mp = ip->i_mount;
451 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +1000452 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +1000454 if (error)
455 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +1000458 if (error)
459 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
462 error = xfs_trans_reserve(tp, 0,
463 XFS_IFREE_LOG_RES(mp),
464 0, XFS_TRANS_PERM_LOG_RES,
465 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +1000466 if (error)
467 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000470 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
472
473 ASSERT(ip->i_d.di_anextents == 0);
474
475 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +0100476 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +1000477
478error_cancel:
479 ASSERT(XFS_FORCED_SHUTDOWN(mp));
480 xfs_trans_cancel(tp, 0);
481error_unlock:
482 *tpp = NULL;
483 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
484 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Christoph Hellwig993386c2007-08-28 16:12:30 +1000487int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000489 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000491 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int error;
493
Christoph Hellwig42173f62008-04-22 17:33:25 +1000494 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000498 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 0;
500
David Chinner2a82b8b2007-07-11 11:09:12 +1000501 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000502 int truncated;
503
David Chinner2a82b8b2007-07-11 11:09:12 +1000504 /*
505 * If we are using filestreams, and we have an unlinked
506 * file that we are processing the last close on, then nothing
507 * will be able to reopen and write to this file. Purge this
508 * inode from the filestreams cache so that it doesn't delay
509 * teardown of the inode.
510 */
511 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
512 xfs_filestream_deassociate(ip);
513
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000514 /*
515 * If we previously truncated this file and removed old data
516 * in the process, we want to initiate "early" writeout on
517 * the last close. This is an attempt to combat the notorious
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300518 * NULL files problem which is particularly noticeable from a
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000519 * truncate down, buffered (re-)write (delalloc), followed by
520 * a crash. What we are effectively doing here is
521 * significantly reducing the time window where we'd otherwise
522 * be exposed to that problem.
523 */
Christoph Hellwig09262b42007-08-29 11:44:50 +1000524 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Dave Chinnerdf4368a2011-06-23 01:35:00 +0000525 if (truncated) {
526 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
527 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
528 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
529 }
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000530 }
531
Dave Chinner6e857562010-12-23 12:02:31 +1100532 if (ip->i_d.di_nlink == 0)
533 return 0;
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000534
Dave Chinner6e857562010-12-23 12:02:31 +1100535 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
536 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
537 ip->i_delayed_blks > 0)) &&
538 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
539 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
540
541 /*
542 * If we can't get the iolock just skip truncating the blocks
543 * past EOF because we could deadlock with the mmap_sem
544 * otherwise. We'll get another chance to drop them once the
545 * last reference to the inode is dropped, so we'll never leak
546 * blocks permanently.
547 *
548 * Further, check if the inode is being opened, written and
549 * closed frequently and we have delayed allocation blocks
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300550 * outstanding (e.g. streaming writes from the NFS server),
Dave Chinner6e857562010-12-23 12:02:31 +1100551 * truncating the blocks past EOF will cause fragmentation to
552 * occur.
553 *
554 * In this case don't do the truncation, either, but we have to
555 * be careful how we detect this case. Blocks beyond EOF show
556 * up as i_delayed_blks even when the inode is clean, so we
557 * need to truncate them away first before checking for a dirty
558 * release. Hence on the first dirty close we will still remove
559 * the speculative allocation, but after that we will leave it
560 * in place.
561 */
562 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
563 return 0;
564
565 error = xfs_free_eofblocks(mp, ip,
566 XFS_FREE_EOF_TRYLOCK);
567 if (error)
568 return error;
569
570 /* delalloc blocks after truncation means it really is dirty */
571 if (ip->i_delayed_blks)
572 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 0;
575}
576
577/*
578 * xfs_inactive
579 *
580 * This is called when the vnode reference count for the vnode
581 * goes to zero. If the file has been unlinked, then it must
582 * now be truncated. Also, we clear all of the read-ahead state
583 * kept for the inode here since the file is now closed.
584 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000585int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000587 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000589 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 xfs_fsblock_t first_block;
591 int committed;
592 xfs_trans_t *tp;
593 xfs_mount_t *mp;
594 int error;
595 int truncate;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
598 * If the inode is already free, then there can be nothing
599 * to clean up here.
600 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +0100601 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ASSERT(ip->i_df.if_real_bytes == 0);
603 ASSERT(ip->i_df.if_broot_bytes == 0);
604 return VN_INACTIVE_CACHE;
605 }
606
607 /*
608 * Only do a truncate if it's a regular file with
609 * some actual space in it. It's OK to look at the
610 * inode's fields without the lock because we're the
611 * only one with a reference to the inode.
612 */
613 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000614 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
615 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
617
618 mp = ip->i_mount;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 error = 0;
621
622 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000623 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto out;
625
626 if (ip->i_d.di_nlink != 0) {
627 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +1000628 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100629 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +1100630 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
631 (!(ip->i_d.di_flags &
632 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
633 (ip->i_delayed_blks != 0)))) {
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000634 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000635 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100636 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 goto out;
639 }
640
641 ASSERT(ip->i_d.di_nlink == 0);
642
Christoph Hellwig7d095252009-06-08 15:33:32 +0200643 error = xfs_qm_dqattach(ip, 0);
644 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100645 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
648 if (truncate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 xfs_ilock(ip, XFS_IOLOCK_EXCL);
650
Christoph Hellwig857b9772011-07-08 14:34:30 +0200651 xfs_ioend_wait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 error = xfs_trans_reserve(tp, 0,
654 XFS_ITRUNCATE_LOG_RES(mp),
655 0, XFS_TRANS_PERM_LOG_RES,
656 XFS_ITRUNCATE_LOG_COUNT);
657 if (error) {
658 /* Don't call itruncate_cleanup */
659 ASSERT(XFS_FORCED_SHUTDOWN(mp));
660 xfs_trans_cancel(tp, 0);
661 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100662 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000666 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /*
669 * normally, we have to run xfs_itruncate_finish sync.
670 * But if filesystem is wsync and we're in the inactive
671 * path, then we know that nlink == 0, and that the
672 * xaction that made nlink == 0 is permanently committed
673 * since xfs_remove runs as a synchronous transaction.
674 */
675 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
676 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
677
678 if (error) {
679 xfs_trans_cancel(tp,
680 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
681 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100682 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
685
686 /*
687 * If we get an error while cleaning up a
688 * symlink we bail out.
689 */
690 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
691 xfs_inactive_symlink_rmt(ip, &tp) :
692 xfs_inactive_symlink_local(ip, &tp);
693
694 if (error) {
695 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100696 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Christoph Hellwig898621d2010-06-24 11:36:58 +1000699 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
701 error = xfs_trans_reserve(tp, 0,
702 XFS_IFREE_LOG_RES(mp),
703 0, XFS_TRANS_PERM_LOG_RES,
704 XFS_INACTIVE_LOG_COUNT);
705 if (error) {
706 ASSERT(XFS_FORCED_SHUTDOWN(mp));
707 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +0100708 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000712 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
715 /*
716 * If there are attributes associated with the file
717 * then blow them away now. The code calls a routine
718 * that recursively deconstructs the attribute fork.
719 * We need to just commit the current transaction
720 * because we can't use it for xfs_attr_inactive().
721 */
722 if (ip->i_d.di_anextents > 0) {
723 error = xfs_inactive_attrs(ip, &tp);
724 /*
725 * If we got an error, the transaction is already
726 * cancelled, and the inode is unlocked. Just get out.
727 */
728 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100729 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else if (ip->i_afp) {
731 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
732 }
733
734 /*
735 * Free the inode.
736 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600737 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 error = xfs_ifree(tp, ip, &free_list);
739 if (error) {
740 /*
741 * If we fail to free the inode, shut down. The cancel
742 * might do that, we need to make sure. Otherwise the
743 * inode might be lost for a long time or forever.
744 */
745 if (!XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100746 xfs_notice(mp, "%s: xfs_ifree returned error %d",
747 __func__, error);
Nathan Scott7d04a332006-06-09 14:58:38 +1000748 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
751 } else {
752 /*
753 * Credit the quota account(s). The inode is gone.
754 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200755 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /*
David Chinner78e9da72008-04-10 12:24:17 +1000758 * Just ignore errors at this point. There is nothing we can
759 * do except to try to keep going. Make sure it's not a silent
760 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
David Chinner78e9da72008-04-10 12:24:17 +1000762 error = xfs_bmap_finish(&tp, &free_list, &committed);
763 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100764 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
765 __func__, error);
David Chinner78e9da72008-04-10 12:24:17 +1000766 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
767 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100768 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
769 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /*
773 * Release the dquots held by inode, if any.
774 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200775 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
777
778 out:
779 return VN_INACTIVE_CACHE;
780}
781
Barry Naujok384f3ce2008-05-21 16:58:22 +1000782/*
783 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
784 * is allowed, otherwise it has to be an exact match. If a CI match is found,
785 * ci_name->name will point to a the actual name (caller must free) or
786 * will be set to NULL if an exact match is found.
787 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000788int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000790 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000791 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000792 xfs_inode_t **ipp,
793 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000795 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int error;
797 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000799 trace_xfs_lookup(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
802 return XFS_ERROR(EIO);
803
804 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000805 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000807
808 if (error)
809 goto out;
810
Dave Chinner7b6259e2010-06-24 11:35:17 +1000811 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000812 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000813 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000814
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000815 return 0;
816
Barry Naujok384f3ce2008-05-21 16:58:22 +1000817out_free_name:
818 if (ci_name)
819 kmem_free(ci_name->name);
820out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000821 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return error;
823}
824
Christoph Hellwig993386c2007-08-28 16:12:30 +1000825int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000827 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000828 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000829 mode_t mode,
830 xfs_dev_t rdev,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000831 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100833 int is_dir = S_ISDIR(mode);
834 struct xfs_mount *mp = dp->i_mount;
835 struct xfs_inode *ip = NULL;
836 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000837 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 xfs_bmap_free_t free_list;
839 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000840 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 uint cancel_flags;
842 int committed;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000843 prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100844 struct xfs_dquot *udqp = NULL;
845 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100847 uint log_res;
848 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000850 trace_xfs_create(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100852 if (XFS_FORCED_SHUTDOWN(mp))
853 return XFS_ERROR(EIO);
854
Nathan Scott365ca832005-06-21 15:39:12 +1000855 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000856 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000858 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /*
861 * Make sure that we have allocated dquot(s) on disk.
862 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200863 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100864 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000866 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100868 if (is_dir) {
869 rdev = 0;
870 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
871 log_res = XFS_MKDIR_LOG_RES(mp);
872 log_count = XFS_MKDIR_LOG_COUNT;
873 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
874 } else {
875 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
876 log_res = XFS_CREATE_LOG_RES(mp);
877 log_count = XFS_CREATE_LOG_COUNT;
878 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
884 * Initially assume that the file does not exist and
885 * reserve the resources for that case. If that is not
886 * the case we'll drop the one we have and get a more
887 * appropriate transaction later.
888 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100889 error = xfs_trans_reserve(tp, resblks, log_res, 0,
890 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +0200892 /* flush outstanding delalloc blocks and retry */
893 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -0500894 error = xfs_trans_reserve(tp, resblks, log_res, 0,
895 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +0200896 }
897 if (error == ENOSPC) {
898 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100900 error = xfs_trans_reserve(tp, 0, log_res, 0,
901 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 if (error) {
904 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100905 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000908 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000909 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100911 /*
912 * Check for directory link count overflow.
913 */
914 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
915 error = XFS_ERROR(EMLINK);
916 goto out_trans_cancel;
917 }
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 Hellwig993386c2007-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 Hellwig898621d2010-06-24 11:36:58 +1000952 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-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 Hellwig993386c2007-08-28 16:12:30 +10001208int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209xfs_remove(
Christoph Hellwig993386c2007-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 Hellwig993386c2007-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 Hellwig898621d2010-06-24 11:36:58 +10001273 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1274 xfs_trans_ijoin_ref(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 Hellwig993386c2007-08-28 16:12:30 +10001372int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373xfs_link(
Christoph Hellwig993386c2007-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 Hellwig993386c2007-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 Hellwig898621d2010-06-24 11:36:58 +10001419 xfs_trans_ijoin_ref(tp, sip, XFS_ILOCK_EXCL);
1420 xfs_trans_ijoin_ref(tp, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /*
1423 * If the source has too many links, we can't make any more to it.
1424 */
1425 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1426 error = XFS_ERROR(EMLINK);
1427 goto error_return;
1428 }
1429
Nathan Scott365ca832005-06-21 15:39:12 +10001430 /*
1431 * If we are using project inheritance, we only allow hard link
1432 * creation in our tree when the project IDs are the same; else
1433 * the tree quota mechanism could be circumvented.
1434 */
1435 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001436 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10001437 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10001438 goto error_return;
1439 }
1440
Barry Naujok556b8b12008-04-10 12:22:07 +10001441 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1442 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto error_return;
1444
Eric Sandeen9d87c312009-01-14 23:22:07 -06001445 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Barry Naujok556b8b12008-04-10 12:22:07 +10001447 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1448 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (error)
1450 goto abort_return;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001451 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1453
1454 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001455 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /*
1459 * If this is a synchronous mount, make sure that the
1460 * link transaction goes to disk before returning to
1461 * the user.
1462 */
1463 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1464 xfs_trans_set_sync(tp);
1465 }
1466
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001467 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (error) {
1469 xfs_bmap_cancel(&free_list);
1470 goto abort_return;
1471 }
1472
Christoph Hellwig288699f2010-06-23 18:11:15 +10001473 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 abort_return:
1476 cancel_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 error_return:
1478 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001479 std_return:
1480 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001482
Christoph Hellwig993386c2007-08-28 16:12:30 +10001483int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001485 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001486 struct xfs_name *link_name,
1487 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001488 mode_t mode,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001489 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001491 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 xfs_inode_t *ip;
1494 int error;
1495 int pathlen;
1496 xfs_bmap_free_t free_list;
1497 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001498 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 uint cancel_flags;
1500 int committed;
1501 xfs_fileoff_t first_fsb;
1502 xfs_filblks_t fs_blocks;
1503 int nmaps;
1504 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1505 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10001506 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int byte_cnt;
1508 int n;
1509 xfs_buf_t *bp;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001510 prid_t prid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 struct xfs_dquot *udqp, *gdqp;
1512 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Christoph Hellwig3937be52008-03-06 13:46:19 +11001514 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 error = 0;
1516 ip = NULL;
1517 tp = NULL;
1518
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001519 trace_xfs_symlink(dp, link_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 if (XFS_FORCED_SHUTDOWN(mp))
1522 return XFS_ERROR(EIO);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /*
1525 * Check component lengths of the target path name.
1526 */
1527 pathlen = strlen(target_path);
1528 if (pathlen >= MAXPATHLEN) /* total string too long */
1529 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001532 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001533 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001535 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 /*
1538 * Make sure that we have allocated dquot(s) on disk.
1539 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001540 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1542 if (error)
1543 goto std_return;
1544
1545 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1546 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1547 /*
1548 * The symlink will fit into the inode data fork?
1549 * There can't be any attributes so we get the whole variable part.
1550 */
1551 if (pathlen <= XFS_LITINO(mp))
1552 fs_blocks = 0;
1553 else
1554 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10001555 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
1557 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1558 if (error == ENOSPC && fs_blocks == 0) {
1559 resblks = 0;
1560 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
1561 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1562 }
1563 if (error) {
1564 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 goto error_return;
1566 }
1567
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001568 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001569 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /*
1572 * Check whether the directory allows new symlinks or not.
1573 */
1574 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
1575 error = XFS_ERROR(EPERM);
1576 goto error_return;
1577 }
1578
1579 /*
1580 * Reserve disk quota : blocks and inode.
1581 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001582 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (error)
1584 goto error_return;
1585
1586 /*
1587 * Check for ability to enter directory entry, if no space reserved.
1588 */
Barry Naujok556b8b12008-04-10 12:22:07 +10001589 error = xfs_dir_canenter(tp, dp, link_name, resblks);
1590 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 goto error_return;
1592 /*
1593 * Initialize the bmap freelist prior to calling either
1594 * bmapi or the directory create code.
1595 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001596 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /*
1599 * Allocate an inode for the symlink.
1600 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001601 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
1602 prid, resblks > 0, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (error) {
1604 if (error == ENOSPC)
1605 goto error_return;
1606 goto error1;
1607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Christoph Hellwig993386c2007-08-28 16:12:30 +10001609 /*
1610 * An error after we've joined dp to the transaction will result in the
1611 * transaction cancel unlocking dp so don't do it explicitly in the
1612 * error path.
1613 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001614 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001615 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /*
1618 * Also attach the dquot(s) to it, if applicable.
1619 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001620 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (resblks)
1623 resblks -= XFS_IALLOC_SPACE_RES(mp);
1624 /*
1625 * If the symlink will fit into the inode, write it inline.
1626 */
1627 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
1628 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
1629 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
1630 ip->i_d.di_size = pathlen;
1631
1632 /*
1633 * The inode was initially created in extent format.
1634 */
1635 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
1636 ip->i_df.if_flags |= XFS_IFINLINE;
1637
1638 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
1639 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
1640
1641 } else {
1642 first_fsb = 0;
1643 nmaps = SYMLINK_MAPS;
1644
1645 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
1646 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
1647 &first_block, resblks, mval, &nmaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001648 &free_list);
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001649 if (error)
1650 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (resblks)
1653 resblks -= fs_blocks;
1654 ip->i_d.di_size = pathlen;
1655 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1656
1657 cur_chunk = target_path;
1658 for (n = 0; n < nmaps; n++) {
1659 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1660 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1661 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
1662 BTOBB(byte_cnt), 0);
1663 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1664 if (pathlen < byte_cnt) {
1665 byte_cnt = pathlen;
1666 }
1667 pathlen -= byte_cnt;
1668
1669 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
1670 cur_chunk += byte_cnt;
1671
1672 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
1673 }
1674 }
1675
1676 /*
1677 * Create the directory entry for the symlink.
1678 */
Barry Naujok556b8b12008-04-10 12:22:07 +10001679 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
1680 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001681 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001682 goto error2;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001683 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1685
1686 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * If this is a synchronous mount, make sure that the
1688 * symlink transaction goes to disk before returning to
1689 * the user.
1690 */
1691 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1692 xfs_trans_set_sync(tp);
1693 }
1694
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001695 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (error) {
1697 goto error2;
1698 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001699 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001700 xfs_qm_dqrele(udqp);
1701 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Christoph Hellwig288699f2010-06-23 18:11:15 +10001703 *ipp = ip;
1704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 error2:
1707 IRELE(ip);
1708 error1:
1709 xfs_bmap_cancel(&free_list);
1710 cancel_flags |= XFS_TRANS_ABORT;
1711 error_return:
1712 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001713 xfs_qm_dqrele(udqp);
1714 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Christoph Hellwig993386c2007-08-28 16:12:30 +10001716 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001718 std_return:
1719 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722int
Christoph Hellwig993386c2007-08-28 16:12:30 +10001723xfs_set_dmattrs(
1724 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10001726 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001728 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int error;
1731
1732 if (!capable(CAP_SYS_ADMIN))
1733 return XFS_ERROR(EPERM);
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (XFS_FORCED_SHUTDOWN(mp))
1736 return XFS_ERROR(EIO);
1737
1738 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1739 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1740 if (error) {
1741 xfs_trans_cancel(tp, 0);
1742 return error;
1743 }
1744 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001745 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Christoph Hellwig613d7042007-10-11 17:44:08 +10001747 ip->i_d.di_dmevmask = evmask;
1748 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001751 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 return error;
1754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756/*
1757 * xfs_alloc_file_space()
1758 * This routine allocates disk space for the given file.
1759 *
1760 * If alloc_type == 0, this request is for an ALLOCSP type
1761 * request which will change the file size. In this case, no
1762 * DMAPI event will be generated by the call. A TRUNCATE event
1763 * will be generated later by xfs_setattr.
1764 *
1765 * If alloc_type != 0, this request is for a RESVSP type
1766 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1767 * lower block boundary byte address is less than the file's
1768 * length.
1769 *
1770 * RETURNS:
1771 * 0 on success
1772 * errno on error
1773 *
1774 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001775STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776xfs_alloc_file_space(
1777 xfs_inode_t *ip,
1778 xfs_off_t offset,
1779 xfs_off_t len,
1780 int alloc_type,
1781 int attr_flags)
1782{
Nathan Scottdd9f4382006-01-11 15:28:28 +11001783 xfs_mount_t *mp = ip->i_mount;
1784 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 xfs_filblks_t allocated_fsb;
1786 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001787 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001789 xfs_fsblock_t firstfsb;
1790 int nimaps;
1791 int bmapi_flag;
1792 int quota_flag;
1793 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001795 xfs_bmbt_irec_t imaps[1], *imapp;
1796 xfs_bmap_free_t free_list;
1797 uint qblocks, resblks, resrtextents;
1798 int committed;
1799 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001801 trace_xfs_alloc_file_space(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 if (XFS_FORCED_SHUTDOWN(mp))
1804 return XFS_ERROR(EIO);
1805
Christoph Hellwig7d095252009-06-08 15:33:32 +02001806 error = xfs_qm_dqattach(ip, 0);
1807 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return error;
1809
1810 if (len <= 0)
1811 return XFS_ERROR(EINVAL);
1812
David Chinner957d0eb2007-06-18 16:50:37 +10001813 rt = XFS_IS_REALTIME_INODE(ip);
1814 extsz = xfs_get_extsz_hint(ip);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11001818 nimaps = 1;
Dave Chinner44722352010-08-24 12:02:11 +10001819 bmapi_flag = XFS_BMAPI_WRITE | alloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1821 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001824 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001827 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Nathan Scottdd9f4382006-01-11 15:28:28 +11001829 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11001830 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11001831 */
1832 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001834 do_div(s, extsz);
1835 s *= extsz;
1836 e = startoffset_fsb + allocatesize_fsb;
1837 if ((temp = do_mod(startoffset_fsb, extsz)))
1838 e += temp;
1839 if ((temp = do_mod(e, extsz)))
1840 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001842 s = 0;
1843 e = allocatesize_fsb;
1844 }
1845
Dave Chinner72656c42010-09-03 12:19:33 +10001846 /*
1847 * The transaction reservation is limited to a 32-bit block
1848 * count, hence we need to limit the number of blocks we are
1849 * trying to reserve to avoid an overflow. We can't allocate
1850 * more than @nimaps extents, and an extent is limited on disk
1851 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1852 */
1853 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
Nathan Scottdd9f4382006-01-11 15:28:28 +11001854 if (unlikely(rt)) {
Dave Chinner72656c42010-09-03 12:19:33 +10001855 resrtextents = qblocks = resblks;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001856 resrtextents /= mp->m_sb.sb_rextsize;
1857 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1858 quota_flag = XFS_QMOPT_RES_RTBLKS;
1859 } else {
1860 resrtextents = 0;
Dave Chinner72656c42010-09-03 12:19:33 +10001861 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001862 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864
1865 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001866 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001869 error = xfs_trans_reserve(tp, resblks,
1870 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 XFS_TRANS_PERM_LOG_RES,
1872 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001874 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 */
1876 if (error) {
1877 /*
1878 * Free the transaction structure.
1879 */
1880 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1881 xfs_trans_cancel(tp, 0);
1882 break;
1883 }
1884 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001885 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1886 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (error)
1888 goto error1;
1889
Christoph Hellwig898621d2010-06-24 11:36:58 +10001890 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001893 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001895 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001896 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11001897 allocatesize_fsb, bmapi_flag,
1898 &firstfsb, 0, imapp, &nimaps,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001899 &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (error) {
1901 goto error0;
1902 }
1903
1904 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001905 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001907 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (error) {
1909 goto error0;
1910 }
1911
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001912 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1914 if (error) {
1915 break;
1916 }
1917
1918 allocated_fsb = imapp->br_blockcount;
1919
Nathan Scottdd9f4382006-01-11 15:28:28 +11001920 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 error = XFS_ERROR(ENOSPC);
1922 break;
1923 }
1924
1925 startoffset_fsb += allocated_fsb;
1926 allocatesize_fsb -= allocated_fsb;
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 return error;
1930
Nathan Scottdd9f4382006-01-11 15:28:28 +11001931error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001933 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001934
1935error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1937 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001938 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941/*
1942 * Zero file bytes between startoff and endoff inclusive.
1943 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001944 *
1945 * This function is used by xfs_free_file_space() to zero
1946 * partial blocks when the range to free is not block aligned.
1947 * When unreserving space with boundaries that are not block
1948 * aligned we round up the start and round down the end
1949 * boundaries and then use this function to zero the parts of
1950 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 */
1952STATIC int
1953xfs_zero_remaining_bytes(
1954 xfs_inode_t *ip,
1955 xfs_off_t startoff,
1956 xfs_off_t endoff)
1957{
1958 xfs_bmbt_irec_t imap;
1959 xfs_fileoff_t offset_fsb;
1960 xfs_off_t lastoffset;
1961 xfs_off_t offset;
1962 xfs_buf_t *bp;
1963 xfs_mount_t *mp = ip->i_mount;
1964 int nimap;
1965 int error = 0;
1966
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001967 /*
1968 * Avoid doing I/O beyond eof - it's not necessary
1969 * since nothing can read beyond eof. The space will
1970 * be zeroed when the file is extended anyway.
1971 */
1972 if (startoff >= ip->i_size)
1973 return 0;
1974
1975 if (endoff > ip->i_size)
1976 endoff = ip->i_size;
1977
Dave Chinner686865f2010-09-24 20:07:47 +10001978 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1979 mp->m_rtdev_targp : mp->m_ddev_targp,
1980 mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11001981 if (!bp)
1982 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1985 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1986 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001987 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001988 NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (error || nimap < 1)
1990 break;
1991 ASSERT(imap.br_blockcount >= 1);
1992 ASSERT(imap.br_startoff == offset_fsb);
1993 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1994 if (lastoffset > endoff)
1995 lastoffset = endoff;
1996 if (imap.br_startblock == HOLESTARTBLOCK)
1997 continue;
1998 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1999 if (imap.br_state == XFS_EXT_UNWRITTEN)
2000 continue;
2001 XFS_BUF_UNDONE(bp);
2002 XFS_BUF_UNWRITE(bp);
2003 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002004 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002006 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002007 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2009 mp, bp, XFS_BUF_ADDR(bp));
2010 break;
2011 }
2012 memset(XFS_BUF_PTR(bp) +
2013 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2014 0, lastoffset - offset + 1);
2015 XFS_BUF_UNDONE(bp);
2016 XFS_BUF_UNREAD(bp);
2017 XFS_BUF_WRITE(bp);
2018 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002019 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002020 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2022 mp, bp, XFS_BUF_ADDR(bp));
2023 break;
2024 }
2025 }
2026 xfs_buf_free(bp);
2027 return error;
2028}
2029
2030/*
2031 * xfs_free_file_space()
2032 * This routine frees disk space for the given file.
2033 *
2034 * This routine is only called by xfs_change_file_space
2035 * for an UNRESVSP type call.
2036 *
2037 * RETURNS:
2038 * 0 on success
2039 * errno on error
2040 *
2041 */
2042STATIC int
2043xfs_free_file_space(
2044 xfs_inode_t *ip,
2045 xfs_off_t offset,
2046 xfs_off_t len,
2047 int attr_flags)
2048{
2049 int committed;
2050 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 xfs_fileoff_t endoffset_fsb;
2052 int error;
2053 xfs_fsblock_t firstfsb;
2054 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 xfs_bmbt_irec_t imap;
2056 xfs_off_t ioffset;
2057 xfs_extlen_t mod=0;
2058 xfs_mount_t *mp;
2059 int nimap;
2060 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002061 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 int rt;
2063 xfs_fileoff_t startoffset_fsb;
2064 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002065 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 mp = ip->i_mount;
2068
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002069 trace_xfs_free_file_space(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002070
Christoph Hellwig7d095252009-06-08 15:33:32 +02002071 error = xfs_qm_dqattach(ip, 0);
2072 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return error;
2074
2075 error = 0;
2076 if (len <= 0) /* if nothing being freed */
2077 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002078 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Christoph Hellwig288699f2010-06-23 18:11:15 +10002080 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002082 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002083 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002084 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002086 /* wait for the completion of any pending DIOs */
2087 xfs_ioend_wait(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11002088 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002089
Tim Shimmine6a4b372007-11-23 16:30:42 +11002090 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002092
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002093 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002094 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002095 if (error)
2096 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002097 }
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /*
2100 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002101 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2103 * will take care of it for us.
2104 */
Eric Sandeen62118702008-03-06 13:44:28 +11002105 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002107 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002108 1, 0, NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (error)
2110 goto out_unlock_iolock;
2111 ASSERT(nimap == 0 || nimap == 1);
2112 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2113 xfs_daddr_t block;
2114
2115 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2116 block = imap.br_startblock;
2117 mod = do_div(block, mp->m_sb.sb_rextsize);
2118 if (mod)
2119 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2120 }
2121 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002122 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002123 1, 0, NULL, 0, &imap, &nimap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (error)
2125 goto out_unlock_iolock;
2126 ASSERT(nimap == 0 || nimap == 1);
2127 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2128 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2129 mod++;
2130 if (mod && (mod != mp->m_sb.sb_rextsize))
2131 endoffset_fsb -= mod;
2132 }
2133 }
2134 if ((done = (endoffset_fsb <= startoffset_fsb)))
2135 /*
2136 * One contiguous piece to clear
2137 */
2138 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2139 else {
2140 /*
2141 * Some full blocks, possibly two pieces to clear
2142 */
2143 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2144 error = xfs_zero_remaining_bytes(ip, offset,
2145 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2146 if (!error &&
2147 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2148 error = xfs_zero_remaining_bytes(ip,
2149 XFS_FSB_TO_B(mp, endoffset_fsb),
2150 offset + len - 1);
2151 }
2152
2153 /*
2154 * free file space until done or until there is an error
2155 */
2156 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2157 while (!error && !done) {
2158
2159 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002160 * allocate and setup the transaction. Allow this
2161 * transaction to dip into the reserve blocks to ensure
2162 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 */
2164 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002165 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 error = xfs_trans_reserve(tp,
2167 resblks,
2168 XFS_WRITE_LOG_RES(mp),
2169 0,
2170 XFS_TRANS_PERM_LOG_RES,
2171 XFS_WRITE_LOG_COUNT);
2172
2173 /*
2174 * check for running out of space
2175 */
2176 if (error) {
2177 /*
2178 * Free the transaction structure.
2179 */
2180 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2181 xfs_trans_cancel(tp, 0);
2182 break;
2183 }
2184 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002185 error = xfs_trans_reserve_quota(tp, mp,
2186 ip->i_udquot, ip->i_gdquot,
2187 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (error)
2189 goto error1;
2190
Christoph Hellwig898621d2010-06-24 11:36:58 +10002191 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 /*
2194 * issue the bunmapi() call to free the blocks
2195 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002196 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002197 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 endoffset_fsb - startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002199 0, 2, &firstfsb, &free_list, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (error) {
2201 goto error0;
2202 }
2203
2204 /*
2205 * complete the transaction
2206 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002207 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (error) {
2209 goto error0;
2210 }
2211
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002212 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2214 }
2215
2216 out_unlock_iolock:
2217 if (need_iolock)
2218 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2219 return error;
2220
2221 error0:
2222 xfs_bmap_cancel(&free_list);
2223 error1:
2224 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2225 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2226 XFS_ILOCK_EXCL);
2227 return error;
2228}
2229
2230/*
2231 * xfs_change_file_space()
2232 * This routine allocates or frees disk space for the given file.
2233 * The user specified parameters are checked for alignment and size
2234 * limitations.
2235 *
2236 * RETURNS:
2237 * 0 on success
2238 * errno on error
2239 *
2240 */
2241int
2242xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002243 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 int cmd,
2245 xfs_flock64_t *bf,
2246 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 int attr_flags)
2248{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002249 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 int clrprealloc;
2251 int error;
2252 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 int setprealloc;
2254 xfs_off_t startoffset;
2255 xfs_off_t llen;
2256 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002257 struct iattr iattr;
Dave Chinner44722352010-08-24 12:02:11 +10002258 int prealloc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Christoph Hellwig993386c2007-08-28 16:12:30 +10002260 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return XFS_ERROR(EINVAL);
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 switch (bf->l_whence) {
2264 case 0: /*SEEK_SET*/
2265 break;
2266 case 1: /*SEEK_CUR*/
2267 bf->l_start += offset;
2268 break;
2269 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002270 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 break;
2272 default:
2273 return XFS_ERROR(EINVAL);
2274 }
2275
2276 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2277
2278 if ( (bf->l_start < 0)
2279 || (bf->l_start > XFS_MAXIOFFSET(mp))
2280 || (bf->l_start + llen < 0)
2281 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2282 return XFS_ERROR(EINVAL);
2283
2284 bf->l_whence = 0;
2285
2286 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002287 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 /*
2290 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2291 * file space.
2292 * These calls do NOT zero the data space allocated to the file,
2293 * nor do they change the file size.
2294 *
2295 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2296 * space.
2297 * These calls cause the new file data to be zeroed and the file
2298 * size to be changed.
2299 */
2300 setprealloc = clrprealloc = 0;
Dave Chinner44722352010-08-24 12:02:11 +10002301 prealloc_type = XFS_BMAPI_PREALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 switch (cmd) {
Dave Chinner44722352010-08-24 12:02:11 +10002304 case XFS_IOC_ZERO_RANGE:
2305 prealloc_type |= XFS_BMAPI_CONVERT;
2306 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2307 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 case XFS_IOC_RESVSP:
2309 case XFS_IOC_RESVSP64:
2310 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
Dave Chinner44722352010-08-24 12:02:11 +10002311 prealloc_type, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (error)
2313 return error;
2314 setprealloc = 1;
2315 break;
2316
2317 case XFS_IOC_UNRESVSP:
2318 case XFS_IOC_UNRESVSP64:
2319 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2320 attr_flags)))
2321 return error;
2322 break;
2323
2324 case XFS_IOC_ALLOCSP:
2325 case XFS_IOC_ALLOCSP64:
2326 case XFS_IOC_FREESP:
2327 case XFS_IOC_FREESP64:
2328 if (startoffset > fsize) {
2329 error = xfs_alloc_file_space(ip, fsize,
2330 startoffset - fsize, 0, attr_flags);
2331 if (error)
2332 break;
2333 }
2334
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002335 iattr.ia_valid = ATTR_SIZE;
2336 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02002338 error = xfs_setattr_size(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 if (error)
2341 return error;
2342
2343 clrprealloc = 1;
2344 break;
2345
2346 default:
2347 ASSERT(0);
2348 return XFS_ERROR(EINVAL);
2349 }
2350
2351 /*
2352 * update the inode timestamp, mode, and prealloc flag bits
2353 */
2354 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2355
2356 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2357 0, 0, 0))) {
2358 /* ASSERT(0); */
2359 xfs_trans_cancel(tp, 0);
2360 return error;
2361 }
2362
2363 xfs_ilock(ip, XFS_ILOCK_EXCL);
2364
Christoph Hellwig898621d2010-06-24 11:36:58 +10002365 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002367 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 ip->i_d.di_mode &= ~S_ISUID;
2369
2370 /*
2371 * Note that we don't have to worry about mandatory
2372 * file locking being disabled here because we only
2373 * clear the S_ISGID bit if the Group execute bit is
2374 * on, but if it was on then mandatory locking wouldn't
2375 * have been enabled.
2376 */
2377 if (ip->i_d.di_mode & S_IXGRP)
2378 ip->i_d.di_mode &= ~S_ISGID;
2379
Dave Chinnerdcd79a12010-09-28 12:27:25 +10002380 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 if (setprealloc)
2383 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2384 else if (clrprealloc)
2385 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2386
2387 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinner82878892011-03-26 09:13:08 +11002388 if (attr_flags & XFS_ATTR_SYNC)
2389 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002391 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2394
2395 return error;
2396}