]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/xfs/xfs_utils.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jk/spufs
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / xfs_utils.c
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_attr_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h"
36 #include "xfs_bmap.h"
37 #include "xfs_error.h"
38 #include "xfs_quota.h"
39 #include "xfs_rw.h"
40 #include "xfs_itable.h"
41 #include "xfs_utils.h"
42
43
44 int
45 xfs_dir_lookup_int(
46 xfs_inode_t *dp,
47 uint lock_mode,
48 struct xfs_name *name,
49 xfs_ino_t *inum,
50 xfs_inode_t **ipp)
51 {
52 int error;
53
54 xfs_itrace_entry(dp);
55
56 error = xfs_dir_lookup(NULL, dp, name, inum);
57 if (!error) {
58 /*
59 * Unlock the directory. We do this because we can't
60 * hold the directory lock while doing the vn_get()
61 * in xfs_iget(). Doing so could cause us to hold
62 * a lock while waiting for the inode to finish
63 * being inactive while it's waiting for a log
64 * reservation in the inactive routine.
65 */
66 xfs_iunlock(dp, lock_mode);
67 error = xfs_iget(dp->i_mount, NULL, *inum, 0, 0, ipp, 0);
68 xfs_ilock(dp, lock_mode);
69
70 if (error) {
71 *ipp = NULL;
72 } else if ((*ipp)->i_d.di_mode == 0) {
73 /*
74 * The inode has been freed. Something is
75 * wrong so just get out of here.
76 */
77 xfs_iunlock(dp, lock_mode);
78 xfs_iput_new(*ipp, 0);
79 *ipp = NULL;
80 xfs_ilock(dp, lock_mode);
81 error = XFS_ERROR(ENOENT);
82 }
83 }
84 return error;
85 }
86
87 /*
88 * Allocates a new inode from disk and return a pointer to the
89 * incore copy. This routine will internally commit the current
90 * transaction and allocate a new one if the Space Manager needed
91 * to do an allocation to replenish the inode free-list.
92 *
93 * This routine is designed to be called from xfs_create and
94 * xfs_create_dir.
95 *
96 */
97 int
98 xfs_dir_ialloc(
99 xfs_trans_t **tpp, /* input: current transaction;
100 output: may be a new transaction. */
101 xfs_inode_t *dp, /* directory within whose allocate
102 the inode. */
103 mode_t mode,
104 xfs_nlink_t nlink,
105 xfs_dev_t rdev,
106 cred_t *credp,
107 prid_t prid, /* project id */
108 int okalloc, /* ok to allocate new space */
109 xfs_inode_t **ipp, /* pointer to inode; it will be
110 locked. */
111 int *committed)
112
113 {
114 xfs_trans_t *tp;
115 xfs_trans_t *ntp;
116 xfs_inode_t *ip;
117 xfs_buf_t *ialloc_context = NULL;
118 boolean_t call_again = B_FALSE;
119 int code;
120 uint log_res;
121 uint log_count;
122 void *dqinfo;
123 uint tflags;
124
125 tp = *tpp;
126 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
127
128 /*
129 * xfs_ialloc will return a pointer to an incore inode if
130 * the Space Manager has an available inode on the free
131 * list. Otherwise, it will do an allocation and replenish
132 * the freelist. Since we can only do one allocation per
133 * transaction without deadlocks, we will need to commit the
134 * current transaction and start a new one. We will then
135 * need to call xfs_ialloc again to get the inode.
136 *
137 * If xfs_ialloc did an allocation to replenish the freelist,
138 * it returns the bp containing the head of the freelist as
139 * ialloc_context. We will hold a lock on it across the
140 * transaction commit so that no other process can steal
141 * the inode(s) that we've just allocated.
142 */
143 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc,
144 &ialloc_context, &call_again, &ip);
145
146 /*
147 * Return an error if we were unable to allocate a new inode.
148 * This should only happen if we run out of space on disk or
149 * encounter a disk error.
150 */
151 if (code) {
152 *ipp = NULL;
153 return code;
154 }
155 if (!call_again && (ip == NULL)) {
156 *ipp = NULL;
157 return XFS_ERROR(ENOSPC);
158 }
159
160 /*
161 * If call_again is set, then we were unable to get an
162 * inode in one operation. We need to commit the current
163 * transaction and call xfs_ialloc() again. It is guaranteed
164 * to succeed the second time.
165 */
166 if (call_again) {
167
168 /*
169 * Normally, xfs_trans_commit releases all the locks.
170 * We call bhold to hang on to the ialloc_context across
171 * the commit. Holding this buffer prevents any other
172 * processes from doing any allocations in this
173 * allocation group.
174 */
175 xfs_trans_bhold(tp, ialloc_context);
176 /*
177 * Save the log reservation so we can use
178 * them in the next transaction.
179 */
180 log_res = xfs_trans_get_log_res(tp);
181 log_count = xfs_trans_get_log_count(tp);
182
183 /*
184 * We want the quota changes to be associated with the next
185 * transaction, NOT this one. So, detach the dqinfo from this
186 * and attach it to the next transaction.
187 */
188 dqinfo = NULL;
189 tflags = 0;
190 if (tp->t_dqinfo) {
191 dqinfo = (void *)tp->t_dqinfo;
192 tp->t_dqinfo = NULL;
193 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
194 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
195 }
196
197 ntp = xfs_trans_dup(tp);
198 code = xfs_trans_commit(tp, 0);
199 tp = ntp;
200 if (committed != NULL) {
201 *committed = 1;
202 }
203 /*
204 * If we get an error during the commit processing,
205 * release the buffer that is still held and return
206 * to the caller.
207 */
208 if (code) {
209 xfs_buf_relse(ialloc_context);
210 if (dqinfo) {
211 tp->t_dqinfo = dqinfo;
212 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
213 }
214 *tpp = ntp;
215 *ipp = NULL;
216 return code;
217 }
218 code = xfs_trans_reserve(tp, 0, log_res, 0,
219 XFS_TRANS_PERM_LOG_RES, log_count);
220 /*
221 * Re-attach the quota info that we detached from prev trx.
222 */
223 if (dqinfo) {
224 tp->t_dqinfo = dqinfo;
225 tp->t_flags |= tflags;
226 }
227
228 if (code) {
229 xfs_buf_relse(ialloc_context);
230 *tpp = ntp;
231 *ipp = NULL;
232 return code;
233 }
234 xfs_trans_bjoin(tp, ialloc_context);
235
236 /*
237 * Call ialloc again. Since we've locked out all
238 * other allocations in this allocation group,
239 * this call should always succeed.
240 */
241 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid,
242 okalloc, &ialloc_context, &call_again, &ip);
243
244 /*
245 * If we get an error at this point, return to the caller
246 * so that the current transaction can be aborted.
247 */
248 if (code) {
249 *tpp = tp;
250 *ipp = NULL;
251 return code;
252 }
253 ASSERT ((!call_again) && (ip != NULL));
254
255 } else {
256 if (committed != NULL) {
257 *committed = 0;
258 }
259 }
260
261 *ipp = ip;
262 *tpp = tp;
263
264 return 0;
265 }
266
267 /*
268 * Decrement the link count on an inode & log the change.
269 * If this causes the link count to go to zero, initiate the
270 * logging activity required to truncate a file.
271 */
272 int /* error */
273 xfs_droplink(
274 xfs_trans_t *tp,
275 xfs_inode_t *ip)
276 {
277 int error;
278
279 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
280
281 ASSERT (ip->i_d.di_nlink > 0);
282 ip->i_d.di_nlink--;
283 drop_nlink(ip->i_vnode);
284 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
285
286 error = 0;
287 if (ip->i_d.di_nlink == 0) {
288 /*
289 * We're dropping the last link to this file.
290 * Move the on-disk inode to the AGI unlinked list.
291 * From xfs_inactive() we will pull the inode from
292 * the list and free it.
293 */
294 error = xfs_iunlink(tp, ip);
295 }
296 return error;
297 }
298
299 /*
300 * This gets called when the inode's version needs to be changed from 1 to 2.
301 * Currently this happens when the nlink field overflows the old 16-bit value
302 * or when chproj is called to change the project for the first time.
303 * As a side effect the superblock version will also get rev'd
304 * to contain the NLINK bit.
305 */
306 void
307 xfs_bump_ino_vers2(
308 xfs_trans_t *tp,
309 xfs_inode_t *ip)
310 {
311 xfs_mount_t *mp;
312
313 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
314 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1);
315
316 ip->i_d.di_version = XFS_DINODE_VERSION_2;
317 ip->i_d.di_onlink = 0;
318 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
319 mp = tp->t_mountp;
320 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
321 spin_lock(&mp->m_sb_lock);
322 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
323 xfs_sb_version_addnlink(&mp->m_sb);
324 spin_unlock(&mp->m_sb_lock);
325 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
326 } else {
327 spin_unlock(&mp->m_sb_lock);
328 }
329 }
330 /* Caller must log the inode */
331 }
332
333 /*
334 * Increment the link count on an inode & log the change.
335 */
336 int
337 xfs_bumplink(
338 xfs_trans_t *tp,
339 xfs_inode_t *ip)
340 {
341 if (ip->i_d.di_nlink >= XFS_MAXLINK)
342 return XFS_ERROR(EMLINK);
343 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
344
345 ASSERT(ip->i_d.di_nlink > 0);
346 ip->i_d.di_nlink++;
347 inc_nlink(ip->i_vnode);
348 if ((ip->i_d.di_version == XFS_DINODE_VERSION_1) &&
349 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
350 /*
351 * The inode has increased its number of links beyond
352 * what can fit in an old format inode. It now needs
353 * to be converted to a version 2 inode with a 32 bit
354 * link count. If this is the first inode in the file
355 * system to do this, then we need to bump the superblock
356 * version number as well.
357 */
358 xfs_bump_ino_vers2(tp, ip);
359 }
360
361 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
362 return 0;
363 }
364
365 /*
366 * Try to truncate the given file to 0 length. Currently called
367 * only out of xfs_remove when it has to truncate a file to free
368 * up space for the remove to proceed.
369 */
370 int
371 xfs_truncate_file(
372 xfs_mount_t *mp,
373 xfs_inode_t *ip)
374 {
375 xfs_trans_t *tp;
376 int error;
377
378 #ifdef QUOTADEBUG
379 /*
380 * This is called to truncate the quotainodes too.
381 */
382 if (XFS_IS_UQUOTA_ON(mp)) {
383 if (ip->i_ino != mp->m_sb.sb_uquotino)
384 ASSERT(ip->i_udquot);
385 }
386 if (XFS_IS_OQUOTA_ON(mp)) {
387 if (ip->i_ino != mp->m_sb.sb_gquotino)
388 ASSERT(ip->i_gdquot);
389 }
390 #endif
391 /*
392 * Make the call to xfs_itruncate_start before starting the
393 * transaction, because we cannot make the call while we're
394 * in a transaction.
395 */
396 xfs_ilock(ip, XFS_IOLOCK_EXCL);
397 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0);
398 if (error) {
399 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
400 return error;
401 }
402
403 tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
404 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
405 XFS_TRANS_PERM_LOG_RES,
406 XFS_ITRUNCATE_LOG_COUNT))) {
407 xfs_trans_cancel(tp, 0);
408 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
409 return error;
410 }
411
412 /*
413 * Follow the normal truncate locking protocol. Since we
414 * hold the inode in the transaction, we know that it's number
415 * of references will stay constant.
416 */
417 xfs_ilock(ip, XFS_ILOCK_EXCL);
418 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
419 xfs_trans_ihold(tp, ip);
420 /*
421 * Signal a sync xaction. The only case where that isn't
422 * the case is if we're truncating an already unlinked file
423 * on a wsync fs. In that case, we know the blocks can't
424 * reappear in the file because the links to file are
425 * permanently toast. Currently, we're always going to
426 * want a sync transaction because this code is being
427 * called from places where nlink is guaranteed to be 1
428 * but I'm leaving the tests in to protect against future
429 * changes -- rcc.
430 */
431 error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0,
432 XFS_DATA_FORK,
433 ((ip->i_d.di_nlink != 0 ||
434 !(mp->m_flags & XFS_MOUNT_WSYNC))
435 ? 1 : 0));
436 if (error) {
437 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
438 XFS_TRANS_ABORT);
439 } else {
440 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
441 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
442 }
443 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
444
445 return error;
446 }