blob: 42c0ef288aeb219290ad344bc426430b17a76f52 [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)) {
Mark Tinguely725eb1e2013-06-17 15:35:57 -0500325 error = xfs_inactive_symlink(ip, &tp);
326 if (error)
327 goto out_cancel;
Christoph Hellwigb373e982012-07-04 11:13:29 -0400328 } else if (truncate) {
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000329 ip->i_d.di_size = 0;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000330 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
331
332 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
Christoph Hellwigb373e982012-07-04 11:13:29 -0400333 if (error)
334 goto out_cancel;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000335
336 ASSERT(ip->i_d.di_nextents == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 /*
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400340 * If there are attributes associated with the file then blow them away
341 * now. The code calls a routine that recursively deconstructs the
342 * attribute fork. We need to just commit the current transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * because we can't use it for xfs_attr_inactive().
344 */
345 if (ip->i_d.di_anextents > 0) {
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400346 ASSERT(ip->i_d.di_forkoff != 0);
347
348 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400349 if (error)
Christoph Hellwig0b561852012-07-04 11:13:31 -0400350 goto out_unlock;
351
352 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400353
354 error = xfs_attr_inactive(ip);
355 if (error)
Christoph Hellwig0b561852012-07-04 11:13:31 -0400356 goto out;
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400357
358 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
359 error = xfs_trans_reserve(tp, 0,
360 XFS_IFREE_LOG_RES(mp),
361 0, XFS_TRANS_PERM_LOG_RES,
362 XFS_INACTIVE_LOG_COUNT);
Christoph Hellwig0b561852012-07-04 11:13:31 -0400363 if (error) {
364 xfs_trans_cancel(tp, 0);
365 goto out;
366 }
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400367
368 xfs_ilock(ip, XFS_ILOCK_EXCL);
369 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
Christoph Hellwigfe67be02012-07-04 11:13:30 -0400372 if (ip->i_afp)
373 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
374
375 ASSERT(ip->i_d.di_anextents == 0);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Free the inode.
379 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600380 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 error = xfs_ifree(tp, ip, &free_list);
382 if (error) {
383 /*
384 * If we fail to free the inode, shut down. The cancel
385 * might do that, we need to make sure. Otherwise the
386 * inode might be lost for a long time or forever.
387 */
388 if (!XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100389 xfs_notice(mp, "%s: xfs_ifree returned error %d",
390 __func__, error);
Nathan Scott7d04a332006-06-09 14:58:38 +1000391 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
394 } else {
395 /*
396 * Credit the quota account(s). The inode is gone.
397 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200398 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
David Chinner78e9da72008-04-10 12:24:17 +1000401 * Just ignore errors at this point. There is nothing we can
402 * do except to try to keep going. Make sure it's not a silent
403 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
David Chinner78e9da72008-04-10 12:24:17 +1000405 error = xfs_bmap_finish(&tp, &free_list, &committed);
406 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100407 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
408 __func__, error);
David Chinner78e9da72008-04-10 12:24:17 +1000409 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
410 if (error)
Dave Chinner53487782011-03-07 10:05:35 +1100411 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
412 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Christoph Hellwig7d095252009-06-08 15:33:32 +0200414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Release the dquots held by inode, if any.
417 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200418 xfs_qm_dqdetach(ip);
Christoph Hellwig0b561852012-07-04 11:13:31 -0400419out_unlock:
420 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigb373e982012-07-04 11:13:29 -0400421out:
422 return VN_INACTIVE_CACHE;
423out_cancel:
424 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
Christoph Hellwig0b561852012-07-04 11:13:31 -0400425 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Barry Naujok384f3ce2008-05-21 16:58:22 +1000428/*
429 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
430 * is allowed, otherwise it has to be an exact match. If a CI match is found,
431 * ci_name->name will point to a the actual name (caller must free) or
432 * will be set to NULL if an exact match is found.
433 */
Christoph Hellwig993386c12007-08-28 16:12:30 +1000434int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435xfs_lookup(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000436 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000437 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +1000438 xfs_inode_t **ipp,
439 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000441 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int error;
443 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000445 trace_xfs_lookup(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
448 return XFS_ERROR(EIO);
449
450 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000451 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000453
454 if (error)
455 goto out;
456
Dave Chinner7b6259e2010-06-24 11:35:17 +1000457 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000458 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000459 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000460
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000461 return 0;
462
Barry Naujok384f3ce2008-05-21 16:58:22 +1000463out_free_name:
464 if (ci_name)
465 kmem_free(ci_name->name);
466out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +1000467 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return error;
469}
470
Christoph Hellwig993386c12007-08-28 16:12:30 +1000471int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472xfs_create(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000473 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000474 struct xfs_name *name,
Al Viro576b1d62011-07-26 02:50:15 -0400475 umode_t mode,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000476 xfs_dev_t rdev,
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000477 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100479 int is_dir = S_ISDIR(mode);
480 struct xfs_mount *mp = dp->i_mount;
481 struct xfs_inode *ip = NULL;
482 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000483 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 xfs_bmap_free_t free_list;
485 xfs_fsblock_t first_block;
Thiago Farina667a9292012-11-12 21:32:59 -0200486 bool unlock_dp_on_error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 uint cancel_flags;
488 int committed;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000489 prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100490 struct xfs_dquot *udqp = NULL;
491 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100493 uint log_res;
494 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000496 trace_xfs_create(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100498 if (XFS_FORCED_SHUTDOWN(mp))
499 return XFS_ERROR(EIO);
500
Nathan Scott365ca832005-06-21 15:39:12 +1000501 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000502 prid = xfs_get_projid(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 else
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000504 prid = XFS_PROJID_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
507 * Make sure that we have allocated dquot(s) on disk.
508 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200509 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100510 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000512 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100514 if (is_dir) {
515 rdev = 0;
516 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
517 log_res = XFS_MKDIR_LOG_RES(mp);
518 log_count = XFS_MKDIR_LOG_COUNT;
519 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
520 } else {
521 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
522 log_res = XFS_CREATE_LOG_RES(mp);
523 log_count = XFS_CREATE_LOG_COUNT;
524 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 * Initially assume that the file does not exist and
531 * reserve the resources for that case. If that is not
532 * the case we'll drop the one we have and get a more
533 * appropriate transaction later.
534 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100535 error = xfs_trans_reserve(tp, resblks, log_res, 0,
536 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +0200538 /* flush outstanding delalloc blocks and retry */
Dave Chinner9aa05002012-10-08 21:56:04 +1100539 xfs_flush_inodes(mp);
Christoph Hellwig4734d402009-09-09 18:19:02 -0500540 error = xfs_trans_reserve(tp, resblks, log_res, 0,
541 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +0200542 }
543 if (error == ENOSPC) {
544 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100546 error = xfs_trans_reserve(tp, 0, log_res, 0,
547 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 if (error) {
550 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100551 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000554 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Thiago Farina667a9292012-11-12 21:32:59 -0200555 unlock_dp_on_error = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100557 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /*
560 * Reserve disk quota and the inode.
561 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200562 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100564 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Barry Naujok556b8b12008-04-10 12:22:07 +1000566 error = xfs_dir_canenter(tp, dp, name, resblks);
567 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100568 goto out_trans_cancel;
569
570 /*
571 * A newly created regular or special file just has one directory
572 * entry pointing to them, but a directory also the "." entry
573 * pointing to itself.
574 */
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000575 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100576 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (error) {
578 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100579 goto out_trans_cancel;
580 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /*
Christoph Hellwig993386c12007-08-28 16:12:30 +1000584 * Now we join the directory inode to the transaction. We do not do it
585 * earlier because xfs_dir_ialloc might commit the previous transaction
586 * (and release all the locks). An error from here on will result in
587 * the transaction cancel unlocking dp so don't do it explicitly in the
588 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000590 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Thiago Farina667a9292012-11-12 21:32:59 -0200591 unlock_dp_on_error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Barry Naujok556b8b12008-04-10 12:22:07 +1000593 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000594 &first_block, &free_list, resblks ?
595 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (error) {
597 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100598 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000600 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
602
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100603 if (is_dir) {
604 error = xfs_dir_init(tp, ip, dp);
605 if (error)
606 goto out_bmap_cancel;
607
608 error = xfs_bumplink(tp, dp);
609 if (error)
610 goto out_bmap_cancel;
611 }
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /*
614 * If this is a synchronous mount, make sure that the
615 * create transaction goes to disk before returning to
616 * the user.
617 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100618 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /*
622 * Attach the dquot(s) to the inodes and modify them incore.
623 * These ids of the inode couldn't have changed since the new
624 * inode has been locked ever since it was created.
625 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200626 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100628 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100629 if (error)
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000630 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000632 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000633 if (error)
634 goto out_release_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Christoph Hellwig7d095252009-06-08 15:33:32 +0200636 xfs_qm_dqrele(udqp);
637 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100639 *ipp = ip;
Christoph Hellwig288699f2010-06-23 18:11:15 +1000640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100642 out_bmap_cancel:
643 xfs_bmap_cancel(&free_list);
644 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100646 out_trans_cancel:
647 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000648 out_release_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
650 * Wait until after the current transaction is aborted to
651 * release the inode. This prevents recursive transactions
652 * and deadlocks from xfs_inactive.
653 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000654 if (ip)
655 IRELE(ip);
656
657 xfs_qm_dqrele(udqp);
658 xfs_qm_dqrele(gdqp);
659
660 if (unlock_dp_on_error)
661 xfs_iunlock(dp, XFS_ILOCK_EXCL);
662 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666int xfs_locked_n;
667int xfs_small_retries;
668int xfs_middle_retries;
669int xfs_lots_retries;
670int xfs_lock_delays;
671#endif
672
673/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000674 * Bump the subclass so xfs_lock_inodes() acquires each lock with
675 * a different value
676 */
677static inline int
678xfs_lock_inumorder(int lock_mode, int subclass)
679{
680 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +1000681 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000682 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +1000683 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000684
685 return lock_mode;
686}
687
688/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * The following routine will lock n inodes in exclusive mode.
690 * We assume the caller calls us with the inodes in i_ino order.
691 *
692 * We need to detect deadlock where an inode that we lock
693 * is in the AIL and we start waiting for another inode that is locked
694 * by a thread in a long running transaction (such as truncate). This can
695 * result in deadlock since the long running trans might need to wait
696 * for the inode we just locked in order to push the tail and free space
697 * in the log.
698 */
699void
700xfs_lock_inodes(
701 xfs_inode_t **ips,
702 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 uint lock_mode)
704{
705 int attempts = 0, i, j, try_lock;
706 xfs_log_item_t *lp;
707
708 ASSERT(ips && (inodes >= 2)); /* we need at least two */
709
Christoph Hellwigcfa853e2008-04-22 17:34:06 +1000710 try_lock = 0;
711 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713again:
714 for (; i < inodes; i++) {
715 ASSERT(ips[i]);
716
717 if (i && (ips[i] == ips[i-1])) /* Already locked */
718 continue;
719
720 /*
721 * If try_lock is not set yet, make sure all locked inodes
722 * are not in the AIL.
723 * If any are, set try_lock to be used later.
724 */
725
726 if (!try_lock) {
727 for (j = (i - 1); j >= 0 && !try_lock; j--) {
728 lp = (xfs_log_item_t *)ips[j]->i_itemp;
729 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
730 try_lock++;
731 }
732 }
733 }
734
735 /*
736 * If any of the previous locks we have locked is in the AIL,
737 * we must TRY to get the second and subsequent locks. If
738 * we can't get any, we must release all we have
739 * and try again.
740 */
741
742 if (try_lock) {
743 /* try_lock must be 0 if i is 0. */
744 /*
745 * try_lock means we have an inode locked
746 * that is in the AIL.
747 */
748 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000749 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 attempts++;
751
752 /*
753 * Unlock all previous guys and try again.
754 * xfs_iunlock will try to push the tail
755 * if the inode is in the AIL.
756 */
757
758 for(j = i - 1; j >= 0; j--) {
759
760 /*
761 * Check to see if we've already
762 * unlocked this one.
763 * Not the first one going back,
764 * and the inode ptr is the same.
765 */
766 if ((j != (i - 1)) && ips[j] ==
767 ips[j+1])
768 continue;
769
770 xfs_iunlock(ips[j], lock_mode);
771 }
772
773 if ((attempts % 5) == 0) {
774 delay(1); /* Don't just spin the CPU */
775#ifdef DEBUG
776 xfs_lock_delays++;
777#endif
778 }
779 i = 0;
780 try_lock = 0;
781 goto again;
782 }
783 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000784 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 }
787
788#ifdef DEBUG
789 if (attempts) {
790 if (attempts < 5) xfs_small_retries++;
791 else if (attempts < 100) xfs_middle_retries++;
792 else xfs_lots_retries++;
793 } else {
794 xfs_locked_n++;
795 }
796#endif
797}
798
David Chinnerf9114eb2008-09-17 16:51:21 +1000799/*
800 * xfs_lock_two_inodes() can only be used to lock one type of lock
801 * at a time - the iolock or the ilock, but not both at once. If
802 * we lock both at once, lockdep will report false positives saying
803 * we have violated locking orders.
804 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +1000805void
806xfs_lock_two_inodes(
807 xfs_inode_t *ip0,
808 xfs_inode_t *ip1,
809 uint lock_mode)
810{
811 xfs_inode_t *temp;
812 int attempts = 0;
813 xfs_log_item_t *lp;
814
David Chinnerf9114eb2008-09-17 16:51:21 +1000815 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
816 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +1000817 ASSERT(ip0->i_ino != ip1->i_ino);
818
819 if (ip0->i_ino > ip1->i_ino) {
820 temp = ip0;
821 ip0 = ip1;
822 ip1 = temp;
823 }
824
825 again:
826 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
827
828 /*
829 * If the first lock we have locked is in the AIL, we must TRY to get
830 * the second lock. If we can't get it, we must release the first one
831 * and try again.
832 */
833 lp = (xfs_log_item_t *)ip0->i_itemp;
834 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
835 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
836 xfs_iunlock(ip0, lock_mode);
837 if ((++attempts % 5) == 0)
838 delay(1); /* Don't just spin the CPU */
839 goto again;
840 }
841 } else {
842 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
843 }
844}
845
Christoph Hellwig993386c12007-08-28 16:12:30 +1000846int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847xfs_remove(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000848 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +1000849 struct xfs_name *name,
850 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Christoph Hellwig993386c12007-08-28 16:12:30 +1000852 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000854 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int error = 0;
856 xfs_bmap_free_t free_list;
857 xfs_fsblock_t first_block;
858 int cancel_flags;
859 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int link_zero;
861 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000862 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000864 trace_xfs_remove(dp, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (XFS_FORCED_SHUTDOWN(mp))
867 return XFS_ERROR(EIO);
868
Christoph Hellwig7d095252009-06-08 15:33:32 +0200869 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000870 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Christoph Hellwig7d095252009-06-08 15:33:32 +0200873 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000874 if (error)
875 goto std_return;
876
877 if (is_dir) {
878 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
879 log_count = XFS_DEFAULT_LOG_COUNT;
880 } else {
881 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
882 log_count = XFS_REMOVE_LOG_COUNT;
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * We try to get the real space reservation first,
888 * allowing for directory btree deletion(s) implying
889 * possible bmap insert(s). If we can't get the space
890 * reservation then we use 0 instead, and avoid the bmap
891 * btree insert(s) in the directory code by, if the bmap
892 * insert tries to happen, instead trimming the LAST
893 * block from the directory.
894 */
895 resblks = XFS_REMOVE_SPACE_RES(mp);
896 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000897 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (error == ENOSPC) {
899 resblks = 0;
900 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000901 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 if (error) {
904 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000905 cancel_flags = 0;
906 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
Christoph Hellwige1cccd92008-08-13 16:18:07 +1000909 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Christoph Hellwigddc34152011-09-19 15:00:54 +0000911 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
912 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000915 * If we're removing a directory perform some additional validation.
916 */
917 if (is_dir) {
918 ASSERT(ip->i_d.di_nlink >= 2);
919 if (ip->i_d.di_nlink != 2) {
920 error = XFS_ERROR(ENOTEMPTY);
921 goto out_trans_cancel;
922 }
923 if (!xfs_dir_isempty(ip)) {
924 error = XFS_ERROR(ENOTEMPTY);
925 goto out_trans_cancel;
926 }
927 }
928
Eric Sandeen9d87c312009-01-14 23:22:07 -0600929 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +1000930 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +1000931 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (error) {
933 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000934 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000936 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000938 if (is_dir) {
939 /*
940 * Drop the link from ip's "..".
941 */
942 error = xfs_droplink(tp, dp);
943 if (error)
944 goto out_bmap_cancel;
945
946 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +1100947 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000948 */
949 error = xfs_droplink(tp, ip);
950 if (error)
951 goto out_bmap_cancel;
952 } else {
953 /*
954 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +1100955 * inode here. For a directory this is done implicitly
956 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000957 */
958 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000961 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +1100962 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000963 */
964 error = xfs_droplink(tp, ip);
965 if (error)
966 goto out_bmap_cancel;
967
968 /*
969 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * we are in the transaction.
971 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000972 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * If this is a synchronous mount, make sure that the
976 * remove transaction goes to disk before returning to
977 * the user.
978 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000979 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100982 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000983 if (error)
984 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000986 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +1000987 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /*
David Chinner2a82b8b2007-07-11 11:09:12 +1000991 * If we are using filestreams, kill the stream association.
992 * If the file is still open it may get a new one but that
993 * will get killed on last close in xfs_close() so we don't
994 * have to worry about that.
995 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000996 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +1000997 xfs_filestream_deassociate(ip);
998
Christoph Hellwig288699f2010-06-23 18:11:15 +1000999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001001 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 xfs_bmap_cancel(&free_list);
1003 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001004 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001006 std_return:
1007 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Christoph Hellwig993386c12007-08-28 16:12:30 +10001010int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011xfs_link(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001012 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001013 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10001014 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001016 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int error;
1019 xfs_bmap_free_t free_list;
1020 xfs_fsblock_t first_block;
1021 int cancel_flags;
1022 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001025 trace_xfs_link(tdp, target_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Christoph Hellwiga3da7892008-03-06 13:46:12 +11001027 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (XFS_FORCED_SHUTDOWN(mp))
1030 return XFS_ERROR(EIO);
1031
Christoph Hellwig7d095252009-06-08 15:33:32 +02001032 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01001033 if (error)
1034 goto std_return;
1035
Christoph Hellwig7d095252009-06-08 15:33:32 +02001036 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (error)
1038 goto std_return;
1039
1040 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1041 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001042 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1044 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1045 if (error == ENOSPC) {
1046 resblks = 0;
1047 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1048 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1049 }
1050 if (error) {
1051 cancel_flags = 0;
1052 goto error_return;
1053 }
1054
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001055 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Christoph Hellwigddc34152011-09-19 15:00:54 +00001057 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1058 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /*
Nathan Scott365ca832005-06-21 15:39:12 +10001061 * If we are using project inheritance, we only allow hard link
1062 * creation in our tree when the project IDs are the same; else
1063 * the tree quota mechanism could be circumvented.
1064 */
1065 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001066 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10001067 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10001068 goto error_return;
1069 }
1070
Barry Naujok556b8b12008-04-10 12:22:07 +10001071 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1072 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 goto error_return;
1074
Eric Sandeen9d87c312009-01-14 23:22:07 -06001075 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Barry Naujok556b8b12008-04-10 12:22:07 +10001077 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1078 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (error)
1080 goto abort_return;
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001081 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1083
1084 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001085 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 /*
1089 * If this is a synchronous mount, make sure that the
1090 * link transaction goes to disk before returning to
1091 * the user.
1092 */
1093 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1094 xfs_trans_set_sync(tp);
1095 }
1096
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001097 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (error) {
1099 xfs_bmap_cancel(&free_list);
1100 goto abort_return;
1101 }
1102
Christoph Hellwig288699f2010-06-23 18:11:15 +10001103 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 abort_return:
1106 cancel_flags |= XFS_TRANS_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 error_return:
1108 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001109 std_return:
1110 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10001112
Christoph Hellwig993386c12007-08-28 16:12:30 +10001113int
Christoph Hellwig993386c12007-08-28 16:12:30 +10001114xfs_set_dmattrs(
1115 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 u_int evmask,
Christoph Hellwig993386c12007-08-28 16:12:30 +10001117 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001119 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int error;
1122
1123 if (!capable(CAP_SYS_ADMIN))
1124 return XFS_ERROR(EPERM);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (XFS_FORCED_SHUTDOWN(mp))
1127 return XFS_ERROR(EIO);
1128
1129 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1130 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1131 if (error) {
1132 xfs_trans_cancel(tp, 0);
1133 return error;
1134 }
1135 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001136 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Christoph Hellwig613d7042007-10-11 17:44:08 +10001138 ip->i_d.di_dmevmask = evmask;
1139 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001142 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 return error;
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147/*
1148 * xfs_alloc_file_space()
1149 * This routine allocates disk space for the given file.
1150 *
1151 * If alloc_type == 0, this request is for an ALLOCSP type
1152 * request which will change the file size. In this case, no
1153 * DMAPI event will be generated by the call. A TRUNCATE event
1154 * will be generated later by xfs_setattr.
1155 *
1156 * If alloc_type != 0, this request is for a RESVSP type
1157 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1158 * lower block boundary byte address is less than the file's
1159 * length.
1160 *
1161 * RETURNS:
1162 * 0 on success
1163 * errno on error
1164 *
1165 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001166STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167xfs_alloc_file_space(
1168 xfs_inode_t *ip,
1169 xfs_off_t offset,
1170 xfs_off_t len,
1171 int alloc_type,
1172 int attr_flags)
1173{
Nathan Scottdd9f4382006-01-11 15:28:28 +11001174 xfs_mount_t *mp = ip->i_mount;
1175 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 xfs_filblks_t allocated_fsb;
1177 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001178 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001180 xfs_fsblock_t firstfsb;
1181 int nimaps;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001182 int quota_flag;
1183 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001185 xfs_bmbt_irec_t imaps[1], *imapp;
1186 xfs_bmap_free_t free_list;
1187 uint qblocks, resblks, resrtextents;
1188 int committed;
1189 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001191 trace_xfs_alloc_file_space(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if (XFS_FORCED_SHUTDOWN(mp))
1194 return XFS_ERROR(EIO);
1195
Christoph Hellwig7d095252009-06-08 15:33:32 +02001196 error = xfs_qm_dqattach(ip, 0);
1197 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return error;
1199
1200 if (len <= 0)
1201 return XFS_ERROR(EINVAL);
1202
David Chinner957d0eb2007-06-18 16:50:37 +10001203 rt = XFS_IS_REALTIME_INODE(ip);
1204 extsz = xfs_get_extsz_hint(ip);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11001208 nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1210 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001213 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001216 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Nathan Scottdd9f4382006-01-11 15:28:28 +11001218 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11001219 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11001220 */
1221 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001223 do_div(s, extsz);
1224 s *= extsz;
1225 e = startoffset_fsb + allocatesize_fsb;
1226 if ((temp = do_mod(startoffset_fsb, extsz)))
1227 e += temp;
1228 if ((temp = do_mod(e, extsz)))
1229 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11001231 s = 0;
1232 e = allocatesize_fsb;
1233 }
1234
Dave Chinner72656c42010-09-03 12:19:33 +10001235 /*
1236 * The transaction reservation is limited to a 32-bit block
1237 * count, hence we need to limit the number of blocks we are
1238 * trying to reserve to avoid an overflow. We can't allocate
1239 * more than @nimaps extents, and an extent is limited on disk
1240 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1241 */
1242 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
Nathan Scottdd9f4382006-01-11 15:28:28 +11001243 if (unlikely(rt)) {
Dave Chinner72656c42010-09-03 12:19:33 +10001244 resrtextents = qblocks = resblks;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001245 resrtextents /= mp->m_sb.sb_rextsize;
1246 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1247 quota_flag = XFS_QMOPT_RES_RTBLKS;
1248 } else {
1249 resrtextents = 0;
Dave Chinner72656c42010-09-03 12:19:33 +10001250 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001251 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
1254 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001255 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
1257 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001258 error = xfs_trans_reserve(tp, resblks,
1259 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 XFS_TRANS_PERM_LOG_RES,
1261 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001263 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
1265 if (error) {
1266 /*
1267 * Free the transaction structure.
1268 */
1269 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1270 xfs_trans_cancel(tp, 0);
1271 break;
1272 }
1273 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001274 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1275 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (error)
1277 goto error1;
1278
Christoph Hellwigddc34152011-09-19 15:00:54 +00001279 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Eric Sandeen9d87c312009-01-14 23:22:07 -06001281 xfs_bmap_init(&free_list, &firstfsb);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001282 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1283 allocatesize_fsb, alloc_type, &firstfsb,
1284 0, imapp, &nimaps, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (error) {
1286 goto error0;
1287 }
1288
1289 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11001290 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001292 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (error) {
1294 goto error0;
1295 }
1296
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001297 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1299 if (error) {
1300 break;
1301 }
1302
1303 allocated_fsb = imapp->br_blockcount;
1304
Nathan Scottdd9f4382006-01-11 15:28:28 +11001305 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 error = XFS_ERROR(ENOSPC);
1307 break;
1308 }
1309
1310 startoffset_fsb += allocated_fsb;
1311 allocatesize_fsb -= allocated_fsb;
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 return error;
1315
Nathan Scottdd9f4382006-01-11 15:28:28 +11001316error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 xfs_bmap_cancel(&free_list);
Dave Chinnerea562ed2012-05-08 20:48:53 +10001318 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11001319
1320error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1322 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001323 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326/*
1327 * Zero file bytes between startoff and endoff inclusive.
1328 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001329 *
1330 * This function is used by xfs_free_file_space() to zero
1331 * partial blocks when the range to free is not block aligned.
1332 * When unreserving space with boundaries that are not block
1333 * aligned we round up the start and round down the end
1334 * boundaries and then use this function to zero the parts of
1335 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
1337STATIC int
1338xfs_zero_remaining_bytes(
1339 xfs_inode_t *ip,
1340 xfs_off_t startoff,
1341 xfs_off_t endoff)
1342{
1343 xfs_bmbt_irec_t imap;
1344 xfs_fileoff_t offset_fsb;
1345 xfs_off_t lastoffset;
1346 xfs_off_t offset;
1347 xfs_buf_t *bp;
1348 xfs_mount_t *mp = ip->i_mount;
1349 int nimap;
1350 int error = 0;
1351
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001352 /*
1353 * Avoid doing I/O beyond eof - it's not necessary
1354 * since nothing can read beyond eof. The space will
1355 * be zeroed when the file is extended anyway.
1356 */
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001357 if (startoff >= XFS_ISIZE(ip))
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001358 return 0;
1359
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001360 if (endoff > XFS_ISIZE(ip))
1361 endoff = XFS_ISIZE(ip);
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10001362
Dave Chinner686865f2010-09-24 20:07:47 +10001363 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1364 mp->m_rtdev_targp : mp->m_ddev_targp,
Dave Chinneraa5c1582012-04-23 15:58:56 +10001365 BTOBB(mp->m_sb.sb_blocksize), 0);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11001366 if (!bp)
1367 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001369 xfs_buf_unlock(bp);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1372 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1373 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00001374 error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (error || nimap < 1)
1376 break;
1377 ASSERT(imap.br_blockcount >= 1);
1378 ASSERT(imap.br_startoff == offset_fsb);
1379 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1380 if (lastoffset > endoff)
1381 lastoffset = endoff;
1382 if (imap.br_startblock == HOLESTARTBLOCK)
1383 continue;
1384 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1385 if (imap.br_state == XFS_EXT_UNWRITTEN)
1386 continue;
1387 XFS_BUF_UNDONE(bp);
1388 XFS_BUF_UNWRITE(bp);
1389 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001390 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001392 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10001393 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00001394 xfs_buf_ioerror_alert(bp,
1395 "xfs_zero_remaining_bytes(read)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
1397 }
Chandra Seetharaman62926042011-07-22 23:40:15 +00001398 memset(bp->b_addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
1400 0, lastoffset - offset + 1);
1401 XFS_BUF_UNDONE(bp);
1402 XFS_BUF_UNREAD(bp);
1403 XFS_BUF_WRITE(bp);
1404 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001405 error = xfs_buf_iowait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10001406 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00001407 xfs_buf_ioerror_alert(bp,
1408 "xfs_zero_remaining_bytes(write)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410 }
1411 }
1412 xfs_buf_free(bp);
1413 return error;
1414}
1415
1416/*
1417 * xfs_free_file_space()
1418 * This routine frees disk space for the given file.
1419 *
1420 * This routine is only called by xfs_change_file_space
1421 * for an UNRESVSP type call.
1422 *
1423 * RETURNS:
1424 * 0 on success
1425 * errno on error
1426 *
1427 */
1428STATIC int
1429xfs_free_file_space(
1430 xfs_inode_t *ip,
1431 xfs_off_t offset,
1432 xfs_off_t len,
1433 int attr_flags)
1434{
1435 int committed;
1436 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 xfs_fileoff_t endoffset_fsb;
1438 int error;
1439 xfs_fsblock_t firstfsb;
1440 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 xfs_bmbt_irec_t imap;
1442 xfs_off_t ioffset;
1443 xfs_extlen_t mod=0;
1444 xfs_mount_t *mp;
1445 int nimap;
1446 uint resblks;
Dave Chinner28ca4892013-05-20 09:51:09 +10001447 xfs_off_t rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int rt;
1449 xfs_fileoff_t startoffset_fsb;
1450 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07001451 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 mp = ip->i_mount;
1454
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001455 trace_xfs_free_file_space(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10001456
Christoph Hellwig7d095252009-06-08 15:33:32 +02001457 error = xfs_qm_dqattach(ip, 0);
1458 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return error;
1460
1461 error = 0;
1462 if (len <= 0) /* if nothing being freed */
1463 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11001464 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Christoph Hellwig288699f2010-06-23 18:11:15 +10001466 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Christoph Hellwig0f285c82008-07-18 17:13:28 +10001468 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07001469 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11001470 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01001472 /* wait for the completion of any pending DIOs */
Christoph Hellwig4a06fd22011-08-23 08:28:13 +00001473 inode_dio_wait(VFS_I(ip));
Yingping Lu9fa80462006-03-22 12:44:35 +11001474 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07001475
Dave Chinner28ca4892013-05-20 09:51:09 +10001476 rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 ioffset = offset & ~(rounding - 1);
Dave Chinnerfb595812012-11-12 22:53:57 +11001478 error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
1479 ioffset, -1);
1480 if (error)
1481 goto out_unlock_iolock;
1482 truncate_pagecache_range(VFS_I(ip), ioffset, -1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /*
1485 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001486 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * actually need to zero the extent edges. Otherwise xfs_bunmapi
1488 * will take care of it for us.
1489 */
Eric Sandeen62118702008-03-06 13:44:28 +11001490 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00001492 error = xfs_bmapi_read(ip, startoffset_fsb, 1,
1493 &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (error)
1495 goto out_unlock_iolock;
1496 ASSERT(nimap == 0 || nimap == 1);
1497 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1498 xfs_daddr_t block;
1499
1500 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1501 block = imap.br_startblock;
1502 mod = do_div(block, mp->m_sb.sb_rextsize);
1503 if (mod)
1504 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1505 }
1506 nimap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00001507 error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
1508 &imap, &nimap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (error)
1510 goto out_unlock_iolock;
1511 ASSERT(nimap == 0 || nimap == 1);
1512 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1513 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1514 mod++;
1515 if (mod && (mod != mp->m_sb.sb_rextsize))
1516 endoffset_fsb -= mod;
1517 }
1518 }
1519 if ((done = (endoffset_fsb <= startoffset_fsb)))
1520 /*
1521 * One contiguous piece to clear
1522 */
1523 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
1524 else {
1525 /*
1526 * Some full blocks, possibly two pieces to clear
1527 */
1528 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
1529 error = xfs_zero_remaining_bytes(ip, offset,
1530 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
1531 if (!error &&
1532 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
1533 error = xfs_zero_remaining_bytes(ip,
1534 XFS_FSB_TO_B(mp, endoffset_fsb),
1535 offset + len - 1);
1536 }
1537
1538 /*
1539 * free file space until done or until there is an error
1540 */
1541 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1542 while (!error && !done) {
1543
1544 /*
David Chinner91ebecc2007-07-19 16:28:30 +10001545 * allocate and setup the transaction. Allow this
1546 * transaction to dip into the reserve blocks to ensure
1547 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
1549 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10001550 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 error = xfs_trans_reserve(tp,
1552 resblks,
1553 XFS_WRITE_LOG_RES(mp),
1554 0,
1555 XFS_TRANS_PERM_LOG_RES,
1556 XFS_WRITE_LOG_COUNT);
1557
1558 /*
1559 * check for running out of space
1560 */
1561 if (error) {
1562 /*
1563 * Free the transaction structure.
1564 */
1565 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1566 xfs_trans_cancel(tp, 0);
1567 break;
1568 }
1569 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001570 error = xfs_trans_reserve_quota(tp, mp,
1571 ip->i_udquot, ip->i_gdquot,
1572 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (error)
1574 goto error1;
1575
Christoph Hellwigddc34152011-09-19 15:00:54 +00001576 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 /*
1579 * issue the bunmapi() call to free the blocks
1580 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001581 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001582 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 endoffset_fsb - startoffset_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001584 0, 2, &firstfsb, &free_list, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (error) {
1586 goto error0;
1587 }
1588
1589 /*
1590 * complete the transaction
1591 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001592 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (error) {
1594 goto error0;
1595 }
1596
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001597 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1599 }
1600
1601 out_unlock_iolock:
1602 if (need_iolock)
1603 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1604 return error;
1605
1606 error0:
1607 xfs_bmap_cancel(&free_list);
1608 error1:
1609 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1610 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
1611 XFS_ILOCK_EXCL);
1612 return error;
1613}
1614
Dave Chinneref9d8732012-11-29 15:26:33 +11001615
1616STATIC int
1617xfs_zero_file_space(
1618 struct xfs_inode *ip,
1619 xfs_off_t offset,
1620 xfs_off_t len,
1621 int attr_flags)
1622{
1623 struct xfs_mount *mp = ip->i_mount;
1624 uint granularity;
1625 xfs_off_t start_boundary;
1626 xfs_off_t end_boundary;
1627 int error;
1628
1629 granularity = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
1630
1631 /*
1632 * Round the range of extents we are going to convert inwards. If the
1633 * offset is aligned, then it doesn't get changed so we zero from the
1634 * start of the block offset points to.
1635 */
1636 start_boundary = round_up(offset, granularity);
1637 end_boundary = round_down(offset + len, granularity);
1638
1639 ASSERT(start_boundary >= offset);
1640 ASSERT(end_boundary <= offset + len);
1641
1642 if (!(attr_flags & XFS_ATTR_NOLOCK))
1643 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1644
1645 if (start_boundary < end_boundary - 1) {
1646 /* punch out the page cache over the conversion range */
1647 truncate_pagecache_range(VFS_I(ip), start_boundary,
1648 end_boundary - 1);
1649 /* convert the blocks */
1650 error = xfs_alloc_file_space(ip, start_boundary,
1651 end_boundary - start_boundary - 1,
1652 XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT,
1653 attr_flags);
1654 if (error)
1655 goto out_unlock;
1656
1657 /* We've handled the interior of the range, now for the edges */
1658 if (start_boundary != offset)
1659 error = xfs_iozero(ip, offset, start_boundary - offset);
1660 if (error)
1661 goto out_unlock;
1662
1663 if (end_boundary != offset + len)
1664 error = xfs_iozero(ip, end_boundary,
1665 offset + len - end_boundary);
1666
1667 } else {
1668 /*
1669 * It's either a sub-granularity range or the range spanned lies
1670 * partially across two adjacent blocks.
1671 */
1672 error = xfs_iozero(ip, offset, len);
1673 }
1674
1675out_unlock:
1676 if (!(attr_flags & XFS_ATTR_NOLOCK))
1677 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1678 return error;
1679
1680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682/*
1683 * xfs_change_file_space()
1684 * This routine allocates or frees disk space for the given file.
1685 * The user specified parameters are checked for alignment and size
1686 * limitations.
1687 *
1688 * RETURNS:
1689 * 0 on success
1690 * errno on error
1691 *
1692 */
1693int
1694xfs_change_file_space(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001695 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 int cmd,
1697 xfs_flock64_t *bf,
1698 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int attr_flags)
1700{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001701 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 int clrprealloc;
1703 int error;
1704 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int setprealloc;
1706 xfs_off_t startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10001708 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Christoph Hellwig993386c12007-08-28 16:12:30 +10001710 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return XFS_ERROR(EINVAL);
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 switch (bf->l_whence) {
1714 case 0: /*SEEK_SET*/
1715 break;
1716 case 1: /*SEEK_CUR*/
1717 bf->l_start += offset;
1718 break;
1719 case 2: /*SEEK_END*/
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001720 bf->l_start += XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 break;
1722 default:
1723 return XFS_ERROR(EINVAL);
1724 }
1725
Dave Chinnerf5b89112012-11-14 17:42:47 +11001726 /*
1727 * length of <= 0 for resv/unresv/zero is invalid. length for
1728 * alloc/free is ignored completely and we have no idea what userspace
1729 * might have set it to, so set it to zero to allow range
1730 * checks to pass.
1731 */
1732 switch (cmd) {
1733 case XFS_IOC_ZERO_RANGE:
1734 case XFS_IOC_RESVSP:
1735 case XFS_IOC_RESVSP64:
1736 case XFS_IOC_UNRESVSP:
1737 case XFS_IOC_UNRESVSP64:
1738 if (bf->l_len <= 0)
1739 return XFS_ERROR(EINVAL);
1740 break;
1741 default:
1742 bf->l_len = 0;
1743 break;
1744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Dave Chinner32972382012-06-08 15:44:54 +10001746 if (bf->l_start < 0 ||
1747 bf->l_start > mp->m_super->s_maxbytes ||
Dave Chinnerf5b89112012-11-14 17:42:47 +11001748 bf->l_start + bf->l_len < 0 ||
1749 bf->l_start + bf->l_len >= mp->m_super->s_maxbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return XFS_ERROR(EINVAL);
1751
1752 bf->l_whence = 0;
1753
1754 startoffset = bf->l_start;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001755 fsize = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 setprealloc = clrprealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 switch (cmd) {
Dave Chinner44722352010-08-24 12:02:11 +10001759 case XFS_IOC_ZERO_RANGE:
Dave Chinneref9d8732012-11-29 15:26:33 +11001760 error = xfs_zero_file_space(ip, startoffset, bf->l_len,
1761 attr_flags);
1762 if (error)
1763 return error;
1764 setprealloc = 1;
1765 break;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 case XFS_IOC_RESVSP:
1768 case XFS_IOC_RESVSP64:
1769 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
Dave Chinneref9d8732012-11-29 15:26:33 +11001770 XFS_BMAPI_PREALLOC, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (error)
1772 return error;
1773 setprealloc = 1;
1774 break;
1775
1776 case XFS_IOC_UNRESVSP:
1777 case XFS_IOC_UNRESVSP64:
1778 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
1779 attr_flags)))
1780 return error;
1781 break;
1782
1783 case XFS_IOC_ALLOCSP:
1784 case XFS_IOC_ALLOCSP64:
1785 case XFS_IOC_FREESP:
1786 case XFS_IOC_FREESP64:
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10001787 /*
1788 * These operations actually do IO when extending the file, but
1789 * the allocation is done seperately to the zeroing that is
1790 * done. This set of operations need to be serialised against
1791 * other IO operations, such as truncate and buffered IO. We
1792 * need to take the IOLOCK here to serialise the allocation and
1793 * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
1794 * truncate, direct IO) from racing against the transient
1795 * allocated but not written state we can have here.
1796 */
1797 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (startoffset > fsize) {
1799 error = xfs_alloc_file_space(ip, fsize,
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10001800 startoffset - fsize, 0,
1801 attr_flags | XFS_ATTR_NOLOCK);
1802 if (error) {
1803 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 break;
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10001805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807
Christoph Hellwig0f285c82008-07-18 17:13:28 +10001808 iattr.ia_valid = ATTR_SIZE;
1809 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Dave Chinnerbc4010ec2012-04-23 15:58:57 +10001811 error = xfs_setattr_size(ip, &iattr,
1812 attr_flags | XFS_ATTR_NOLOCK);
1813 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 if (error)
1816 return error;
1817
1818 clrprealloc = 1;
1819 break;
1820
1821 default:
1822 ASSERT(0);
1823 return XFS_ERROR(EINVAL);
1824 }
1825
1826 /*
1827 * update the inode timestamp, mode, and prealloc flag bits
1828 */
1829 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
1830
1831 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
1832 0, 0, 0))) {
1833 /* ASSERT(0); */
1834 xfs_trans_cancel(tp, 0);
1835 return error;
1836 }
1837
1838 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001839 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Christoph Hellwig0f285c82008-07-18 17:13:28 +10001841 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 ip->i_d.di_mode &= ~S_ISUID;
1843
1844 /*
1845 * Note that we don't have to worry about mandatory
1846 * file locking being disabled here because we only
1847 * clear the S_ISGID bit if the Group execute bit is
1848 * on, but if it was on then mandatory locking wouldn't
1849 * have been enabled.
1850 */
1851 if (ip->i_d.di_mode & S_IXGRP)
1852 ip->i_d.di_mode &= ~S_ISGID;
1853
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001854 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856 if (setprealloc)
1857 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
1858 else if (clrprealloc)
1859 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
1860
1861 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinner82878892011-03-26 09:13:08 +11001862 if (attr_flags & XFS_ATTR_SYNC)
1863 xfs_trans_set_sync(tp);
Christoph Hellwig23bb0be2011-09-18 20:47:51 +00001864 return xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}