blob: 0176bb21f09a5c3c0795f6f6932e07f92643c94a [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.
Dave Chinner19de7352013-04-03 16:11:18 +11003 * Copyright (c) 2012 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_log.h"
25#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_quota.h"
44#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110045#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100048#include "xfs_filestream.h"
Christoph Hellwig993386c12007-08-28 16:12:30 +100049#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110051#include "xfs_icache.h"
Dave Chinner19de7352013-04-03 16:11:18 +110052#include "xfs_symlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
David Chinner92dfe8d2007-05-24 15:22:19 +100056 * This is called by xfs_inactive to free any blocks beyond eof
57 * when the link count isn't zero and by xfs_dm_punch_hole() when
58 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Brian Foster40165e22012-11-06 09:50:41 -050060int
David Chinner92dfe8d2007-05-24 15:22:19 +100061xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +100063 xfs_inode_t *ip,
Christoph Hellwig5a153222012-07-04 11:13:32 -040064 bool need_iolock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 xfs_trans_t *tp;
67 int error;
68 xfs_fileoff_t end_fsb;
69 xfs_fileoff_t last_fsb;
70 xfs_filblks_t map_len;
71 int nimaps;
72 xfs_bmbt_irec_t imap;
73
74 /*
75 * Figure out if there are any blocks beyond the end
76 * of the file. If not, then there is nothing to do.
77 */
Christoph Hellwigce7ae1512011-12-18 20:00:11 +000078 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
Dave Chinner32972382012-06-08 15:44:54 +100079 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Kulikov Vasiliy3f348852010-07-20 17:54:28 +100080 if (last_fsb <= end_fsb)
Jesper Juhl014c2542006-01-15 02:37:08 +010081 return 0;
Kulikov Vasiliy3f348852010-07-20 17:54:28 +100082 map_len = last_fsb - end_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 nimaps = 1;
85 xfs_ilock(ip, XFS_ILOCK_SHARED);
Dave Chinner5c8ed202011-09-18 20:40:45 +000086 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 xfs_iunlock(ip, XFS_ILOCK_SHARED);
88
89 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +110090 (imap.br_startblock != HOLESTARTBLOCK ||
91 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /*
93 * Attach the dquots to the inode up front.
94 */
Christoph Hellwig7d095252009-06-08 15:33:32 +020095 error = xfs_qm_dqattach(ip, 0);
96 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +010097 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /*
100 * There are blocks after the end of file.
101 * Free them up now by truncating the file to
102 * its current size.
103 */
104 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
105
Christoph Hellwig5a153222012-07-04 11:13:32 -0400106 if (need_iolock) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000107 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
108 xfs_trans_cancel(tp, 0);
Brian Foster40165e22012-11-06 09:50:41 -0500109 return EAGAIN;
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000110 }
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 error = xfs_trans_reserve(tp, 0,
114 XFS_ITRUNCATE_LOG_RES(mp),
115 0, XFS_TRANS_PERM_LOG_RES,
116 XFS_ITRUNCATE_LOG_COUNT);
117 if (error) {
118 ASSERT(XFS_FORCED_SHUTDOWN(mp));
119 xfs_trans_cancel(tp, 0);
Christoph Hellwig5a153222012-07-04 11:13:32 -0400120 if (need_iolock)
121 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100122 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
125 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000126 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000128 /*
129 * Do not update the on-disk file size. If we update the
130 * on-disk file size and then the system crashes before the
131 * contents of the file are flushed to disk then the files
132 * may be full of holes (ie NULL files bug).
133 */
134 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000135 XFS_ISIZE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (error) {
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200137 /*
138 * If we get an error at this point we simply don't
139 * bother truncating the file.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 xfs_trans_cancel(tp,
142 (XFS_TRANS_RELEASE_LOG_RES |
143 XFS_TRANS_ABORT));
144 } else {
145 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000146 XFS_TRANS_RELEASE_LOG_RES);
Brian Foster27b52862012-11-06 09:50:38 -0500147 if (!error)
148 xfs_inode_clear_eofblocks_tag(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Christoph Hellwig5a153222012-07-04 11:13:32 -0400150
151 xfs_iunlock(ip, XFS_ILOCK_EXCL);
152 if (need_iolock)
153 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100155 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Christoph Hellwig993386c12007-08-28 16:12:30 +1000158int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159xfs_release(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000160 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Christoph Hellwig993386c12007-08-28 16:12:30 +1000162 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int error;
164
Christoph Hellwig42173f62008-04-22 17:33:25 +1000165 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000169 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171
David Chinner2a82b8b2007-07-11 11:09:12 +1000172 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000173 int truncated;
174
David Chinner2a82b8b2007-07-11 11:09:12 +1000175 /*
176 * If we are using filestreams, and we have an unlinked
177 * file that we are processing the last close on, then nothing
178 * will be able to reopen and write to this file. Purge this
179 * inode from the filestreams cache so that it doesn't delay
180 * teardown of the inode.
181 */
182 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
183 xfs_filestream_deassociate(ip);
184
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000185 /*
186 * If we previously truncated this file and removed old data
187 * in the process, we want to initiate "early" writeout on
188 * the last close. This is an attempt to combat the notorious
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300189 * NULL files problem which is particularly noticeable from a
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000190 * truncate down, buffered (re-)write (delalloc), followed by
191 * a crash. What we are effectively doing here is
192 * significantly reducing the time window where we'd otherwise
193 * be exposed to that problem.
194 */
Christoph Hellwig09262b432007-08-29 11:44:50 +1000195 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Dave Chinnerdf4368a2011-06-23 01:35:00 +0000196 if (truncated) {
197 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
Dave Chinner4bc1ea62012-11-12 22:53:56 +1100198 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
199 error = -filemap_flush(VFS_I(ip)->i_mapping);
200 if (error)
201 return error;
202 }
Dave Chinnerdf4368a2011-06-23 01:35:00 +0000203 }
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +1000204 }
205
Dave Chinner6e857562010-12-23 12:02:31 +1100206 if (ip->i_d.di_nlink == 0)
207 return 0;
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000208
Brian Foster72b53ef2012-11-06 09:50:40 -0500209 if (xfs_can_free_eofblocks(ip, false)) {
Dave Chinner6e857562010-12-23 12:02:31 +1100210
211 /*
212 * If we can't get the iolock just skip truncating the blocks
213 * past EOF because we could deadlock with the mmap_sem
214 * otherwise. We'll get another chance to drop them once the
215 * last reference to the inode is dropped, so we'll never leak
216 * blocks permanently.
217 *
218 * Further, check if the inode is being opened, written and
219 * closed frequently and we have delayed allocation blocks
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300220 * outstanding (e.g. streaming writes from the NFS server),
Dave Chinner6e857562010-12-23 12:02:31 +1100221 * truncating the blocks past EOF will cause fragmentation to
222 * occur.
223 *
224 * In this case don't do the truncation, either, but we have to
225 * be careful how we detect this case. Blocks beyond EOF show
226 * up as i_delayed_blks even when the inode is clean, so we
227 * need to truncate them away first before checking for a dirty
228 * release. Hence on the first dirty close we will still remove
229 * the speculative allocation, but after that we will leave it
230 * in place.
231 */
232 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
233 return 0;
234
Christoph Hellwig5a153222012-07-04 11:13:32 -0400235 error = xfs_free_eofblocks(mp, ip, true);
Brian Foster40165e22012-11-06 09:50:41 -0500236 if (error && error != EAGAIN)
Dave Chinner6e857562010-12-23 12:02:31 +1100237 return error;
238
239 /* delalloc blocks after truncation means it really is dirty */
240 if (ip->i_delayed_blks)
241 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
246/*
247 * xfs_inactive
248 *
249 * This is called when the vnode reference count for the vnode
250 * goes to zero. If the file has been unlinked, then it must
251 * now be truncated. Also, we clear all of the read-ahead state
252 * kept for the inode here since the file is now closed.
253 */
Christoph Hellwig993386c12007-08-28 16:12:30 +1000254int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255xfs_inactive(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000256 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000258 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 xfs_fsblock_t first_block;
260 int committed;
261 xfs_trans_t *tp;
262 xfs_mount_t *mp;
263 int error;
Christoph Hellwigb373e982012-07-04 11:13:29 -0400264 int truncate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /*
267 * If the inode is already free, then there can be nothing
268 * to clean up here.
269 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +0100270 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ASSERT(ip->i_df.if_real_bytes == 0);
272 ASSERT(ip->i_df.if_broot_bytes == 0);
273 return VN_INACTIVE_CACHE;
274 }
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 mp = ip->i_mount;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 error = 0;
279
280 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000281 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 goto out;
283
284 if (ip->i_d.di_nlink != 0) {
Brian Foster72b53ef2012-11-06 09:50:40 -0500285 /*
286 * force is true because we are evicting an inode from the
287 * cache. Post-eof blocks must be freed, lest we end up with
288 * broken free space accounting.
289 */
290 if (xfs_can_free_eofblocks(ip, true)) {
Christoph Hellwig5a153222012-07-04 11:13:32 -0400291 error = xfs_free_eofblocks(mp, ip, false);
David Chinner92dfe8d2007-05-24 15:22:19 +1000292 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100293 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 goto out;
296 }
297
Christoph Hellwigb373e982012-07-04 11:13:29 -0400298 if (S_ISREG(ip->i_d.di_mode) &&
299 (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
300 ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
301 truncate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Christoph Hellwig7d095252009-06-08 15:33:32 +0200303 error = xfs_qm_dqattach(ip, 0);
304 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100305 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
Christoph Hellwigb373e982012-07-04 11:13:29 -0400308 error = xfs_trans_reserve(tp, 0,
309 (truncate || S_ISLNK(ip->i_d.di_mode)) ?
310 XFS_ITRUNCATE_LOG_RES(mp) :
311 XFS_IFREE_LOG_RES(mp),
312 0,
313 XFS_TRANS_PERM_LOG_RES,
314 XFS_ITRUNCATE_LOG_COUNT);
315 if (error) {
316 ASSERT(XFS_FORCED_SHUTDOWN(mp));
317 xfs_trans_cancel(tp, 0);
318 return VN_INACTIVE_CACHE;
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Christoph Hellwig0b561852012-07-04 11:13:31 -0400321 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigb373e982012-07-04 11:13:29 -0400322 xfs_trans_ijoin(tp, ip, 0);
323
324 if (S_ISLNK(ip->i_d.di_mode)) {
325 /*
326 * Zero length symlinks _can_ exist.
327 */
328 if (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) {
329 error = xfs_inactive_symlink_rmt(ip, &tp);
330 if (error)
331 goto out_cancel;
332 } else if (ip->i_df.if_bytes > 0) {
333 xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
334 XFS_DATA_FORK);
335 ASSERT(ip->i_df.if_bytes == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Christoph Hellwigb373e982012-07-04 11:13:29 -0400337 } else if (truncate) {
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000338 ip->i_d.di_size = 0;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000339 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
340
341 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
Christoph Hellwigb373e982012-07-04 11:13:29 -0400342 if (error)
343 goto out_cancel;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000344
345 ASSERT(ip->i_d.di_nextents == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 /*
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400349 * If there are attributes associated with the file then blow them away
350 * now. The code calls a routine that recursively deconstructs the
351 * attribute fork. We need to just commit the current transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * because we can't use it for xfs_attr_inactive().
353 */
354 if (ip->i_d.di_anextents > 0) {
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400355 ASSERT(ip->i_d.di_forkoff != 0);
356
357 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400358 if (error)
Christoph Hellwig0b561852012-07-04 11:13:31 -0400359 goto out_unlock;
360
361 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400362
363 error = xfs_attr_inactive(ip);
364 if (error)
Christoph Hellwig0b561852012-07-04 11:13:31 -0400365 goto out;
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400366
367 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
368 error = xfs_trans_reserve(tp, 0,
369 XFS_IFREE_LOG_RES(mp),
370 0, XFS_TRANS_PERM_LOG_RES,
371 XFS_INACTIVE_LOG_COUNT);
Christoph Hellwig0b561852012-07-04 11:13:31 -0400372 if (error) {
373 xfs_trans_cancel(tp, 0);
374 goto out;
375 }
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400376
377 xfs_ilock(ip, XFS_ILOCK_EXCL);
378 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400381 if (ip->i_afp)
382 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
383
384 ASSERT(ip->i_d.di_anextents == 0);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * Free the inode.
388 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600389 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 error = xfs_ifree(tp, ip, &free_list);
391 if (error) {
392 /*
393 * If we fail to free the inode, shut down. The cancel
394 * might do that, we need to make sure. Otherwise the
395 * inode might be lost for a long time or forever.
396 */
397 if (!XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100398 xfs_notice(mp, "%s: xfs_ifree returned error %d",
399 __func__, error);
Nathan Scott7d04a332006-06-09 14:58:38 +1000400 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
403 } else {
404 /*
405 * Credit the quota account(s). The inode is gone.
406 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200407 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
David Chinner78e9da72008-04-10 12:24:17 +1000410 * Just ignore errors at this point. There is nothing we can
411 * do except to try to keep going. Make sure it's not a silent
412 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
David Chinner78e9da72008-04-10 12:24:17 +1000414 error = xfs_bmap_finish(&tp, &free_list, &committed);
415 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100416 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
417 __func__, error);
David Chinner78e9da72008-04-10 12:24:17 +1000418 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
419 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100420 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
421 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
425 * Release the dquots held by inode, if any.
426 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200427 xfs_qm_dqdetach(ip);
Christoph Hellwig0b561852012-07-04 11:13:31 -0400428out_unlock:
429 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigb373e982012-07-04 11:13:29 -0400430out:
431 return VN_INACTIVE_CACHE;
432out_cancel:
433 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
Christoph Hellwig0b561852012-07-04 11:13:31 -0400434 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Barry Naujok384f3ce2008-05-21 16:58:22 +1000437/*
438 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
439 * is allowed, otherwise it has to be an exact match. If a CI match is found,
440 * ci_name->name will point to a the actual name (caller must free) or
441 * will be set to NULL if an exact match is found.
442 */
Christoph Hellwig993386c12007-08-28 16:12:30 +1000443int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444xfs_lookup(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000445 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000446 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000447 xfs_inode_t **ipp,
448 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000450 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int error;
452 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000454 trace_xfs_lookup(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
457 return XFS_ERROR(EIO);
458
459 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000460 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000462
463 if (error)
464 goto out;
465
Dave Chinner7b6259e2010-06-24 11:35:17 +1000466 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000467 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000468 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000469
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000470 return 0;
471
Barry Naujok384f3ce2008-05-21 16:58:22 +1000472out_free_name:
473 if (ci_name)
474 kmem_free(ci_name->name);
475out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000476 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return error;
478}
479
Christoph Hellwig993386c12007-08-28 16:12:30 +1000480int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481xfs_create(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000482 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000483 struct xfs_name *name,
Al Viro576b1d62011-07-26 02:50:15 -0400484 umode_t mode,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000485 xfs_dev_t rdev,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000486 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100488 int is_dir = S_ISDIR(mode);
489 struct xfs_mount *mp = dp->i_mount;
490 struct xfs_inode *ip = NULL;
491 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000492 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 xfs_bmap_free_t free_list;
494 xfs_fsblock_t first_block;
Thiago Farina667a9292012-11-12 21:32:59 -0200495 bool unlock_dp_on_error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 uint cancel_flags;
497 int committed;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000498 prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100499 struct xfs_dquot *udqp = NULL;
500 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100502 uint log_res;
503 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000505 trace_xfs_create(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100507 if (XFS_FORCED_SHUTDOWN(mp))
508 return XFS_ERROR(EIO);
509
Nathan Scott365ca832005-06-21 15:39:12 +1000510 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000511 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000513 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /*
516 * Make sure that we have allocated dquot(s) on disk.
517 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200518 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100519 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000521 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100523 if (is_dir) {
524 rdev = 0;
525 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
526 log_res = XFS_MKDIR_LOG_RES(mp);
527 log_count = XFS_MKDIR_LOG_COUNT;
528 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
529 } else {
530 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
531 log_res = XFS_CREATE_LOG_RES(mp);
532 log_count = XFS_CREATE_LOG_COUNT;
533 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * Initially assume that the file does not exist and
540 * reserve the resources for that case. If that is not
541 * the case we'll drop the one we have and get a more
542 * appropriate transaction later.
543 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100544 error = xfs_trans_reserve(tp, resblks, log_res, 0,
545 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +0200547 /* flush outstanding delalloc blocks and retry */
Dave Chinner9aa05002012-10-08 21:56:04 +1100548 xfs_flush_inodes(mp);
Christoph Hellwig4734d402009-09-09 18:19:02 -0500549 error = xfs_trans_reserve(tp, resblks, log_res, 0,
550 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +0200551 }
552 if (error == ENOSPC) {
553 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100555 error = xfs_trans_reserve(tp, 0, log_res, 0,
556 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 if (error) {
559 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100560 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000563 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Thiago Farina667a9292012-11-12 21:32:59 -0200564 unlock_dp_on_error = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100566 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /*
569 * Reserve disk quota and the inode.
570 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200571 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100573 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Barry Naujok556b8b12008-04-10 12:22:07 +1000575 error = xfs_dir_canenter(tp, dp, name, resblks);
576 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100577 goto out_trans_cancel;
578
579 /*
580 * A newly created regular or special file just has one directory
581 * entry pointing to them, but a directory also the "." entry
582 * pointing to itself.
583 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000584 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100585 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (error) {
587 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100588 goto out_trans_cancel;
589 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /*
Christoph Hellwig993386c12007-08-28 16:12:30 +1000593 * Now we join the directory inode to the transaction. We do not do it
594 * earlier because xfs_dir_ialloc might commit the previous transaction
595 * (and release all the locks). An error from here on will result in
596 * the transaction cancel unlocking dp so don't do it explicitly in the
597 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000599 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Thiago Farina667a9292012-11-12 21:32:59 -0200600 unlock_dp_on_error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Barry Naujok556b8b12008-04-10 12:22:07 +1000602 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000603 &first_block, &free_list, resblks ?
604 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (error) {
606 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100607 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000609 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
611
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100612 if (is_dir) {
613 error = xfs_dir_init(tp, ip, dp);
614 if (error)
615 goto out_bmap_cancel;
616
617 error = xfs_bumplink(tp, dp);
618 if (error)
619 goto out_bmap_cancel;
620 }
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * If this is a synchronous mount, make sure that the
624 * create transaction goes to disk before returning to
625 * the user.
626 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100627 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * Attach the dquot(s) to the inodes and modify them incore.
632 * These ids of the inode couldn't have changed since the new
633 * inode has been locked ever since it was created.
634 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200635 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100637 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100638 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000639 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000641 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000642 if (error)
643 goto out_release_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Christoph Hellwig7d095252009-06-08 15:33:32 +0200645 xfs_qm_dqrele(udqp);
646 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100648 *ipp = ip;
Christoph Hellwig288699f2010-06-23 18:11:15 +1000649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100651 out_bmap_cancel:
652 xfs_bmap_cancel(&free_list);
653 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100655 out_trans_cancel:
656 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000657 out_release_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /*
659 * Wait until after the current transaction is aborted to
660 * release the inode. This prevents recursive transactions
661 * and deadlocks from xfs_inactive.
662 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000663 if (ip)
664 IRELE(ip);
665
666 xfs_qm_dqrele(udqp);
667 xfs_qm_dqrele(gdqp);
668
669 if (unlock_dp_on_error)
670 xfs_iunlock(dp, XFS_ILOCK_EXCL);
671 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675int xfs_locked_n;
676int xfs_small_retries;
677int xfs_middle_retries;
678int xfs_lots_retries;
679int xfs_lock_delays;
680#endif
681
682/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000683 * Bump the subclass so xfs_lock_inodes() acquires each lock with
684 * a different value
685 */
686static inline int
687xfs_lock_inumorder(int lock_mode, int subclass)
688{
689 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +1000690 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000691 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +1000692 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000693
694 return lock_mode;
695}
696
697/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * The following routine will lock n inodes in exclusive mode.
699 * We assume the caller calls us with the inodes in i_ino order.
700 *
701 * We need to detect deadlock where an inode that we lock
702 * is in the AIL and we start waiting for another inode that is locked
703 * by a thread in a long running transaction (such as truncate). This can
704 * result in deadlock since the long running trans might need to wait
705 * for the inode we just locked in order to push the tail and free space
706 * in the log.
707 */
708void
709xfs_lock_inodes(
710 xfs_inode_t **ips,
711 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 uint lock_mode)
713{
714 int attempts = 0, i, j, try_lock;
715 xfs_log_item_t *lp;
716
717 ASSERT(ips && (inodes >= 2)); /* we need at least two */
718
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000719 try_lock = 0;
720 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722again:
723 for (; i < inodes; i++) {
724 ASSERT(ips[i]);
725
726 if (i && (ips[i] == ips[i-1])) /* Already locked */
727 continue;
728
729 /*
730 * If try_lock is not set yet, make sure all locked inodes
731 * are not in the AIL.
732 * If any are, set try_lock to be used later.
733 */
734
735 if (!try_lock) {
736 for (j = (i - 1); j >= 0 && !try_lock; j--) {
737 lp = (xfs_log_item_t *)ips[j]->i_itemp;
738 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
739 try_lock++;
740 }
741 }
742 }
743
744 /*
745 * If any of the previous locks we have locked is in the AIL,
746 * we must TRY to get the second and subsequent locks. If
747 * we can't get any, we must release all we have
748 * and try again.
749 */
750
751 if (try_lock) {
752 /* try_lock must be 0 if i is 0. */
753 /*
754 * try_lock means we have an inode locked
755 * that is in the AIL.
756 */
757 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000758 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 attempts++;
760
761 /*
762 * Unlock all previous guys and try again.
763 * xfs_iunlock will try to push the tail
764 * if the inode is in the AIL.
765 */
766
767 for(j = i - 1; j >= 0; j--) {
768
769 /*
770 * Check to see if we've already
771 * unlocked this one.
772 * Not the first one going back,
773 * and the inode ptr is the same.
774 */
775 if ((j != (i - 1)) && ips[j] ==
776 ips[j+1])
777 continue;
778
779 xfs_iunlock(ips[j], lock_mode);
780 }
781
782 if ((attempts % 5) == 0) {
783 delay(1); /* Don't just spin the CPU */
784#ifdef DEBUG
785 xfs_lock_delays++;
786#endif
787 }
788 i = 0;
789 try_lock = 0;
790 goto again;
791 }
792 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000793 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795 }
796
797#ifdef DEBUG
798 if (attempts) {
799 if (attempts < 5) xfs_small_retries++;
800 else if (attempts < 100) xfs_middle_retries++;
801 else xfs_lots_retries++;
802 } else {
803 xfs_locked_n++;
804 }
805#endif
806}
807
David Chinnerf9114eb2008-09-17 16:51:21 +1000808/*
809 * xfs_lock_two_inodes() can only be used to lock one type of lock
810 * at a time - the iolock or the ilock, but not both at once. If
811 * we lock both at once, lockdep will report false positives saying
812 * we have violated locking orders.
813 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +1000814void
815xfs_lock_two_inodes(
816 xfs_inode_t *ip0,
817 xfs_inode_t *ip1,
818 uint lock_mode)
819{
820 xfs_inode_t *temp;
821 int attempts = 0;
822 xfs_log_item_t *lp;
823
David Chinnerf9114eb2008-09-17 16:51:21 +1000824 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
825 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +1000826 ASSERT(ip0->i_ino != ip1->i_ino);
827
828 if (ip0->i_ino > ip1->i_ino) {
829 temp = ip0;
830 ip0 = ip1;
831 ip1 = temp;
832 }
833
834 again:
835 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
836
837 /*
838 * If the first lock we have locked is in the AIL, we must TRY to get
839 * the second lock. If we can't get it, we must release the first one
840 * and try again.
841 */
842 lp = (xfs_log_item_t *)ip0->i_itemp;
843 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
844 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
845 xfs_iunlock(ip0, lock_mode);
846 if ((++attempts % 5) == 0)
847 delay(1); /* Don't just spin the CPU */
848 goto again;
849 }
850 } else {
851 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
852 }
853}
854
Christoph Hellwig993386c12007-08-28 16:12:30 +1000855int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856xfs_remove(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000857 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000858 struct xfs_name *name,
859 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Christoph Hellwig993386c12007-08-28 16:12:30 +1000861 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000863 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int error = 0;
865 xfs_bmap_free_t free_list;
866 xfs_fsblock_t first_block;
867 int cancel_flags;
868 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int link_zero;
870 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000871 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000873 trace_xfs_remove(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (XFS_FORCED_SHUTDOWN(mp))
876 return XFS_ERROR(EIO);
877
Christoph Hellwig7d095252009-06-08 15:33:32 +0200878 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000879 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Christoph Hellwig7d095252009-06-08 15:33:32 +0200882 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000883 if (error)
884 goto std_return;
885
886 if (is_dir) {
887 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
888 log_count = XFS_DEFAULT_LOG_COUNT;
889 } else {
890 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
891 log_count = XFS_REMOVE_LOG_COUNT;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /*
896 * We try to get the real space reservation first,
897 * allowing for directory btree deletion(s) implying
898 * possible bmap insert(s). If we can't get the space
899 * reservation then we use 0 instead, and avoid the bmap
900 * btree insert(s) in the directory code by, if the bmap
901 * insert tries to happen, instead trimming the LAST
902 * block from the directory.
903 */
904 resblks = XFS_REMOVE_SPACE_RES(mp);
905 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000906 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (error == ENOSPC) {
908 resblks = 0;
909 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000910 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 if (error) {
913 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000914 cancel_flags = 0;
915 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
Christoph Hellwige1cccd92008-08-13 16:18:07 +1000918 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Christoph Hellwigddc34152011-09-19 15:00:54 +0000920 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
921 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000924 * If we're removing a directory perform some additional validation.
925 */
926 if (is_dir) {
927 ASSERT(ip->i_d.di_nlink >= 2);
928 if (ip->i_d.di_nlink != 2) {
929 error = XFS_ERROR(ENOTEMPTY);
930 goto out_trans_cancel;
931 }
932 if (!xfs_dir_isempty(ip)) {
933 error = XFS_ERROR(ENOTEMPTY);
934 goto out_trans_cancel;
935 }
936 }
937
Eric Sandeen9d87c312009-01-14 23:22:07 -0600938 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +1000939 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +1000940 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (error) {
942 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000943 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000945 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000947 if (is_dir) {
948 /*
949 * Drop the link from ip's "..".
950 */
951 error = xfs_droplink(tp, dp);
952 if (error)
953 goto out_bmap_cancel;
954
955 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +1100956 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000957 */
958 error = xfs_droplink(tp, ip);
959 if (error)
960 goto out_bmap_cancel;
961 } else {
962 /*
963 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +1100964 * inode here. For a directory this is done implicitly
965 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000966 */
967 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000970 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +1100971 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000972 */
973 error = xfs_droplink(tp, ip);
974 if (error)
975 goto out_bmap_cancel;
976
977 /*
978 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * we are in the transaction.
980 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000981 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * If this is a synchronous mount, make sure that the
985 * remove transaction goes to disk before returning to
986 * the user.
987 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000988 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100991 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000992 if (error)
993 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000995 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +1000996 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001000 * If we are using filestreams, kill the stream association.
1001 * If the file is still open it may get a new one but that
1002 * will get killed on last close in xfs_close() so we don't
1003 * have to worry about that.
1004 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001005 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001006 xfs_filestream_deassociate(ip);
1007
Christoph Hellwig288699f2010-06-23 18:11:15 +10001008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001010 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 xfs_bmap_cancel(&free_list);
1012 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001013 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001015 std_return:
1016 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Christoph Hellwig993386c12007-08-28 16:12:30 +10001019int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020xfs_link(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001021 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001022 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10001023 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001025 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int error;
1028 xfs_bmap_free_t free_list;
1029 xfs_fsblock_t first_block;
1030 int cancel_flags;
1031 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001034 trace_xfs_link(tdp, target_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001036 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (XFS_FORCED_SHUTDOWN(mp))
1039 return XFS_ERROR(EIO);
1040
Christoph Hellwig7d095252009-06-08 15:33:32 +02001041 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01001042 if (error)
1043 goto std_return;
1044
Christoph Hellwig7d095252009-06-08 15:33:32 +02001045 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (error)
1047 goto std_return;
1048
1049 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1050 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001051 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1053 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1054 if (error == ENOSPC) {
1055 resblks = 0;
1056 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1057 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1058 }
1059 if (error) {
1060 cancel_flags = 0;
1061 goto error_return;
1062 }
1063
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001064 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Christoph Hellwigddc34152011-09-19 15:00:54 +00001066 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1067 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /*
Nathan Scott365ca832005-06-21 15:39:12 +10001070 * If we are using project inheritance, we only allow hard link
1071 * creation in our tree when the project IDs are the same; else
1072 * the tree quota mechanism could be circumvented.
1073 */
1074 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001075 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10001076 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10001077 goto error_return;
1078 }
1079
Barry Naujok556b8b12008-04-10 12:22:07 +10001080 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1081 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto error_return;
1083
Eric Sandeen9d87c312009-01-14 23:22:07 -06001084 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Barry Naujok556b8b12008-04-10 12:22:07 +10001086 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1087 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (error)
1089 goto abort_return;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001090 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1092
1093 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001094 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /*
1098 * If this is a synchronous mount, make sure that the
1099 * link transaction goes to disk before returning to
1100 * the user.
1101 */
1102 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1103 xfs_trans_set_sync(tp);
1104 }
1105
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001106 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (error) {
1108 xfs_bmap_cancel(&free_list);
1109 goto abort_return;
1110 }
1111
Christoph Hellwig288699f2010-06-23 18:11:15 +10001112 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 abort_return:
1115 cancel_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 error_return:
1117 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001118 std_return:
1119 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001121
Christoph Hellwig993386c12007-08-28 16:12:30 +10001122int
Christoph Hellwig993386c12007-08-28 16:12:30 +10001123xfs_set_dmattrs(
1124 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 u_int evmask,
Christoph Hellwig993386c12007-08-28 16:12:30 +10001126 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001128 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int error;
1131
1132 if (!capable(CAP_SYS_ADMIN))
1133 return XFS_ERROR(EPERM);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (XFS_FORCED_SHUTDOWN(mp))
1136 return XFS_ERROR(EIO);
1137
1138 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1139 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1140 if (error) {
1141 xfs_trans_cancel(tp, 0);
1142 return error;
1143 }
1144 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001145 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Christoph Hellwig613d7042007-10-11 17:44:08 +10001147 ip->i_d.di_dmevmask = evmask;
1148 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001151 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 return error;
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/*
1157 * xfs_alloc_file_space()
1158 * This routine allocates disk space for the given file.
1159 *
1160 * If alloc_type == 0, this request is for an ALLOCSP type
1161 * request which will change the file size. In this case, no
1162 * DMAPI event will be generated by the call. A TRUNCATE event
1163 * will be generated later by xfs_setattr.
1164 *
1165 * If alloc_type != 0, this request is for a RESVSP type
1166 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1167 * lower block boundary byte address is less than the file's
1168 * length.
1169 *
1170 * RETURNS:
1171 * 0 on success
1172 * errno on error
1173 *
1174 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001175STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176xfs_alloc_file_space(
1177 xfs_inode_t *ip,
1178 xfs_off_t offset,
1179 xfs_off_t len,
1180 int alloc_type,
1181 int attr_flags)
1182{
Nathan Scottdd9f4382006-01-11 15:28:28 +11001183 xfs_mount_t *mp = ip->i_mount;
1184 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 xfs_filblks_t allocated_fsb;
1186 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001187 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001189 xfs_fsblock_t firstfsb;
1190 int nimaps;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001191 int quota_flag;
1192 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001194 xfs_bmbt_irec_t imaps[1], *imapp;
1195 xfs_bmap_free_t free_list;
1196 uint qblocks, resblks, resrtextents;
1197 int committed;
1198 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001200 trace_xfs_alloc_file_space(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 if (XFS_FORCED_SHUTDOWN(mp))
1203 return XFS_ERROR(EIO);
1204
Christoph Hellwig7d095252009-06-08 15:33:32 +02001205 error = xfs_qm_dqattach(ip, 0);
1206 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return error;
1208
1209 if (len <= 0)
1210 return XFS_ERROR(EINVAL);
1211
David Chinner957d0eb2007-06-18 16:50:37 +10001212 rt = XFS_IS_REALTIME_INODE(ip);
1213 extsz = xfs_get_extsz_hint(ip);
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11001217 nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1219 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001222 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001225 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Nathan Scottdd9f4382006-01-11 15:28:28 +11001227 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11001228 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11001229 */
1230 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001232 do_div(s, extsz);
1233 s *= extsz;
1234 e = startoffset_fsb + allocatesize_fsb;
1235 if ((temp = do_mod(startoffset_fsb, extsz)))
1236 e += temp;
1237 if ((temp = do_mod(e, extsz)))
1238 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001240 s = 0;
1241 e = allocatesize_fsb;
1242 }
1243
Dave Chinner72656c42010-09-03 12:19:33 +10001244 /*
1245 * The transaction reservation is limited to a 32-bit block
1246 * count, hence we need to limit the number of blocks we are
1247 * trying to reserve to avoid an overflow. We can't allocate
1248 * more than @nimaps extents, and an extent is limited on disk
1249 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1250 */
1251 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
Nathan Scottdd9f4382006-01-11 15:28:28 +11001252 if (unlikely(rt)) {
Dave Chinner72656c42010-09-03 12:19:33 +10001253 resrtextents = qblocks = resblks;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001254 resrtextents /= mp->m_sb.sb_rextsize;
1255 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1256 quota_flag = XFS_QMOPT_RES_RTBLKS;
1257 } else {
1258 resrtextents = 0;
Dave Chinner72656c42010-09-03 12:19:33 +10001259 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001260 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
1263 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001264 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
1266 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001267 error = xfs_trans_reserve(tp, resblks,
1268 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 XFS_TRANS_PERM_LOG_RES,
1270 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001272 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
1274 if (error) {
1275 /*
1276 * Free the transaction structure.
1277 */
1278 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1279 xfs_trans_cancel(tp, 0);
1280 break;
1281 }
1282 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001283 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1284 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (error)
1286 goto error1;
1287
Christoph Hellwigddc34152011-09-19 15:00:54 +00001288 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Eric Sandeen9d87c312009-01-14 23:22:07 -06001290 xfs_bmap_init(&free_list, &firstfsb);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001291 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1292 allocatesize_fsb, alloc_type, &firstfsb,
1293 0, imapp, &nimaps, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (error) {
1295 goto error0;
1296 }
1297
1298 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001299 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001301 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (error) {
1303 goto error0;
1304 }
1305
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001306 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1308 if (error) {
1309 break;
1310 }
1311
1312 allocated_fsb = imapp->br_blockcount;
1313
Nathan Scottdd9f4382006-01-11 15:28:28 +11001314 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 error = XFS_ERROR(ENOSPC);
1316 break;
1317 }
1318
1319 startoffset_fsb += allocated_fsb;
1320 allocatesize_fsb -= allocated_fsb;
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 return error;
1324
Nathan Scottdd9f4382006-01-11 15:28:28 +11001325error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 xfs_bmap_cancel(&free_list);
Dave Chinnerea562ed2012-05-08 20:48:53 +10001327 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001328
1329error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1331 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001332 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
1335/*
1336 * Zero file bytes between startoff and endoff inclusive.
1337 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001338 *
1339 * This function is used by xfs_free_file_space() to zero
1340 * partial blocks when the range to free is not block aligned.
1341 * When unreserving space with boundaries that are not block
1342 * aligned we round up the start and round down the end
1343 * boundaries and then use this function to zero the parts of
1344 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
1346STATIC int
1347xfs_zero_remaining_bytes(
1348 xfs_inode_t *ip,
1349 xfs_off_t startoff,
1350 xfs_off_t endoff)
1351{
1352 xfs_bmbt_irec_t imap;
1353 xfs_fileoff_t offset_fsb;
1354 xfs_off_t lastoffset;
1355 xfs_off_t offset;
1356 xfs_buf_t *bp;
1357 xfs_mount_t *mp = ip->i_mount;
1358 int nimap;
1359 int error = 0;
1360
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001361 /*
1362 * Avoid doing I/O beyond eof - it's not necessary
1363 * since nothing can read beyond eof. The space will
1364 * be zeroed when the file is extended anyway.
1365 */
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001366 if (startoff >= XFS_ISIZE(ip))
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001367 return 0;
1368
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001369 if (endoff > XFS_ISIZE(ip))
1370 endoff = XFS_ISIZE(ip);
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001371
Dave Chinner686865f2010-09-24 20:07:47 +10001372 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1373 mp->m_rtdev_targp : mp->m_ddev_targp,
Dave Chinneraa5c1582012-04-23 15:58:56 +10001374 BTOBB(mp->m_sb.sb_blocksize), 0);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11001375 if (!bp)
1376 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001378 xfs_buf_unlock(bp);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1381 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1382 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00001383 error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (error || nimap < 1)
1385 break;
1386 ASSERT(imap.br_blockcount >= 1);
1387 ASSERT(imap.br_startoff == offset_fsb);
1388 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1389 if (lastoffset > endoff)
1390 lastoffset = endoff;
1391 if (imap.br_startblock == HOLESTARTBLOCK)
1392 continue;
1393 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1394 if (imap.br_state == XFS_EXT_UNWRITTEN)
1395 continue;
1396 XFS_BUF_UNDONE(bp);
1397 XFS_BUF_UNWRITE(bp);
1398 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001399 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001401 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10001402 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00001403 xfs_buf_ioerror_alert(bp,
1404 "xfs_zero_remaining_bytes(read)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 break;
1406 }
Chandra Seetharaman62926042011-07-22 23:40:15 +00001407 memset(bp->b_addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
1409 0, lastoffset - offset + 1);
1410 XFS_BUF_UNDONE(bp);
1411 XFS_BUF_UNREAD(bp);
1412 XFS_BUF_WRITE(bp);
1413 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001414 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10001415 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00001416 xfs_buf_ioerror_alert(bp,
1417 "xfs_zero_remaining_bytes(write)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 break;
1419 }
1420 }
1421 xfs_buf_free(bp);
1422 return error;
1423}
1424
1425/*
1426 * xfs_free_file_space()
1427 * This routine frees disk space for the given file.
1428 *
1429 * This routine is only called by xfs_change_file_space
1430 * for an UNRESVSP type call.
1431 *
1432 * RETURNS:
1433 * 0 on success
1434 * errno on error
1435 *
1436 */
1437STATIC int
1438xfs_free_file_space(
1439 xfs_inode_t *ip,
1440 xfs_off_t offset,
1441 xfs_off_t len,
1442 int attr_flags)
1443{
1444 int committed;
1445 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 xfs_fileoff_t endoffset_fsb;
1447 int error;
1448 xfs_fsblock_t firstfsb;
1449 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 xfs_bmbt_irec_t imap;
1451 xfs_off_t ioffset;
1452 xfs_extlen_t mod=0;
1453 xfs_mount_t *mp;
1454 int nimap;
1455 uint resblks;
Dave Chinner7031d0e2013-05-20 09:51:09 +10001456 xfs_off_t rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int rt;
1458 xfs_fileoff_t startoffset_fsb;
1459 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07001460 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 mp = ip->i_mount;
1463
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001464 trace_xfs_free_file_space(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10001465
Christoph Hellwig7d095252009-06-08 15:33:32 +02001466 error = xfs_qm_dqattach(ip, 0);
1467 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return error;
1469
1470 error = 0;
1471 if (len <= 0) /* if nothing being freed */
1472 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11001473 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001475 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Christoph Hellwig0f285c82008-07-18 17:13:28 +10001477 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07001478 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11001479 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01001481 /* wait for the completion of any pending DIOs */
Christoph Hellwig4a06fd22011-08-23 08:28:13 +00001482 inode_dio_wait(VFS_I(ip));
Yingping Lu9fa80462006-03-22 12:44:35 +11001483 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07001484
Dave Chinner7031d0e2013-05-20 09:51:09 +10001485 rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 ioffset = offset & ~(rounding - 1);
Dave Chinnerfb595812012-11-12 22:53:57 +11001487 error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
1488 ioffset, -1);
1489 if (error)
1490 goto out_unlock_iolock;
1491 truncate_pagecache_range(VFS_I(ip), ioffset, -1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /*
1494 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001495 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * actually need to zero the extent edges. Otherwise xfs_bunmapi
1497 * will take care of it for us.
1498 */
Eric Sandeen62118702008-03-06 13:44:28 +11001499 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00001501 error = xfs_bmapi_read(ip, startoffset_fsb, 1,
1502 &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (error)
1504 goto out_unlock_iolock;
1505 ASSERT(nimap == 0 || nimap == 1);
1506 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1507 xfs_daddr_t block;
1508
1509 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1510 block = imap.br_startblock;
1511 mod = do_div(block, mp->m_sb.sb_rextsize);
1512 if (mod)
1513 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1514 }
1515 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00001516 error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
1517 &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (error)
1519 goto out_unlock_iolock;
1520 ASSERT(nimap == 0 || nimap == 1);
1521 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1522 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1523 mod++;
1524 if (mod && (mod != mp->m_sb.sb_rextsize))
1525 endoffset_fsb -= mod;
1526 }
1527 }
1528 if ((done = (endoffset_fsb <= startoffset_fsb)))
1529 /*
1530 * One contiguous piece to clear
1531 */
1532 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
1533 else {
1534 /*
1535 * Some full blocks, possibly two pieces to clear
1536 */
1537 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
1538 error = xfs_zero_remaining_bytes(ip, offset,
1539 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
1540 if (!error &&
1541 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
1542 error = xfs_zero_remaining_bytes(ip,
1543 XFS_FSB_TO_B(mp, endoffset_fsb),
1544 offset + len - 1);
1545 }
1546
1547 /*
1548 * free file space until done or until there is an error
1549 */
1550 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1551 while (!error && !done) {
1552
1553 /*
David Chinner91ebecc2007-07-19 16:28:30 +10001554 * allocate and setup the transaction. Allow this
1555 * transaction to dip into the reserve blocks to ensure
1556 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
1558 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10001559 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 error = xfs_trans_reserve(tp,
1561 resblks,
1562 XFS_WRITE_LOG_RES(mp),
1563 0,
1564 XFS_TRANS_PERM_LOG_RES,
1565 XFS_WRITE_LOG_COUNT);
1566
1567 /*
1568 * check for running out of space
1569 */
1570 if (error) {
1571 /*
1572 * Free the transaction structure.
1573 */
1574 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1575 xfs_trans_cancel(tp, 0);
1576 break;
1577 }
1578 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001579 error = xfs_trans_reserve_quota(tp, mp,
1580 ip->i_udquot, ip->i_gdquot,
1581 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (error)
1583 goto error1;
1584
Christoph Hellwigddc34152011-09-19 15:00:54 +00001585 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /*
1588 * issue the bunmapi() call to free the blocks
1589 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001590 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001591 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 endoffset_fsb - startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001593 0, 2, &firstfsb, &free_list, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (error) {
1595 goto error0;
1596 }
1597
1598 /*
1599 * complete the transaction
1600 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001601 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (error) {
1603 goto error0;
1604 }
1605
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001606 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1608 }
1609
1610 out_unlock_iolock:
1611 if (need_iolock)
1612 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1613 return error;
1614
1615 error0:
1616 xfs_bmap_cancel(&free_list);
1617 error1:
1618 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1619 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
1620 XFS_ILOCK_EXCL);
1621 return error;
1622}
1623
Dave Chinneref9d8732012-11-29 15:26:33 +11001624
1625STATIC int
1626xfs_zero_file_space(
1627 struct xfs_inode *ip,
1628 xfs_off_t offset,
1629 xfs_off_t len,
1630 int attr_flags)
1631{
1632 struct xfs_mount *mp = ip->i_mount;
1633 uint granularity;
1634 xfs_off_t start_boundary;
1635 xfs_off_t end_boundary;
1636 int error;
1637
1638 granularity = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
1639
1640 /*
1641 * Round the range of extents we are going to convert inwards. If the
1642 * offset is aligned, then it doesn't get changed so we zero from the
1643 * start of the block offset points to.
1644 */
1645 start_boundary = round_up(offset, granularity);
1646 end_boundary = round_down(offset + len, granularity);
1647
1648 ASSERT(start_boundary >= offset);
1649 ASSERT(end_boundary <= offset + len);
1650
1651 if (!(attr_flags & XFS_ATTR_NOLOCK))
1652 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1653
1654 if (start_boundary < end_boundary - 1) {
1655 /* punch out the page cache over the conversion range */
1656 truncate_pagecache_range(VFS_I(ip), start_boundary,
1657 end_boundary - 1);
1658 /* convert the blocks */
1659 error = xfs_alloc_file_space(ip, start_boundary,
1660 end_boundary - start_boundary - 1,
1661 XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT,
1662 attr_flags);
1663 if (error)
1664 goto out_unlock;
1665
1666 /* We've handled the interior of the range, now for the edges */
1667 if (start_boundary != offset)
1668 error = xfs_iozero(ip, offset, start_boundary - offset);
1669 if (error)
1670 goto out_unlock;
1671
1672 if (end_boundary != offset + len)
1673 error = xfs_iozero(ip, end_boundary,
1674 offset + len - end_boundary);
1675
1676 } else {
1677 /*
1678 * It's either a sub-granularity range or the range spanned lies
1679 * partially across two adjacent blocks.
1680 */
1681 error = xfs_iozero(ip, offset, len);
1682 }
1683
1684out_unlock:
1685 if (!(attr_flags & XFS_ATTR_NOLOCK))
1686 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1687 return error;
1688
1689}
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691/*
1692 * xfs_change_file_space()
1693 * This routine allocates or frees disk space for the given file.
1694 * The user specified parameters are checked for alignment and size
1695 * limitations.
1696 *
1697 * RETURNS:
1698 * 0 on success
1699 * errno on error
1700 *
1701 */
1702int
1703xfs_change_file_space(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001704 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int cmd,
1706 xfs_flock64_t *bf,
1707 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int attr_flags)
1709{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001710 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 int clrprealloc;
1712 int error;
1713 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 int setprealloc;
1715 xfs_off_t startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10001717 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Christoph Hellwig993386c12007-08-28 16:12:30 +10001719 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return XFS_ERROR(EINVAL);
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 switch (bf->l_whence) {
1723 case 0: /*SEEK_SET*/
1724 break;
1725 case 1: /*SEEK_CUR*/
1726 bf->l_start += offset;
1727 break;
1728 case 2: /*SEEK_END*/
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001729 bf->l_start += XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 break;
1731 default:
1732 return XFS_ERROR(EINVAL);
1733 }
1734
Dave Chinnerf5b89112012-11-14 17:42:47 +11001735 /*
1736 * length of <= 0 for resv/unresv/zero is invalid. length for
1737 * alloc/free is ignored completely and we have no idea what userspace
1738 * might have set it to, so set it to zero to allow range
1739 * checks to pass.
1740 */
1741 switch (cmd) {
1742 case XFS_IOC_ZERO_RANGE:
1743 case XFS_IOC_RESVSP:
1744 case XFS_IOC_RESVSP64:
1745 case XFS_IOC_UNRESVSP:
1746 case XFS_IOC_UNRESVSP64:
1747 if (bf->l_len <= 0)
1748 return XFS_ERROR(EINVAL);
1749 break;
1750 default:
1751 bf->l_len = 0;
1752 break;
1753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Dave Chinner32972382012-06-08 15:44:54 +10001755 if (bf->l_start < 0 ||
1756 bf->l_start > mp->m_super->s_maxbytes ||
Dave Chinnerf5b89112012-11-14 17:42:47 +11001757 bf->l_start + bf->l_len < 0 ||
1758 bf->l_start + bf->l_len >= mp->m_super->s_maxbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return XFS_ERROR(EINVAL);
1760
1761 bf->l_whence = 0;
1762
1763 startoffset = bf->l_start;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001764 fsize = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 setprealloc = clrprealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 switch (cmd) {
Dave Chinner44722352010-08-24 12:02:11 +10001768 case XFS_IOC_ZERO_RANGE:
Dave Chinneref9d8732012-11-29 15:26:33 +11001769 error = xfs_zero_file_space(ip, startoffset, bf->l_len,
1770 attr_flags);
1771 if (error)
1772 return error;
1773 setprealloc = 1;
1774 break;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 case XFS_IOC_RESVSP:
1777 case XFS_IOC_RESVSP64:
1778 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
Dave Chinneref9d8732012-11-29 15:26:33 +11001779 XFS_BMAPI_PREALLOC, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (error)
1781 return error;
1782 setprealloc = 1;
1783 break;
1784
1785 case XFS_IOC_UNRESVSP:
1786 case XFS_IOC_UNRESVSP64:
1787 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
1788 attr_flags)))
1789 return error;
1790 break;
1791
1792 case XFS_IOC_ALLOCSP:
1793 case XFS_IOC_ALLOCSP64:
1794 case XFS_IOC_FREESP:
1795 case XFS_IOC_FREESP64:
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10001796 /*
1797 * These operations actually do IO when extending the file, but
1798 * the allocation is done seperately to the zeroing that is
1799 * done. This set of operations need to be serialised against
1800 * other IO operations, such as truncate and buffered IO. We
1801 * need to take the IOLOCK here to serialise the allocation and
1802 * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
1803 * truncate, direct IO) from racing against the transient
1804 * allocated but not written state we can have here.
1805 */
1806 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (startoffset > fsize) {
1808 error = xfs_alloc_file_space(ip, fsize,
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10001809 startoffset - fsize, 0,
1810 attr_flags | XFS_ATTR_NOLOCK);
1811 if (error) {
1812 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 break;
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10001814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816
Christoph Hellwig0f285c82008-07-18 17:13:28 +10001817 iattr.ia_valid = ATTR_SIZE;
1818 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10001820 error = xfs_setattr_size(ip, &iattr,
1821 attr_flags | XFS_ATTR_NOLOCK);
1822 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (error)
1825 return error;
1826
1827 clrprealloc = 1;
1828 break;
1829
1830 default:
1831 ASSERT(0);
1832 return XFS_ERROR(EINVAL);
1833 }
1834
1835 /*
1836 * update the inode timestamp, mode, and prealloc flag bits
1837 */
1838 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
1839
1840 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
1841 0, 0, 0))) {
1842 /* ASSERT(0); */
1843 xfs_trans_cancel(tp, 0);
1844 return error;
1845 }
1846
1847 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001848 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Christoph Hellwig0f285c82008-07-18 17:13:28 +10001850 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 ip->i_d.di_mode &= ~S_ISUID;
1852
1853 /*
1854 * Note that we don't have to worry about mandatory
1855 * file locking being disabled here because we only
1856 * clear the S_ISGID bit if the Group execute bit is
1857 * on, but if it was on then mandatory locking wouldn't
1858 * have been enabled.
1859 */
1860 if (ip->i_d.di_mode & S_IXGRP)
1861 ip->i_d.di_mode &= ~S_ISGID;
1862
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001863 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 if (setprealloc)
1866 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
1867 else if (clrprealloc)
1868 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
1869
1870 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinner82878892011-03-26 09:13:08 +11001871 if (attr_flags & XFS_ATTR_SYNC)
1872 xfs_trans_set_sync(tp);
Christoph Hellwig23bb0be2011-09-18 20:47:51 +00001873 return xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}