[XFS] kill sys_cred
[deliverable/linux.git] / fs / xfs / xfs_vnodeops.c
1 /*
2 * Copyright (c) 2000-2006 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
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_types.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_inum.h"
25 #include "xfs_trans.h"
26 #include "xfs_sb.h"
27 #include "xfs_ag.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
44 #include "xfs_bmap.h"
45 #include "xfs_attr.h"
46 #include "xfs_rw.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_utils.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_trans_space.h"
52 #include "xfs_log_priv.h"
53 #include "xfs_filestream.h"
54 #include "xfs_vnodeops.h"
55
56 int
57 xfs_open(
58 xfs_inode_t *ip)
59 {
60 int mode;
61
62 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
63 return XFS_ERROR(EIO);
64
65 /*
66 * If it's a directory with any blocks, read-ahead block 0
67 * as we're almost certain to have the next operation be a read there.
68 */
69 if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
70 mode = xfs_ilock_map_shared(ip);
71 if (ip->i_d.di_nextents > 0)
72 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
73 xfs_iunlock(ip, mode);
74 }
75 return 0;
76 }
77
78 int
79 xfs_setattr(
80 struct xfs_inode *ip,
81 struct iattr *iattr,
82 int flags)
83 {
84 xfs_mount_t *mp = ip->i_mount;
85 struct inode *inode = VFS_I(ip);
86 int mask = iattr->ia_valid;
87 xfs_trans_t *tp;
88 int code;
89 uint lock_flags;
90 uint commit_flags=0;
91 uid_t uid=0, iuid=0;
92 gid_t gid=0, igid=0;
93 int timeflags = 0;
94 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
95 int file_owner;
96 int need_iolock = 1;
97
98 xfs_itrace_entry(ip);
99
100 if (mp->m_flags & XFS_MOUNT_RDONLY)
101 return XFS_ERROR(EROFS);
102
103 if (XFS_FORCED_SHUTDOWN(mp))
104 return XFS_ERROR(EIO);
105
106 olddquot1 = olddquot2 = NULL;
107 udqp = gdqp = NULL;
108
109 /*
110 * If disk quotas is on, we make sure that the dquots do exist on disk,
111 * before we start any other transactions. Trying to do this later
112 * is messy. We don't care to take a readlock to look at the ids
113 * in inode here, because we can't hold it across the trans_reserve.
114 * If the IDs do change before we take the ilock, we're covered
115 * because the i_*dquot fields will get updated anyway.
116 */
117 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
118 uint qflags = 0;
119
120 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
121 uid = iattr->ia_uid;
122 qflags |= XFS_QMOPT_UQUOTA;
123 } else {
124 uid = ip->i_d.di_uid;
125 }
126 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
127 gid = iattr->ia_gid;
128 qflags |= XFS_QMOPT_GQUOTA;
129 } else {
130 gid = ip->i_d.di_gid;
131 }
132
133 /*
134 * We take a reference when we initialize udqp and gdqp,
135 * so it is important that we never blindly double trip on
136 * the same variable. See xfs_create() for an example.
137 */
138 ASSERT(udqp == NULL);
139 ASSERT(gdqp == NULL);
140 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
141 qflags, &udqp, &gdqp);
142 if (code)
143 return code;
144 }
145
146 /*
147 * For the other attributes, we acquire the inode lock and
148 * first do an error checking pass.
149 */
150 tp = NULL;
151 lock_flags = XFS_ILOCK_EXCL;
152 if (flags & XFS_ATTR_NOLOCK)
153 need_iolock = 0;
154 if (!(mask & ATTR_SIZE)) {
155 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
156 (mp->m_flags & XFS_MOUNT_WSYNC)) {
157 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
158 commit_flags = 0;
159 if ((code = xfs_trans_reserve(tp, 0,
160 XFS_ICHANGE_LOG_RES(mp), 0,
161 0, 0))) {
162 lock_flags = 0;
163 goto error_return;
164 }
165 }
166 } else {
167 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
168 !(flags & XFS_ATTR_DMI)) {
169 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
170 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
171 iattr->ia_size, 0, dmflags, NULL);
172 if (code) {
173 lock_flags = 0;
174 goto error_return;
175 }
176 }
177 if (need_iolock)
178 lock_flags |= XFS_IOLOCK_EXCL;
179 }
180
181 xfs_ilock(ip, lock_flags);
182
183 /* boolean: are we the file owner? */
184 file_owner = (current_fsuid() == ip->i_d.di_uid);
185
186 /*
187 * Change various properties of a file.
188 * Only the owner or users with CAP_FOWNER
189 * capability may do these things.
190 */
191 if (mask & (ATTR_MODE|ATTR_UID|ATTR_GID)) {
192 /*
193 * CAP_FOWNER overrides the following restrictions:
194 *
195 * The user ID of the calling process must be equal
196 * to the file owner ID, except in cases where the
197 * CAP_FSETID capability is applicable.
198 */
199 if (!file_owner && !capable(CAP_FOWNER)) {
200 code = XFS_ERROR(EPERM);
201 goto error_return;
202 }
203
204 /*
205 * CAP_FSETID overrides the following restrictions:
206 *
207 * The effective user ID of the calling process shall match
208 * the file owner when setting the set-user-ID and
209 * set-group-ID bits on that file.
210 *
211 * The effective group ID or one of the supplementary group
212 * IDs of the calling process shall match the group owner of
213 * the file when setting the set-group-ID bit on that file
214 */
215 if (mask & ATTR_MODE) {
216 mode_t m = 0;
217
218 if ((iattr->ia_mode & S_ISUID) && !file_owner)
219 m |= S_ISUID;
220 if ((iattr->ia_mode & S_ISGID) &&
221 !in_group_p((gid_t)ip->i_d.di_gid))
222 m |= S_ISGID;
223 #if 0
224 /* Linux allows this, Irix doesn't. */
225 if ((iattr->ia_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
226 m |= S_ISVTX;
227 #endif
228 if (m && !capable(CAP_FSETID))
229 iattr->ia_mode &= ~m;
230 }
231 }
232
233 /*
234 * Change file ownership. Must be the owner or privileged.
235 * If the system was configured with the "restricted_chown"
236 * option, the owner is not permitted to give away the file,
237 * and can change the group id only to a group of which he
238 * or she is a member.
239 */
240 if (mask & (ATTR_UID|ATTR_GID)) {
241 /*
242 * These IDs could have changed since we last looked at them.
243 * But, we're assured that if the ownership did change
244 * while we didn't have the inode locked, inode's dquot(s)
245 * would have changed also.
246 */
247 iuid = ip->i_d.di_uid;
248 igid = ip->i_d.di_gid;
249 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
250 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
251
252 /*
253 * CAP_CHOWN overrides the following restrictions:
254 *
255 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
256 * shall override the restriction that a process cannot
257 * change the user ID of a file it owns and the restriction
258 * that the group ID supplied to the chown() function
259 * shall be equal to either the group ID or one of the
260 * supplementary group IDs of the calling process.
261 */
262 if (restricted_chown &&
263 (iuid != uid || (igid != gid &&
264 !in_group_p((gid_t)gid))) &&
265 !capable(CAP_CHOWN)) {
266 code = XFS_ERROR(EPERM);
267 goto error_return;
268 }
269 /*
270 * Do a quota reservation only if uid/gid is actually
271 * going to change.
272 */
273 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
274 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
275 ASSERT(tp);
276 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
277 capable(CAP_FOWNER) ?
278 XFS_QMOPT_FORCE_RES : 0);
279 if (code) /* out of quota */
280 goto error_return;
281 }
282 }
283
284 /*
285 * Truncate file. Must have write permission and not be a directory.
286 */
287 if (mask & ATTR_SIZE) {
288 /* Short circuit the truncate case for zero length files */
289 if (iattr->ia_size == 0 &&
290 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
291 xfs_iunlock(ip, XFS_ILOCK_EXCL);
292 lock_flags &= ~XFS_ILOCK_EXCL;
293 if (mask & ATTR_CTIME)
294 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
295 code = 0;
296 goto error_return;
297 }
298
299 if (S_ISDIR(ip->i_d.di_mode)) {
300 code = XFS_ERROR(EISDIR);
301 goto error_return;
302 } else if (!S_ISREG(ip->i_d.di_mode)) {
303 code = XFS_ERROR(EINVAL);
304 goto error_return;
305 }
306 /*
307 * Make sure that the dquots are attached to the inode.
308 */
309 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
310 goto error_return;
311 }
312
313 /*
314 * Change file access or modified times.
315 */
316 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
317 if (!file_owner) {
318 if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
319 !capable(CAP_FOWNER)) {
320 code = XFS_ERROR(EPERM);
321 goto error_return;
322 }
323 }
324 }
325
326 /*
327 * Now we can make the changes. Before we join the inode
328 * to the transaction, if ATTR_SIZE is set then take care of
329 * the part of the truncation that must be done without the
330 * inode lock. This needs to be done before joining the inode
331 * to the transaction, because the inode cannot be unlocked
332 * once it is a part of the transaction.
333 */
334 if (mask & ATTR_SIZE) {
335 code = 0;
336 if (iattr->ia_size > ip->i_size) {
337 /*
338 * Do the first part of growing a file: zero any data
339 * in the last block that is beyond the old EOF. We
340 * need to do this before the inode is joined to the
341 * transaction to modify the i_size.
342 */
343 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
344 }
345 xfs_iunlock(ip, XFS_ILOCK_EXCL);
346
347 /*
348 * We are going to log the inode size change in this
349 * transaction so any previous writes that are beyond the on
350 * disk EOF and the new EOF that have not been written out need
351 * to be written here. If we do not write the data out, we
352 * expose ourselves to the null files problem.
353 *
354 * Only flush from the on disk size to the smaller of the in
355 * memory file size or the new size as that's the range we
356 * really care about here and prevents waiting for other data
357 * not within the range we care about here.
358 */
359 if (!code &&
360 ip->i_size != ip->i_d.di_size &&
361 iattr->ia_size > ip->i_d.di_size) {
362 code = xfs_flush_pages(ip,
363 ip->i_d.di_size, iattr->ia_size,
364 XFS_B_ASYNC, FI_NONE);
365 }
366
367 /* wait for all I/O to complete */
368 vn_iowait(ip);
369
370 if (!code)
371 code = xfs_itruncate_data(ip, iattr->ia_size);
372 if (code) {
373 ASSERT(tp == NULL);
374 lock_flags &= ~XFS_ILOCK_EXCL;
375 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
376 goto error_return;
377 }
378 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
379 if ((code = xfs_trans_reserve(tp, 0,
380 XFS_ITRUNCATE_LOG_RES(mp), 0,
381 XFS_TRANS_PERM_LOG_RES,
382 XFS_ITRUNCATE_LOG_COUNT))) {
383 xfs_trans_cancel(tp, 0);
384 if (need_iolock)
385 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
386 return code;
387 }
388 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
389 xfs_ilock(ip, XFS_ILOCK_EXCL);
390 }
391
392 if (tp) {
393 xfs_trans_ijoin(tp, ip, lock_flags);
394 xfs_trans_ihold(tp, ip);
395 }
396
397 /*
398 * Truncate file. Must have write permission and not be a directory.
399 */
400 if (mask & ATTR_SIZE) {
401 /*
402 * Only change the c/mtime if we are changing the size
403 * or we are explicitly asked to change it. This handles
404 * the semantic difference between truncate() and ftruncate()
405 * as implemented in the VFS.
406 */
407 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
408 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
409
410 if (iattr->ia_size > ip->i_size) {
411 ip->i_d.di_size = iattr->ia_size;
412 ip->i_size = iattr->ia_size;
413 if (!(flags & XFS_ATTR_DMI))
414 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
415 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
416 } else if (iattr->ia_size <= ip->i_size ||
417 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
418 /*
419 * signal a sync transaction unless
420 * we're truncating an already unlinked
421 * file on a wsync filesystem
422 */
423 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
424 XFS_DATA_FORK,
425 ((ip->i_d.di_nlink != 0 ||
426 !(mp->m_flags & XFS_MOUNT_WSYNC))
427 ? 1 : 0));
428 if (code)
429 goto abort_return;
430 /*
431 * Truncated "down", so we're removing references
432 * to old data here - if we now delay flushing for
433 * a long time, we expose ourselves unduly to the
434 * notorious NULL files problem. So, we mark this
435 * vnode and flush it when the file is closed, and
436 * do not wait the usual (long) time for writeout.
437 */
438 xfs_iflags_set(ip, XFS_ITRUNCATED);
439 }
440 }
441
442 /*
443 * Change file access modes.
444 */
445 if (mask & ATTR_MODE) {
446 ip->i_d.di_mode &= S_IFMT;
447 ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
448
449 inode->i_mode &= S_IFMT;
450 inode->i_mode |= iattr->ia_mode & ~S_IFMT;
451
452 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
453 timeflags |= XFS_ICHGTIME_CHG;
454 }
455
456 /*
457 * Change file ownership. Must be the owner or privileged.
458 * If the system was configured with the "restricted_chown"
459 * option, the owner is not permitted to give away the file,
460 * and can change the group id only to a group of which he
461 * or she is a member.
462 */
463 if (mask & (ATTR_UID|ATTR_GID)) {
464 /*
465 * CAP_FSETID overrides the following restrictions:
466 *
467 * The set-user-ID and set-group-ID bits of a file will be
468 * cleared upon successful return from chown()
469 */
470 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
471 !capable(CAP_FSETID)) {
472 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
473 }
474
475 /*
476 * Change the ownerships and register quota modifications
477 * in the transaction.
478 */
479 if (iuid != uid) {
480 if (XFS_IS_UQUOTA_ON(mp)) {
481 ASSERT(mask & ATTR_UID);
482 ASSERT(udqp);
483 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
484 &ip->i_udquot, udqp);
485 }
486 ip->i_d.di_uid = uid;
487 inode->i_uid = uid;
488 }
489 if (igid != gid) {
490 if (XFS_IS_GQUOTA_ON(mp)) {
491 ASSERT(!XFS_IS_PQUOTA_ON(mp));
492 ASSERT(mask & ATTR_GID);
493 ASSERT(gdqp);
494 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
495 &ip->i_gdquot, gdqp);
496 }
497 ip->i_d.di_gid = gid;
498 inode->i_gid = gid;
499 }
500
501 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
502 timeflags |= XFS_ICHGTIME_CHG;
503 }
504
505
506 /*
507 * Change file access or modified times.
508 */
509 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
510 if (mask & ATTR_ATIME) {
511 inode->i_atime = iattr->ia_atime;
512 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
513 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
514 ip->i_update_core = 1;
515 }
516 if (mask & ATTR_MTIME) {
517 inode->i_mtime = iattr->ia_mtime;
518 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
519 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
520 timeflags &= ~XFS_ICHGTIME_MOD;
521 timeflags |= XFS_ICHGTIME_CHG;
522 }
523 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
524 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
525 }
526
527 /*
528 * Change file inode change time only if ATTR_CTIME set
529 * AND we have been called by a DMI function.
530 */
531
532 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
533 inode->i_ctime = iattr->ia_ctime;
534 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
535 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
536 ip->i_update_core = 1;
537 timeflags &= ~XFS_ICHGTIME_CHG;
538 }
539
540 /*
541 * Send out timestamp changes that need to be set to the
542 * current time. Not done when called by a DMI function.
543 */
544 if (timeflags && !(flags & XFS_ATTR_DMI))
545 xfs_ichgtime(ip, timeflags);
546
547 XFS_STATS_INC(xs_ig_attrchg);
548
549 /*
550 * If this is a synchronous mount, make sure that the
551 * transaction goes to disk before returning to the user.
552 * This is slightly sub-optimal in that truncates require
553 * two sync transactions instead of one for wsync filesystems.
554 * One for the truncate and one for the timestamps since we
555 * don't want to change the timestamps unless we're sure the
556 * truncate worked. Truncates are less than 1% of the laddis
557 * mix so this probably isn't worth the trouble to optimize.
558 */
559 code = 0;
560 if (tp) {
561 if (mp->m_flags & XFS_MOUNT_WSYNC)
562 xfs_trans_set_sync(tp);
563
564 code = xfs_trans_commit(tp, commit_flags);
565 }
566
567 xfs_iunlock(ip, lock_flags);
568
569 /*
570 * Release any dquot(s) the inode had kept before chown.
571 */
572 XFS_QM_DQRELE(mp, olddquot1);
573 XFS_QM_DQRELE(mp, olddquot2);
574 XFS_QM_DQRELE(mp, udqp);
575 XFS_QM_DQRELE(mp, gdqp);
576
577 if (code) {
578 return code;
579 }
580
581 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
582 !(flags & XFS_ATTR_DMI)) {
583 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
584 NULL, DM_RIGHT_NULL, NULL, NULL,
585 0, 0, AT_DELAY_FLAG(flags));
586 }
587 return 0;
588
589 abort_return:
590 commit_flags |= XFS_TRANS_ABORT;
591 /* FALLTHROUGH */
592 error_return:
593 XFS_QM_DQRELE(mp, udqp);
594 XFS_QM_DQRELE(mp, gdqp);
595 if (tp) {
596 xfs_trans_cancel(tp, commit_flags);
597 }
598 if (lock_flags != 0) {
599 xfs_iunlock(ip, lock_flags);
600 }
601 return code;
602 }
603
604 /*
605 * The maximum pathlen is 1024 bytes. Since the minimum file system
606 * blocksize is 512 bytes, we can get a max of 2 extents back from
607 * bmapi.
608 */
609 #define SYMLINK_MAPS 2
610
611 STATIC int
612 xfs_readlink_bmap(
613 xfs_inode_t *ip,
614 char *link)
615 {
616 xfs_mount_t *mp = ip->i_mount;
617 int pathlen = ip->i_d.di_size;
618 int nmaps = SYMLINK_MAPS;
619 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
620 xfs_daddr_t d;
621 int byte_cnt;
622 int n;
623 xfs_buf_t *bp;
624 int error = 0;
625
626 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
627 mval, &nmaps, NULL, NULL);
628 if (error)
629 goto out;
630
631 for (n = 0; n < nmaps; n++) {
632 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
633 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
634
635 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
636 error = XFS_BUF_GETERROR(bp);
637 if (error) {
638 xfs_ioerror_alert("xfs_readlink",
639 ip->i_mount, bp, XFS_BUF_ADDR(bp));
640 xfs_buf_relse(bp);
641 goto out;
642 }
643 if (pathlen < byte_cnt)
644 byte_cnt = pathlen;
645 pathlen -= byte_cnt;
646
647 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
648 xfs_buf_relse(bp);
649 }
650
651 link[ip->i_d.di_size] = '\0';
652 error = 0;
653
654 out:
655 return error;
656 }
657
658 int
659 xfs_readlink(
660 xfs_inode_t *ip,
661 char *link)
662 {
663 xfs_mount_t *mp = ip->i_mount;
664 int pathlen;
665 int error = 0;
666
667 xfs_itrace_entry(ip);
668
669 if (XFS_FORCED_SHUTDOWN(mp))
670 return XFS_ERROR(EIO);
671
672 xfs_ilock(ip, XFS_ILOCK_SHARED);
673
674 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
675 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
676
677 pathlen = ip->i_d.di_size;
678 if (!pathlen)
679 goto out;
680
681 if (ip->i_df.if_flags & XFS_IFINLINE) {
682 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
683 link[pathlen] = '\0';
684 } else {
685 error = xfs_readlink_bmap(ip, link);
686 }
687
688 out:
689 xfs_iunlock(ip, XFS_ILOCK_SHARED);
690 return error;
691 }
692
693 /*
694 * xfs_fsync
695 *
696 * This is called to sync the inode and its data out to disk. We need to hold
697 * the I/O lock while flushing the data, and the inode lock while flushing the
698 * inode. The inode lock CANNOT be held while flushing the data, so acquire
699 * after we're done with that.
700 */
701 int
702 xfs_fsync(
703 xfs_inode_t *ip)
704 {
705 xfs_trans_t *tp;
706 int error;
707 int log_flushed = 0, changed = 1;
708
709 xfs_itrace_entry(ip);
710
711 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
712 return XFS_ERROR(EIO);
713
714 /* capture size updates in I/O completion before writing the inode. */
715 error = filemap_fdatawait(VFS_I(ip)->i_mapping);
716 if (error)
717 return XFS_ERROR(error);
718
719 /*
720 * We always need to make sure that the required inode state is safe on
721 * disk. The vnode might be clean but we still might need to force the
722 * log because of committed transactions that haven't hit the disk yet.
723 * Likewise, there could be unflushed non-transactional changes to the
724 * inode core that have to go to disk and this requires us to issue
725 * a synchronous transaction to capture these changes correctly.
726 *
727 * This code relies on the assumption that if the update_* fields
728 * of the inode are clear and the inode is unpinned then it is clean
729 * and no action is required.
730 */
731 xfs_ilock(ip, XFS_ILOCK_SHARED);
732
733 if (!(ip->i_update_size || ip->i_update_core)) {
734 /*
735 * Timestamps/size haven't changed since last inode flush or
736 * inode transaction commit. That means either nothing got
737 * written or a transaction committed which caught the updates.
738 * If the latter happened and the transaction hasn't hit the
739 * disk yet, the inode will be still be pinned. If it is,
740 * force the log.
741 */
742
743 xfs_iunlock(ip, XFS_ILOCK_SHARED);
744
745 if (xfs_ipincount(ip)) {
746 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
747 XFS_LOG_FORCE | XFS_LOG_SYNC,
748 &log_flushed);
749 } else {
750 /*
751 * If the inode is not pinned and nothing has changed
752 * we don't need to flush the cache.
753 */
754 changed = 0;
755 }
756 } else {
757 /*
758 * Kick off a transaction to log the inode core to get the
759 * updates. The sync transaction will also force the log.
760 */
761 xfs_iunlock(ip, XFS_ILOCK_SHARED);
762 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
763 error = xfs_trans_reserve(tp, 0,
764 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
765 if (error) {
766 xfs_trans_cancel(tp, 0);
767 return error;
768 }
769 xfs_ilock(ip, XFS_ILOCK_EXCL);
770
771 /*
772 * Note - it's possible that we might have pushed ourselves out
773 * of the way during trans_reserve which would flush the inode.
774 * But there's no guarantee that the inode buffer has actually
775 * gone out yet (it's delwri). Plus the buffer could be pinned
776 * anyway if it's part of an inode in another recent
777 * transaction. So we play it safe and fire off the
778 * transaction anyway.
779 */
780 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
781 xfs_trans_ihold(tp, ip);
782 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
783 xfs_trans_set_sync(tp);
784 error = _xfs_trans_commit(tp, 0, &log_flushed);
785
786 xfs_iunlock(ip, XFS_ILOCK_EXCL);
787 }
788
789 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
790 /*
791 * If the log write didn't issue an ordered tag we need
792 * to flush the disk cache for the data device now.
793 */
794 if (!log_flushed)
795 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
796
797 /*
798 * If this inode is on the RT dev we need to flush that
799 * cache as well.
800 */
801 if (XFS_IS_REALTIME_INODE(ip))
802 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
803 }
804
805 return error;
806 }
807
808 /*
809 * This is called by xfs_inactive to free any blocks beyond eof
810 * when the link count isn't zero and by xfs_dm_punch_hole() when
811 * punching a hole to EOF.
812 */
813 int
814 xfs_free_eofblocks(
815 xfs_mount_t *mp,
816 xfs_inode_t *ip,
817 int flags)
818 {
819 xfs_trans_t *tp;
820 int error;
821 xfs_fileoff_t end_fsb;
822 xfs_fileoff_t last_fsb;
823 xfs_filblks_t map_len;
824 int nimaps;
825 xfs_bmbt_irec_t imap;
826 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
827
828 /*
829 * Figure out if there are any blocks beyond the end
830 * of the file. If not, then there is nothing to do.
831 */
832 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
833 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
834 map_len = last_fsb - end_fsb;
835 if (map_len <= 0)
836 return 0;
837
838 nimaps = 1;
839 xfs_ilock(ip, XFS_ILOCK_SHARED);
840 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
841 NULL, 0, &imap, &nimaps, NULL, NULL);
842 xfs_iunlock(ip, XFS_ILOCK_SHARED);
843
844 if (!error && (nimaps != 0) &&
845 (imap.br_startblock != HOLESTARTBLOCK ||
846 ip->i_delayed_blks)) {
847 /*
848 * Attach the dquots to the inode up front.
849 */
850 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
851 return error;
852
853 /*
854 * There are blocks after the end of file.
855 * Free them up now by truncating the file to
856 * its current size.
857 */
858 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
859
860 /*
861 * Do the xfs_itruncate_start() call before
862 * reserving any log space because
863 * itruncate_start will call into the buffer
864 * cache and we can't
865 * do that within a transaction.
866 */
867 if (use_iolock)
868 xfs_ilock(ip, XFS_IOLOCK_EXCL);
869 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
870 ip->i_size);
871 if (error) {
872 xfs_trans_cancel(tp, 0);
873 if (use_iolock)
874 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
875 return error;
876 }
877
878 error = xfs_trans_reserve(tp, 0,
879 XFS_ITRUNCATE_LOG_RES(mp),
880 0, XFS_TRANS_PERM_LOG_RES,
881 XFS_ITRUNCATE_LOG_COUNT);
882 if (error) {
883 ASSERT(XFS_FORCED_SHUTDOWN(mp));
884 xfs_trans_cancel(tp, 0);
885 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
886 return error;
887 }
888
889 xfs_ilock(ip, XFS_ILOCK_EXCL);
890 xfs_trans_ijoin(tp, ip,
891 XFS_IOLOCK_EXCL |
892 XFS_ILOCK_EXCL);
893 xfs_trans_ihold(tp, ip);
894
895 error = xfs_itruncate_finish(&tp, ip,
896 ip->i_size,
897 XFS_DATA_FORK,
898 0);
899 /*
900 * If we get an error at this point we
901 * simply don't bother truncating the file.
902 */
903 if (error) {
904 xfs_trans_cancel(tp,
905 (XFS_TRANS_RELEASE_LOG_RES |
906 XFS_TRANS_ABORT));
907 } else {
908 error = xfs_trans_commit(tp,
909 XFS_TRANS_RELEASE_LOG_RES);
910 }
911 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
912 : XFS_ILOCK_EXCL));
913 }
914 return error;
915 }
916
917 /*
918 * Free a symlink that has blocks associated with it.
919 */
920 STATIC int
921 xfs_inactive_symlink_rmt(
922 xfs_inode_t *ip,
923 xfs_trans_t **tpp)
924 {
925 xfs_buf_t *bp;
926 int committed;
927 int done;
928 int error;
929 xfs_fsblock_t first_block;
930 xfs_bmap_free_t free_list;
931 int i;
932 xfs_mount_t *mp;
933 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
934 int nmaps;
935 xfs_trans_t *ntp;
936 int size;
937 xfs_trans_t *tp;
938
939 tp = *tpp;
940 mp = ip->i_mount;
941 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
942 /*
943 * We're freeing a symlink that has some
944 * blocks allocated to it. Free the
945 * blocks here. We know that we've got
946 * either 1 or 2 extents and that we can
947 * free them all in one bunmapi call.
948 */
949 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
950 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
951 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
952 ASSERT(XFS_FORCED_SHUTDOWN(mp));
953 xfs_trans_cancel(tp, 0);
954 *tpp = NULL;
955 return error;
956 }
957 /*
958 * Lock the inode, fix the size, and join it to the transaction.
959 * Hold it so in the normal path, we still have it locked for
960 * the second transaction. In the error paths we need it
961 * held so the cancel won't rele it, see below.
962 */
963 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
964 size = (int)ip->i_d.di_size;
965 ip->i_d.di_size = 0;
966 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
967 xfs_trans_ihold(tp, ip);
968 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
969 /*
970 * Find the block(s) so we can inval and unmap them.
971 */
972 done = 0;
973 XFS_BMAP_INIT(&free_list, &first_block);
974 nmaps = ARRAY_SIZE(mval);
975 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
976 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
977 &free_list, NULL)))
978 goto error0;
979 /*
980 * Invalidate the block(s).
981 */
982 for (i = 0; i < nmaps; i++) {
983 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
984 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
985 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
986 xfs_trans_binval(tp, bp);
987 }
988 /*
989 * Unmap the dead block(s) to the free_list.
990 */
991 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
992 &first_block, &free_list, NULL, &done)))
993 goto error1;
994 ASSERT(done);
995 /*
996 * Commit the first transaction. This logs the EFI and the inode.
997 */
998 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
999 goto error1;
1000 /*
1001 * The transaction must have been committed, since there were
1002 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1003 * The new tp has the extent freeing and EFDs.
1004 */
1005 ASSERT(committed);
1006 /*
1007 * The first xact was committed, so add the inode to the new one.
1008 * Mark it dirty so it will be logged and moved forward in the log as
1009 * part of every commit.
1010 */
1011 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1012 xfs_trans_ihold(tp, ip);
1013 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1014 /*
1015 * Get a new, empty transaction to return to our caller.
1016 */
1017 ntp = xfs_trans_dup(tp);
1018 /*
1019 * Commit the transaction containing extent freeing and EFDs.
1020 * If we get an error on the commit here or on the reserve below,
1021 * we need to unlock the inode since the new transaction doesn't
1022 * have the inode attached.
1023 */
1024 error = xfs_trans_commit(tp, 0);
1025 tp = ntp;
1026 if (error) {
1027 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1028 goto error0;
1029 }
1030 /*
1031 * Remove the memory for extent descriptions (just bookkeeping).
1032 */
1033 if (ip->i_df.if_bytes)
1034 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1035 ASSERT(ip->i_df.if_bytes == 0);
1036 /*
1037 * Put an itruncate log reservation in the new transaction
1038 * for our caller.
1039 */
1040 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1041 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1042 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1043 goto error0;
1044 }
1045 /*
1046 * Return with the inode locked but not joined to the transaction.
1047 */
1048 *tpp = tp;
1049 return 0;
1050
1051 error1:
1052 xfs_bmap_cancel(&free_list);
1053 error0:
1054 /*
1055 * Have to come here with the inode locked and either
1056 * (held and in the transaction) or (not in the transaction).
1057 * If the inode isn't held then cancel would iput it, but
1058 * that's wrong since this is inactive and the vnode ref
1059 * count is 0 already.
1060 * Cancel won't do anything to the inode if held, but it still
1061 * needs to be locked until the cancel is done, if it was
1062 * joined to the transaction.
1063 */
1064 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1065 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1066 *tpp = NULL;
1067 return error;
1068
1069 }
1070
1071 STATIC int
1072 xfs_inactive_symlink_local(
1073 xfs_inode_t *ip,
1074 xfs_trans_t **tpp)
1075 {
1076 int error;
1077
1078 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1079 /*
1080 * We're freeing a symlink which fit into
1081 * the inode. Just free the memory used
1082 * to hold the old symlink.
1083 */
1084 error = xfs_trans_reserve(*tpp, 0,
1085 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1086 0, XFS_TRANS_PERM_LOG_RES,
1087 XFS_ITRUNCATE_LOG_COUNT);
1088
1089 if (error) {
1090 xfs_trans_cancel(*tpp, 0);
1091 *tpp = NULL;
1092 return error;
1093 }
1094 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1095
1096 /*
1097 * Zero length symlinks _can_ exist.
1098 */
1099 if (ip->i_df.if_bytes > 0) {
1100 xfs_idata_realloc(ip,
1101 -(ip->i_df.if_bytes),
1102 XFS_DATA_FORK);
1103 ASSERT(ip->i_df.if_bytes == 0);
1104 }
1105 return 0;
1106 }
1107
1108 STATIC int
1109 xfs_inactive_attrs(
1110 xfs_inode_t *ip,
1111 xfs_trans_t **tpp)
1112 {
1113 xfs_trans_t *tp;
1114 int error;
1115 xfs_mount_t *mp;
1116
1117 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1118 tp = *tpp;
1119 mp = ip->i_mount;
1120 ASSERT(ip->i_d.di_forkoff != 0);
1121 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1122 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1123 if (error)
1124 goto error_unlock;
1125
1126 error = xfs_attr_inactive(ip);
1127 if (error)
1128 goto error_unlock;
1129
1130 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1131 error = xfs_trans_reserve(tp, 0,
1132 XFS_IFREE_LOG_RES(mp),
1133 0, XFS_TRANS_PERM_LOG_RES,
1134 XFS_INACTIVE_LOG_COUNT);
1135 if (error)
1136 goto error_cancel;
1137
1138 xfs_ilock(ip, XFS_ILOCK_EXCL);
1139 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1140 xfs_trans_ihold(tp, ip);
1141 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1142
1143 ASSERT(ip->i_d.di_anextents == 0);
1144
1145 *tpp = tp;
1146 return 0;
1147
1148 error_cancel:
1149 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1150 xfs_trans_cancel(tp, 0);
1151 error_unlock:
1152 *tpp = NULL;
1153 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1154 return error;
1155 }
1156
1157 int
1158 xfs_release(
1159 xfs_inode_t *ip)
1160 {
1161 xfs_mount_t *mp = ip->i_mount;
1162 int error;
1163
1164 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1165 return 0;
1166
1167 /* If this is a read-only mount, don't do this (would generate I/O) */
1168 if (mp->m_flags & XFS_MOUNT_RDONLY)
1169 return 0;
1170
1171 if (!XFS_FORCED_SHUTDOWN(mp)) {
1172 int truncated;
1173
1174 /*
1175 * If we are using filestreams, and we have an unlinked
1176 * file that we are processing the last close on, then nothing
1177 * will be able to reopen and write to this file. Purge this
1178 * inode from the filestreams cache so that it doesn't delay
1179 * teardown of the inode.
1180 */
1181 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1182 xfs_filestream_deassociate(ip);
1183
1184 /*
1185 * If we previously truncated this file and removed old data
1186 * in the process, we want to initiate "early" writeout on
1187 * the last close. This is an attempt to combat the notorious
1188 * NULL files problem which is particularly noticable from a
1189 * truncate down, buffered (re-)write (delalloc), followed by
1190 * a crash. What we are effectively doing here is
1191 * significantly reducing the time window where we'd otherwise
1192 * be exposed to that problem.
1193 */
1194 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1195 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
1196 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1197 }
1198
1199 if (ip->i_d.di_nlink != 0) {
1200 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1201 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1202 ip->i_delayed_blks > 0)) &&
1203 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1204 (!(ip->i_d.di_flags &
1205 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1206 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1207 if (error)
1208 return error;
1209 }
1210 }
1211
1212 return 0;
1213 }
1214
1215 /*
1216 * xfs_inactive
1217 *
1218 * This is called when the vnode reference count for the vnode
1219 * goes to zero. If the file has been unlinked, then it must
1220 * now be truncated. Also, we clear all of the read-ahead state
1221 * kept for the inode here since the file is now closed.
1222 */
1223 int
1224 xfs_inactive(
1225 xfs_inode_t *ip)
1226 {
1227 xfs_bmap_free_t free_list;
1228 xfs_fsblock_t first_block;
1229 int committed;
1230 xfs_trans_t *tp;
1231 xfs_mount_t *mp;
1232 int error;
1233 int truncate;
1234
1235 xfs_itrace_entry(ip);
1236
1237 /*
1238 * If the inode is already free, then there can be nothing
1239 * to clean up here.
1240 */
1241 if (ip->i_d.di_mode == 0 || VN_BAD(VFS_I(ip))) {
1242 ASSERT(ip->i_df.if_real_bytes == 0);
1243 ASSERT(ip->i_df.if_broot_bytes == 0);
1244 return VN_INACTIVE_CACHE;
1245 }
1246
1247 /*
1248 * Only do a truncate if it's a regular file with
1249 * some actual space in it. It's OK to look at the
1250 * inode's fields without the lock because we're the
1251 * only one with a reference to the inode.
1252 */
1253 truncate = ((ip->i_d.di_nlink == 0) &&
1254 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1255 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1256 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1257
1258 mp = ip->i_mount;
1259
1260 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1261 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1262
1263 error = 0;
1264
1265 /* If this is a read-only mount, don't do this (would generate I/O) */
1266 if (mp->m_flags & XFS_MOUNT_RDONLY)
1267 goto out;
1268
1269 if (ip->i_d.di_nlink != 0) {
1270 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1271 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1272 ip->i_delayed_blks > 0)) &&
1273 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1274 (!(ip->i_d.di_flags &
1275 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1276 (ip->i_delayed_blks != 0)))) {
1277 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1278 if (error)
1279 return VN_INACTIVE_CACHE;
1280 }
1281 goto out;
1282 }
1283
1284 ASSERT(ip->i_d.di_nlink == 0);
1285
1286 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1287 return VN_INACTIVE_CACHE;
1288
1289 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1290 if (truncate) {
1291 /*
1292 * Do the xfs_itruncate_start() call before
1293 * reserving any log space because itruncate_start
1294 * will call into the buffer cache and we can't
1295 * do that within a transaction.
1296 */
1297 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1298
1299 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1300 if (error) {
1301 xfs_trans_cancel(tp, 0);
1302 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1303 return VN_INACTIVE_CACHE;
1304 }
1305
1306 error = xfs_trans_reserve(tp, 0,
1307 XFS_ITRUNCATE_LOG_RES(mp),
1308 0, XFS_TRANS_PERM_LOG_RES,
1309 XFS_ITRUNCATE_LOG_COUNT);
1310 if (error) {
1311 /* Don't call itruncate_cleanup */
1312 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1313 xfs_trans_cancel(tp, 0);
1314 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1315 return VN_INACTIVE_CACHE;
1316 }
1317
1318 xfs_ilock(ip, XFS_ILOCK_EXCL);
1319 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1320 xfs_trans_ihold(tp, ip);
1321
1322 /*
1323 * normally, we have to run xfs_itruncate_finish sync.
1324 * But if filesystem is wsync and we're in the inactive
1325 * path, then we know that nlink == 0, and that the
1326 * xaction that made nlink == 0 is permanently committed
1327 * since xfs_remove runs as a synchronous transaction.
1328 */
1329 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1330 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1331
1332 if (error) {
1333 xfs_trans_cancel(tp,
1334 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1335 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1336 return VN_INACTIVE_CACHE;
1337 }
1338 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1339
1340 /*
1341 * If we get an error while cleaning up a
1342 * symlink we bail out.
1343 */
1344 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1345 xfs_inactive_symlink_rmt(ip, &tp) :
1346 xfs_inactive_symlink_local(ip, &tp);
1347
1348 if (error) {
1349 ASSERT(tp == NULL);
1350 return VN_INACTIVE_CACHE;
1351 }
1352
1353 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1354 xfs_trans_ihold(tp, ip);
1355 } else {
1356 error = xfs_trans_reserve(tp, 0,
1357 XFS_IFREE_LOG_RES(mp),
1358 0, XFS_TRANS_PERM_LOG_RES,
1359 XFS_INACTIVE_LOG_COUNT);
1360 if (error) {
1361 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1362 xfs_trans_cancel(tp, 0);
1363 return VN_INACTIVE_CACHE;
1364 }
1365
1366 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1367 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1368 xfs_trans_ihold(tp, ip);
1369 }
1370
1371 /*
1372 * If there are attributes associated with the file
1373 * then blow them away now. The code calls a routine
1374 * that recursively deconstructs the attribute fork.
1375 * We need to just commit the current transaction
1376 * because we can't use it for xfs_attr_inactive().
1377 */
1378 if (ip->i_d.di_anextents > 0) {
1379 error = xfs_inactive_attrs(ip, &tp);
1380 /*
1381 * If we got an error, the transaction is already
1382 * cancelled, and the inode is unlocked. Just get out.
1383 */
1384 if (error)
1385 return VN_INACTIVE_CACHE;
1386 } else if (ip->i_afp) {
1387 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1388 }
1389
1390 /*
1391 * Free the inode.
1392 */
1393 XFS_BMAP_INIT(&free_list, &first_block);
1394 error = xfs_ifree(tp, ip, &free_list);
1395 if (error) {
1396 /*
1397 * If we fail to free the inode, shut down. The cancel
1398 * might do that, we need to make sure. Otherwise the
1399 * inode might be lost for a long time or forever.
1400 */
1401 if (!XFS_FORCED_SHUTDOWN(mp)) {
1402 cmn_err(CE_NOTE,
1403 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1404 error, mp->m_fsname);
1405 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1406 }
1407 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1408 } else {
1409 /*
1410 * Credit the quota account(s). The inode is gone.
1411 */
1412 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1413
1414 /*
1415 * Just ignore errors at this point. There is nothing we can
1416 * do except to try to keep going. Make sure it's not a silent
1417 * error.
1418 */
1419 error = xfs_bmap_finish(&tp, &free_list, &committed);
1420 if (error)
1421 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1422 "xfs_bmap_finish() returned error %d", error);
1423 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1424 if (error)
1425 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1426 "xfs_trans_commit() returned error %d", error);
1427 }
1428 /*
1429 * Release the dquots held by inode, if any.
1430 */
1431 XFS_QM_DQDETACH(mp, ip);
1432
1433 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1434
1435 out:
1436 return VN_INACTIVE_CACHE;
1437 }
1438
1439 /*
1440 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1441 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1442 * ci_name->name will point to a the actual name (caller must free) or
1443 * will be set to NULL if an exact match is found.
1444 */
1445 int
1446 xfs_lookup(
1447 xfs_inode_t *dp,
1448 struct xfs_name *name,
1449 xfs_inode_t **ipp,
1450 struct xfs_name *ci_name)
1451 {
1452 xfs_ino_t inum;
1453 int error;
1454 uint lock_mode;
1455
1456 xfs_itrace_entry(dp);
1457
1458 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1459 return XFS_ERROR(EIO);
1460
1461 lock_mode = xfs_ilock_map_shared(dp);
1462 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1463 xfs_iunlock_map_shared(dp, lock_mode);
1464
1465 if (error)
1466 goto out;
1467
1468 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1469 if (error)
1470 goto out_free_name;
1471
1472 xfs_itrace_ref(*ipp);
1473 return 0;
1474
1475 out_free_name:
1476 if (ci_name)
1477 kmem_free(ci_name->name);
1478 out:
1479 *ipp = NULL;
1480 return error;
1481 }
1482
1483 int
1484 xfs_create(
1485 xfs_inode_t *dp,
1486 struct xfs_name *name,
1487 mode_t mode,
1488 xfs_dev_t rdev,
1489 xfs_inode_t **ipp,
1490 cred_t *credp)
1491 {
1492 xfs_mount_t *mp = dp->i_mount;
1493 xfs_inode_t *ip;
1494 xfs_trans_t *tp;
1495 int error;
1496 xfs_bmap_free_t free_list;
1497 xfs_fsblock_t first_block;
1498 boolean_t unlock_dp_on_error = B_FALSE;
1499 int dm_event_sent = 0;
1500 uint cancel_flags;
1501 int committed;
1502 xfs_prid_t prid;
1503 struct xfs_dquot *udqp, *gdqp;
1504 uint resblks;
1505
1506 ASSERT(!*ipp);
1507 xfs_itrace_entry(dp);
1508
1509 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1510 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1511 dp, DM_RIGHT_NULL, NULL,
1512 DM_RIGHT_NULL, name->name, NULL,
1513 mode, 0, 0);
1514
1515 if (error)
1516 return error;
1517 dm_event_sent = 1;
1518 }
1519
1520 if (XFS_FORCED_SHUTDOWN(mp))
1521 return XFS_ERROR(EIO);
1522
1523 /* Return through std_return after this point. */
1524
1525 udqp = gdqp = NULL;
1526 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1527 prid = dp->i_d.di_projid;
1528 else
1529 prid = (xfs_prid_t)dfltprid;
1530
1531 /*
1532 * Make sure that we have allocated dquot(s) on disk.
1533 */
1534 error = XFS_QM_DQVOPALLOC(mp, dp,
1535 current_fsuid(), current_fsgid(), prid,
1536 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1537 if (error)
1538 goto std_return;
1539
1540 ip = NULL;
1541
1542 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1543 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1544 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1545 /*
1546 * Initially assume that the file does not exist and
1547 * reserve the resources for that case. If that is not
1548 * the case we'll drop the one we have and get a more
1549 * appropriate transaction later.
1550 */
1551 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1552 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1553 if (error == ENOSPC) {
1554 resblks = 0;
1555 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1556 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1557 }
1558 if (error) {
1559 cancel_flags = 0;
1560 goto error_return;
1561 }
1562
1563 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1564 unlock_dp_on_error = B_TRUE;
1565
1566 XFS_BMAP_INIT(&free_list, &first_block);
1567
1568 ASSERT(ip == NULL);
1569
1570 /*
1571 * Reserve disk quota and the inode.
1572 */
1573 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1574 if (error)
1575 goto error_return;
1576
1577 error = xfs_dir_canenter(tp, dp, name, resblks);
1578 if (error)
1579 goto error_return;
1580 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1581 rdev, credp, prid, resblks > 0,
1582 &ip, &committed);
1583 if (error) {
1584 if (error == ENOSPC)
1585 goto error_return;
1586 goto abort_return;
1587 }
1588 xfs_itrace_ref(ip);
1589
1590 /*
1591 * At this point, we've gotten a newly allocated inode.
1592 * It is locked (and joined to the transaction).
1593 */
1594
1595 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1596
1597 /*
1598 * Now we join the directory inode to the transaction. We do not do it
1599 * earlier because xfs_dir_ialloc might commit the previous transaction
1600 * (and release all the locks). An error from here on will result in
1601 * the transaction cancel unlocking dp so don't do it explicitly in the
1602 * error path.
1603 */
1604 IHOLD(dp);
1605 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1606 unlock_dp_on_error = B_FALSE;
1607
1608 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1609 &first_block, &free_list, resblks ?
1610 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1611 if (error) {
1612 ASSERT(error != ENOSPC);
1613 goto abort_return;
1614 }
1615 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1616 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1617
1618 /*
1619 * If this is a synchronous mount, make sure that the
1620 * create transaction goes to disk before returning to
1621 * the user.
1622 */
1623 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1624 xfs_trans_set_sync(tp);
1625 }
1626
1627 dp->i_gen++;
1628
1629 /*
1630 * Attach the dquot(s) to the inodes and modify them incore.
1631 * These ids of the inode couldn't have changed since the new
1632 * inode has been locked ever since it was created.
1633 */
1634 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1635
1636 /*
1637 * xfs_trans_commit normally decrements the vnode ref count
1638 * when it unlocks the inode. Since we want to return the
1639 * vnode to the caller, we bump the vnode ref count now.
1640 */
1641 IHOLD(ip);
1642
1643 error = xfs_bmap_finish(&tp, &free_list, &committed);
1644 if (error) {
1645 xfs_bmap_cancel(&free_list);
1646 goto abort_rele;
1647 }
1648
1649 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1650 if (error) {
1651 IRELE(ip);
1652 tp = NULL;
1653 goto error_return;
1654 }
1655
1656 XFS_QM_DQRELE(mp, udqp);
1657 XFS_QM_DQRELE(mp, gdqp);
1658
1659 *ipp = ip;
1660
1661 /* Fallthrough to std_return with error = 0 */
1662
1663 std_return:
1664 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
1665 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1666 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
1667 dp, DM_RIGHT_NULL,
1668 *ipp ? ip : NULL,
1669 DM_RIGHT_NULL, name->name, NULL,
1670 mode, error, 0);
1671 }
1672 return error;
1673
1674 abort_return:
1675 cancel_flags |= XFS_TRANS_ABORT;
1676 /* FALLTHROUGH */
1677
1678 error_return:
1679 if (tp != NULL)
1680 xfs_trans_cancel(tp, cancel_flags);
1681
1682 XFS_QM_DQRELE(mp, udqp);
1683 XFS_QM_DQRELE(mp, gdqp);
1684
1685 if (unlock_dp_on_error)
1686 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1687
1688 goto std_return;
1689
1690 abort_rele:
1691 /*
1692 * Wait until after the current transaction is aborted to
1693 * release the inode. This prevents recursive transactions
1694 * and deadlocks from xfs_inactive.
1695 */
1696 cancel_flags |= XFS_TRANS_ABORT;
1697 xfs_trans_cancel(tp, cancel_flags);
1698 IRELE(ip);
1699
1700 XFS_QM_DQRELE(mp, udqp);
1701 XFS_QM_DQRELE(mp, gdqp);
1702
1703 goto std_return;
1704 }
1705
1706 #ifdef DEBUG
1707 int xfs_locked_n;
1708 int xfs_small_retries;
1709 int xfs_middle_retries;
1710 int xfs_lots_retries;
1711 int xfs_lock_delays;
1712 #endif
1713
1714 /*
1715 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1716 * a different value
1717 */
1718 static inline int
1719 xfs_lock_inumorder(int lock_mode, int subclass)
1720 {
1721 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1722 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1723 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1724 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1725
1726 return lock_mode;
1727 }
1728
1729 /*
1730 * The following routine will lock n inodes in exclusive mode.
1731 * We assume the caller calls us with the inodes in i_ino order.
1732 *
1733 * We need to detect deadlock where an inode that we lock
1734 * is in the AIL and we start waiting for another inode that is locked
1735 * by a thread in a long running transaction (such as truncate). This can
1736 * result in deadlock since the long running trans might need to wait
1737 * for the inode we just locked in order to push the tail and free space
1738 * in the log.
1739 */
1740 void
1741 xfs_lock_inodes(
1742 xfs_inode_t **ips,
1743 int inodes,
1744 uint lock_mode)
1745 {
1746 int attempts = 0, i, j, try_lock;
1747 xfs_log_item_t *lp;
1748
1749 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1750
1751 try_lock = 0;
1752 i = 0;
1753
1754 again:
1755 for (; i < inodes; i++) {
1756 ASSERT(ips[i]);
1757
1758 if (i && (ips[i] == ips[i-1])) /* Already locked */
1759 continue;
1760
1761 /*
1762 * If try_lock is not set yet, make sure all locked inodes
1763 * are not in the AIL.
1764 * If any are, set try_lock to be used later.
1765 */
1766
1767 if (!try_lock) {
1768 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1769 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1770 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1771 try_lock++;
1772 }
1773 }
1774 }
1775
1776 /*
1777 * If any of the previous locks we have locked is in the AIL,
1778 * we must TRY to get the second and subsequent locks. If
1779 * we can't get any, we must release all we have
1780 * and try again.
1781 */
1782
1783 if (try_lock) {
1784 /* try_lock must be 0 if i is 0. */
1785 /*
1786 * try_lock means we have an inode locked
1787 * that is in the AIL.
1788 */
1789 ASSERT(i != 0);
1790 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1791 attempts++;
1792
1793 /*
1794 * Unlock all previous guys and try again.
1795 * xfs_iunlock will try to push the tail
1796 * if the inode is in the AIL.
1797 */
1798
1799 for(j = i - 1; j >= 0; j--) {
1800
1801 /*
1802 * Check to see if we've already
1803 * unlocked this one.
1804 * Not the first one going back,
1805 * and the inode ptr is the same.
1806 */
1807 if ((j != (i - 1)) && ips[j] ==
1808 ips[j+1])
1809 continue;
1810
1811 xfs_iunlock(ips[j], lock_mode);
1812 }
1813
1814 if ((attempts % 5) == 0) {
1815 delay(1); /* Don't just spin the CPU */
1816 #ifdef DEBUG
1817 xfs_lock_delays++;
1818 #endif
1819 }
1820 i = 0;
1821 try_lock = 0;
1822 goto again;
1823 }
1824 } else {
1825 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1826 }
1827 }
1828
1829 #ifdef DEBUG
1830 if (attempts) {
1831 if (attempts < 5) xfs_small_retries++;
1832 else if (attempts < 100) xfs_middle_retries++;
1833 else xfs_lots_retries++;
1834 } else {
1835 xfs_locked_n++;
1836 }
1837 #endif
1838 }
1839
1840 /*
1841 * xfs_lock_two_inodes() can only be used to lock one type of lock
1842 * at a time - the iolock or the ilock, but not both at once. If
1843 * we lock both at once, lockdep will report false positives saying
1844 * we have violated locking orders.
1845 */
1846 void
1847 xfs_lock_two_inodes(
1848 xfs_inode_t *ip0,
1849 xfs_inode_t *ip1,
1850 uint lock_mode)
1851 {
1852 xfs_inode_t *temp;
1853 int attempts = 0;
1854 xfs_log_item_t *lp;
1855
1856 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1857 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1858 ASSERT(ip0->i_ino != ip1->i_ino);
1859
1860 if (ip0->i_ino > ip1->i_ino) {
1861 temp = ip0;
1862 ip0 = ip1;
1863 ip1 = temp;
1864 }
1865
1866 again:
1867 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1868
1869 /*
1870 * If the first lock we have locked is in the AIL, we must TRY to get
1871 * the second lock. If we can't get it, we must release the first one
1872 * and try again.
1873 */
1874 lp = (xfs_log_item_t *)ip0->i_itemp;
1875 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1876 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1877 xfs_iunlock(ip0, lock_mode);
1878 if ((++attempts % 5) == 0)
1879 delay(1); /* Don't just spin the CPU */
1880 goto again;
1881 }
1882 } else {
1883 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1884 }
1885 }
1886
1887 int
1888 xfs_remove(
1889 xfs_inode_t *dp,
1890 struct xfs_name *name,
1891 xfs_inode_t *ip)
1892 {
1893 xfs_mount_t *mp = dp->i_mount;
1894 xfs_trans_t *tp = NULL;
1895 int is_dir = S_ISDIR(ip->i_d.di_mode);
1896 int error = 0;
1897 xfs_bmap_free_t free_list;
1898 xfs_fsblock_t first_block;
1899 int cancel_flags;
1900 int committed;
1901 int link_zero;
1902 uint resblks;
1903 uint log_count;
1904
1905 xfs_itrace_entry(dp);
1906 xfs_itrace_entry(ip);
1907
1908 if (XFS_FORCED_SHUTDOWN(mp))
1909 return XFS_ERROR(EIO);
1910
1911 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1912 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1913 NULL, DM_RIGHT_NULL, name->name, NULL,
1914 ip->i_d.di_mode, 0, 0);
1915 if (error)
1916 return error;
1917 }
1918
1919 error = XFS_QM_DQATTACH(mp, dp, 0);
1920 if (error)
1921 goto std_return;
1922
1923 error = XFS_QM_DQATTACH(mp, ip, 0);
1924 if (error)
1925 goto std_return;
1926
1927 if (is_dir) {
1928 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1929 log_count = XFS_DEFAULT_LOG_COUNT;
1930 } else {
1931 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1932 log_count = XFS_REMOVE_LOG_COUNT;
1933 }
1934 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1935
1936 /*
1937 * We try to get the real space reservation first,
1938 * allowing for directory btree deletion(s) implying
1939 * possible bmap insert(s). If we can't get the space
1940 * reservation then we use 0 instead, and avoid the bmap
1941 * btree insert(s) in the directory code by, if the bmap
1942 * insert tries to happen, instead trimming the LAST
1943 * block from the directory.
1944 */
1945 resblks = XFS_REMOVE_SPACE_RES(mp);
1946 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1947 XFS_TRANS_PERM_LOG_RES, log_count);
1948 if (error == ENOSPC) {
1949 resblks = 0;
1950 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1951 XFS_TRANS_PERM_LOG_RES, log_count);
1952 }
1953 if (error) {
1954 ASSERT(error != ENOSPC);
1955 cancel_flags = 0;
1956 goto out_trans_cancel;
1957 }
1958
1959 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1960
1961 /*
1962 * At this point, we've gotten both the directory and the entry
1963 * inodes locked.
1964 */
1965 IHOLD(ip);
1966 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1967
1968 IHOLD(dp);
1969 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1970
1971 /*
1972 * If we're removing a directory perform some additional validation.
1973 */
1974 if (is_dir) {
1975 ASSERT(ip->i_d.di_nlink >= 2);
1976 if (ip->i_d.di_nlink != 2) {
1977 error = XFS_ERROR(ENOTEMPTY);
1978 goto out_trans_cancel;
1979 }
1980 if (!xfs_dir_isempty(ip)) {
1981 error = XFS_ERROR(ENOTEMPTY);
1982 goto out_trans_cancel;
1983 }
1984 }
1985
1986 XFS_BMAP_INIT(&free_list, &first_block);
1987 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1988 &first_block, &free_list, resblks);
1989 if (error) {
1990 ASSERT(error != ENOENT);
1991 goto out_bmap_cancel;
1992 }
1993 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1994
1995 /*
1996 * Bump the in memory generation count on the parent
1997 * directory so that other can know that it has changed.
1998 */
1999 dp->i_gen++;
2000 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2001
2002 if (is_dir) {
2003 /*
2004 * Drop the link from ip's "..".
2005 */
2006 error = xfs_droplink(tp, dp);
2007 if (error)
2008 goto out_bmap_cancel;
2009
2010 /*
2011 * Drop the "." link from ip to self.
2012 */
2013 error = xfs_droplink(tp, ip);
2014 if (error)
2015 goto out_bmap_cancel;
2016 } else {
2017 /*
2018 * When removing a non-directory we need to log the parent
2019 * inode here for the i_gen update. For a directory this is
2020 * done implicitly by the xfs_droplink call for the ".." entry.
2021 */
2022 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2023 }
2024
2025 /*
2026 * Drop the link from dp to ip.
2027 */
2028 error = xfs_droplink(tp, ip);
2029 if (error)
2030 goto out_bmap_cancel;
2031
2032 /*
2033 * Determine if this is the last link while
2034 * we are in the transaction.
2035 */
2036 link_zero = (ip->i_d.di_nlink == 0);
2037
2038 /*
2039 * If this is a synchronous mount, make sure that the
2040 * remove transaction goes to disk before returning to
2041 * the user.
2042 */
2043 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2044 xfs_trans_set_sync(tp);
2045
2046 error = xfs_bmap_finish(&tp, &free_list, &committed);
2047 if (error)
2048 goto out_bmap_cancel;
2049
2050 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2051 if (error)
2052 goto std_return;
2053
2054 /*
2055 * If we are using filestreams, kill the stream association.
2056 * If the file is still open it may get a new one but that
2057 * will get killed on last close in xfs_close() so we don't
2058 * have to worry about that.
2059 */
2060 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2061 xfs_filestream_deassociate(ip);
2062
2063 xfs_itrace_exit(ip);
2064 xfs_itrace_exit(dp);
2065
2066 std_return:
2067 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2068 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2069 NULL, DM_RIGHT_NULL, name->name, NULL,
2070 ip->i_d.di_mode, error, 0);
2071 }
2072
2073 return error;
2074
2075 out_bmap_cancel:
2076 xfs_bmap_cancel(&free_list);
2077 cancel_flags |= XFS_TRANS_ABORT;
2078 out_trans_cancel:
2079 xfs_trans_cancel(tp, cancel_flags);
2080 goto std_return;
2081 }
2082
2083 int
2084 xfs_link(
2085 xfs_inode_t *tdp,
2086 xfs_inode_t *sip,
2087 struct xfs_name *target_name)
2088 {
2089 xfs_mount_t *mp = tdp->i_mount;
2090 xfs_trans_t *tp;
2091 int error;
2092 xfs_bmap_free_t free_list;
2093 xfs_fsblock_t first_block;
2094 int cancel_flags;
2095 int committed;
2096 int resblks;
2097
2098 xfs_itrace_entry(tdp);
2099 xfs_itrace_entry(sip);
2100
2101 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2102
2103 if (XFS_FORCED_SHUTDOWN(mp))
2104 return XFS_ERROR(EIO);
2105
2106 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2107 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2108 tdp, DM_RIGHT_NULL,
2109 sip, DM_RIGHT_NULL,
2110 target_name->name, NULL, 0, 0, 0);
2111 if (error)
2112 return error;
2113 }
2114
2115 /* Return through std_return after this point. */
2116
2117 error = XFS_QM_DQATTACH(mp, sip, 0);
2118 if (!error && sip != tdp)
2119 error = XFS_QM_DQATTACH(mp, tdp, 0);
2120 if (error)
2121 goto std_return;
2122
2123 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2124 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2125 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2126 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2127 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2128 if (error == ENOSPC) {
2129 resblks = 0;
2130 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2131 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2132 }
2133 if (error) {
2134 cancel_flags = 0;
2135 goto error_return;
2136 }
2137
2138 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
2139
2140 /*
2141 * Increment vnode ref counts since xfs_trans_commit &
2142 * xfs_trans_cancel will both unlock the inodes and
2143 * decrement the associated ref counts.
2144 */
2145 IHOLD(sip);
2146 IHOLD(tdp);
2147 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2148 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2149
2150 /*
2151 * If the source has too many links, we can't make any more to it.
2152 */
2153 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2154 error = XFS_ERROR(EMLINK);
2155 goto error_return;
2156 }
2157
2158 /*
2159 * If we are using project inheritance, we only allow hard link
2160 * creation in our tree when the project IDs are the same; else
2161 * the tree quota mechanism could be circumvented.
2162 */
2163 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2164 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2165 error = XFS_ERROR(EXDEV);
2166 goto error_return;
2167 }
2168
2169 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2170 if (error)
2171 goto error_return;
2172
2173 XFS_BMAP_INIT(&free_list, &first_block);
2174
2175 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2176 &first_block, &free_list, resblks);
2177 if (error)
2178 goto abort_return;
2179 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2180 tdp->i_gen++;
2181 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2182
2183 error = xfs_bumplink(tp, sip);
2184 if (error)
2185 goto abort_return;
2186
2187 /*
2188 * If this is a synchronous mount, make sure that the
2189 * link transaction goes to disk before returning to
2190 * the user.
2191 */
2192 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2193 xfs_trans_set_sync(tp);
2194 }
2195
2196 error = xfs_bmap_finish (&tp, &free_list, &committed);
2197 if (error) {
2198 xfs_bmap_cancel(&free_list);
2199 goto abort_return;
2200 }
2201
2202 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2203 if (error)
2204 goto std_return;
2205
2206 /* Fall through to std_return with error = 0. */
2207 std_return:
2208 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2209 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2210 tdp, DM_RIGHT_NULL,
2211 sip, DM_RIGHT_NULL,
2212 target_name->name, NULL, 0, error, 0);
2213 }
2214 return error;
2215
2216 abort_return:
2217 cancel_flags |= XFS_TRANS_ABORT;
2218 /* FALLTHROUGH */
2219
2220 error_return:
2221 xfs_trans_cancel(tp, cancel_flags);
2222 goto std_return;
2223 }
2224
2225
2226 int
2227 xfs_mkdir(
2228 xfs_inode_t *dp,
2229 struct xfs_name *dir_name,
2230 mode_t mode,
2231 xfs_inode_t **ipp,
2232 cred_t *credp)
2233 {
2234 xfs_mount_t *mp = dp->i_mount;
2235 xfs_inode_t *cdp; /* inode of created dir */
2236 xfs_trans_t *tp;
2237 int cancel_flags;
2238 int error;
2239 int committed;
2240 xfs_bmap_free_t free_list;
2241 xfs_fsblock_t first_block;
2242 boolean_t unlock_dp_on_error = B_FALSE;
2243 boolean_t created = B_FALSE;
2244 int dm_event_sent = 0;
2245 xfs_prid_t prid;
2246 struct xfs_dquot *udqp, *gdqp;
2247 uint resblks;
2248
2249 if (XFS_FORCED_SHUTDOWN(mp))
2250 return XFS_ERROR(EIO);
2251
2252 tp = NULL;
2253
2254 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2255 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2256 dp, DM_RIGHT_NULL, NULL,
2257 DM_RIGHT_NULL, dir_name->name, NULL,
2258 mode, 0, 0);
2259 if (error)
2260 return error;
2261 dm_event_sent = 1;
2262 }
2263
2264 /* Return through std_return after this point. */
2265
2266 xfs_itrace_entry(dp);
2267
2268 mp = dp->i_mount;
2269 udqp = gdqp = NULL;
2270 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2271 prid = dp->i_d.di_projid;
2272 else
2273 prid = (xfs_prid_t)dfltprid;
2274
2275 /*
2276 * Make sure that we have allocated dquot(s) on disk.
2277 */
2278 error = XFS_QM_DQVOPALLOC(mp, dp,
2279 current_fsuid(), current_fsgid(), prid,
2280 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2281 if (error)
2282 goto std_return;
2283
2284 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2285 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2286 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
2287 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2288 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2289 if (error == ENOSPC) {
2290 resblks = 0;
2291 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2292 XFS_TRANS_PERM_LOG_RES,
2293 XFS_MKDIR_LOG_COUNT);
2294 }
2295 if (error) {
2296 cancel_flags = 0;
2297 goto error_return;
2298 }
2299
2300 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2301 unlock_dp_on_error = B_TRUE;
2302
2303 /*
2304 * Check for directory link count overflow.
2305 */
2306 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2307 error = XFS_ERROR(EMLINK);
2308 goto error_return;
2309 }
2310
2311 /*
2312 * Reserve disk quota and the inode.
2313 */
2314 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2315 if (error)
2316 goto error_return;
2317
2318 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2319 if (error)
2320 goto error_return;
2321 /*
2322 * create the directory inode.
2323 */
2324 error = xfs_dir_ialloc(&tp, dp, mode, 2,
2325 0, credp, prid, resblks > 0,
2326 &cdp, NULL);
2327 if (error) {
2328 if (error == ENOSPC)
2329 goto error_return;
2330 goto abort_return;
2331 }
2332 xfs_itrace_ref(cdp);
2333
2334 /*
2335 * Now we add the directory inode to the transaction.
2336 * We waited until now since xfs_dir_ialloc might start
2337 * a new transaction. Had we joined the transaction
2338 * earlier, the locks might have gotten released. An error
2339 * from here on will result in the transaction cancel
2340 * unlocking dp so don't do it explicitly in the error path.
2341 */
2342 IHOLD(dp);
2343 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2344 unlock_dp_on_error = B_FALSE;
2345
2346 XFS_BMAP_INIT(&free_list, &first_block);
2347
2348 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2349 &first_block, &free_list, resblks ?
2350 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2351 if (error) {
2352 ASSERT(error != ENOSPC);
2353 goto error1;
2354 }
2355 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2356
2357 /*
2358 * Bump the in memory version number of the parent directory
2359 * so that other processes accessing it will recognize that
2360 * the directory has changed.
2361 */
2362 dp->i_gen++;
2363
2364 error = xfs_dir_init(tp, cdp, dp);
2365 if (error)
2366 goto error2;
2367
2368 cdp->i_gen = 1;
2369 error = xfs_bumplink(tp, dp);
2370 if (error)
2371 goto error2;
2372
2373 created = B_TRUE;
2374
2375 *ipp = cdp;
2376 IHOLD(cdp);
2377
2378 /*
2379 * Attach the dquots to the new inode and modify the icount incore.
2380 */
2381 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2382
2383 /*
2384 * If this is a synchronous mount, make sure that the
2385 * mkdir transaction goes to disk before returning to
2386 * the user.
2387 */
2388 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2389 xfs_trans_set_sync(tp);
2390 }
2391
2392 error = xfs_bmap_finish(&tp, &free_list, &committed);
2393 if (error) {
2394 IRELE(cdp);
2395 goto error2;
2396 }
2397
2398 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2399 XFS_QM_DQRELE(mp, udqp);
2400 XFS_QM_DQRELE(mp, gdqp);
2401 if (error) {
2402 IRELE(cdp);
2403 }
2404
2405 /* Fall through to std_return with error = 0 or errno from
2406 * xfs_trans_commit. */
2407
2408 std_return:
2409 if ((created || (error != 0 && dm_event_sent != 0)) &&
2410 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2411 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2412 dp, DM_RIGHT_NULL,
2413 created ? cdp : NULL,
2414 DM_RIGHT_NULL,
2415 dir_name->name, NULL,
2416 mode, error, 0);
2417 }
2418 return error;
2419
2420 error2:
2421 error1:
2422 xfs_bmap_cancel(&free_list);
2423 abort_return:
2424 cancel_flags |= XFS_TRANS_ABORT;
2425 error_return:
2426 xfs_trans_cancel(tp, cancel_flags);
2427 XFS_QM_DQRELE(mp, udqp);
2428 XFS_QM_DQRELE(mp, gdqp);
2429
2430 if (unlock_dp_on_error)
2431 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2432
2433 goto std_return;
2434 }
2435
2436 int
2437 xfs_symlink(
2438 xfs_inode_t *dp,
2439 struct xfs_name *link_name,
2440 const char *target_path,
2441 mode_t mode,
2442 xfs_inode_t **ipp,
2443 cred_t *credp)
2444 {
2445 xfs_mount_t *mp = dp->i_mount;
2446 xfs_trans_t *tp;
2447 xfs_inode_t *ip;
2448 int error;
2449 int pathlen;
2450 xfs_bmap_free_t free_list;
2451 xfs_fsblock_t first_block;
2452 boolean_t unlock_dp_on_error = B_FALSE;
2453 uint cancel_flags;
2454 int committed;
2455 xfs_fileoff_t first_fsb;
2456 xfs_filblks_t fs_blocks;
2457 int nmaps;
2458 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2459 xfs_daddr_t d;
2460 const char *cur_chunk;
2461 int byte_cnt;
2462 int n;
2463 xfs_buf_t *bp;
2464 xfs_prid_t prid;
2465 struct xfs_dquot *udqp, *gdqp;
2466 uint resblks;
2467
2468 *ipp = NULL;
2469 error = 0;
2470 ip = NULL;
2471 tp = NULL;
2472
2473 xfs_itrace_entry(dp);
2474
2475 if (XFS_FORCED_SHUTDOWN(mp))
2476 return XFS_ERROR(EIO);
2477
2478 /*
2479 * Check component lengths of the target path name.
2480 */
2481 pathlen = strlen(target_path);
2482 if (pathlen >= MAXPATHLEN) /* total string too long */
2483 return XFS_ERROR(ENAMETOOLONG);
2484
2485 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
2486 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
2487 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2488 link_name->name, target_path, 0, 0, 0);
2489 if (error)
2490 return error;
2491 }
2492
2493 /* Return through std_return after this point. */
2494
2495 udqp = gdqp = NULL;
2496 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2497 prid = dp->i_d.di_projid;
2498 else
2499 prid = (xfs_prid_t)dfltprid;
2500
2501 /*
2502 * Make sure that we have allocated dquot(s) on disk.
2503 */
2504 error = XFS_QM_DQVOPALLOC(mp, dp,
2505 current_fsuid(), current_fsgid(), prid,
2506 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2507 if (error)
2508 goto std_return;
2509
2510 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2511 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2512 /*
2513 * The symlink will fit into the inode data fork?
2514 * There can't be any attributes so we get the whole variable part.
2515 */
2516 if (pathlen <= XFS_LITINO(mp))
2517 fs_blocks = 0;
2518 else
2519 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2520 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2521 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2522 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2523 if (error == ENOSPC && fs_blocks == 0) {
2524 resblks = 0;
2525 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2526 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2527 }
2528 if (error) {
2529 cancel_flags = 0;
2530 goto error_return;
2531 }
2532
2533 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2534 unlock_dp_on_error = B_TRUE;
2535
2536 /*
2537 * Check whether the directory allows new symlinks or not.
2538 */
2539 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2540 error = XFS_ERROR(EPERM);
2541 goto error_return;
2542 }
2543
2544 /*
2545 * Reserve disk quota : blocks and inode.
2546 */
2547 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2548 if (error)
2549 goto error_return;
2550
2551 /*
2552 * Check for ability to enter directory entry, if no space reserved.
2553 */
2554 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2555 if (error)
2556 goto error_return;
2557 /*
2558 * Initialize the bmap freelist prior to calling either
2559 * bmapi or the directory create code.
2560 */
2561 XFS_BMAP_INIT(&free_list, &first_block);
2562
2563 /*
2564 * Allocate an inode for the symlink.
2565 */
2566 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2567 1, 0, credp, prid, resblks > 0, &ip, NULL);
2568 if (error) {
2569 if (error == ENOSPC)
2570 goto error_return;
2571 goto error1;
2572 }
2573 xfs_itrace_ref(ip);
2574
2575 /*
2576 * An error after we've joined dp to the transaction will result in the
2577 * transaction cancel unlocking dp so don't do it explicitly in the
2578 * error path.
2579 */
2580 IHOLD(dp);
2581 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2582 unlock_dp_on_error = B_FALSE;
2583
2584 /*
2585 * Also attach the dquot(s) to it, if applicable.
2586 */
2587 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2588
2589 if (resblks)
2590 resblks -= XFS_IALLOC_SPACE_RES(mp);
2591 /*
2592 * If the symlink will fit into the inode, write it inline.
2593 */
2594 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2595 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2596 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2597 ip->i_d.di_size = pathlen;
2598
2599 /*
2600 * The inode was initially created in extent format.
2601 */
2602 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2603 ip->i_df.if_flags |= XFS_IFINLINE;
2604
2605 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2606 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2607
2608 } else {
2609 first_fsb = 0;
2610 nmaps = SYMLINK_MAPS;
2611
2612 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2613 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2614 &first_block, resblks, mval, &nmaps,
2615 &free_list, NULL);
2616 if (error) {
2617 goto error1;
2618 }
2619
2620 if (resblks)
2621 resblks -= fs_blocks;
2622 ip->i_d.di_size = pathlen;
2623 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2624
2625 cur_chunk = target_path;
2626 for (n = 0; n < nmaps; n++) {
2627 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2628 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2629 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2630 BTOBB(byte_cnt), 0);
2631 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2632 if (pathlen < byte_cnt) {
2633 byte_cnt = pathlen;
2634 }
2635 pathlen -= byte_cnt;
2636
2637 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2638 cur_chunk += byte_cnt;
2639
2640 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2641 }
2642 }
2643
2644 /*
2645 * Create the directory entry for the symlink.
2646 */
2647 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2648 &first_block, &free_list, resblks);
2649 if (error)
2650 goto error1;
2651 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2652 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2653
2654 /*
2655 * Bump the in memory version number of the parent directory
2656 * so that other processes accessing it will recognize that
2657 * the directory has changed.
2658 */
2659 dp->i_gen++;
2660
2661 /*
2662 * If this is a synchronous mount, make sure that the
2663 * symlink transaction goes to disk before returning to
2664 * the user.
2665 */
2666 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2667 xfs_trans_set_sync(tp);
2668 }
2669
2670 /*
2671 * xfs_trans_commit normally decrements the vnode ref count
2672 * when it unlocks the inode. Since we want to return the
2673 * vnode to the caller, we bump the vnode ref count now.
2674 */
2675 IHOLD(ip);
2676
2677 error = xfs_bmap_finish(&tp, &free_list, &committed);
2678 if (error) {
2679 goto error2;
2680 }
2681 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2682 XFS_QM_DQRELE(mp, udqp);
2683 XFS_QM_DQRELE(mp, gdqp);
2684
2685 /* Fall through to std_return with error = 0 or errno from
2686 * xfs_trans_commit */
2687 std_return:
2688 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
2689 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
2690 dp, DM_RIGHT_NULL,
2691 error ? NULL : ip,
2692 DM_RIGHT_NULL, link_name->name,
2693 target_path, 0, error, 0);
2694 }
2695
2696 if (!error)
2697 *ipp = ip;
2698 return error;
2699
2700 error2:
2701 IRELE(ip);
2702 error1:
2703 xfs_bmap_cancel(&free_list);
2704 cancel_flags |= XFS_TRANS_ABORT;
2705 error_return:
2706 xfs_trans_cancel(tp, cancel_flags);
2707 XFS_QM_DQRELE(mp, udqp);
2708 XFS_QM_DQRELE(mp, gdqp);
2709
2710 if (unlock_dp_on_error)
2711 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2712
2713 goto std_return;
2714 }
2715
2716 int
2717 xfs_inode_flush(
2718 xfs_inode_t *ip,
2719 int flags)
2720 {
2721 xfs_mount_t *mp = ip->i_mount;
2722 int error = 0;
2723
2724 if (XFS_FORCED_SHUTDOWN(mp))
2725 return XFS_ERROR(EIO);
2726
2727 /*
2728 * Bypass inodes which have already been cleaned by
2729 * the inode flush clustering code inside xfs_iflush
2730 */
2731 if (xfs_inode_clean(ip))
2732 return 0;
2733
2734 /*
2735 * We make this non-blocking if the inode is contended,
2736 * return EAGAIN to indicate to the caller that they
2737 * did not succeed. This prevents the flush path from
2738 * blocking on inodes inside another operation right
2739 * now, they get caught later by xfs_sync.
2740 */
2741 if (flags & FLUSH_SYNC) {
2742 xfs_ilock(ip, XFS_ILOCK_SHARED);
2743 xfs_iflock(ip);
2744 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2745 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2746 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2747 return EAGAIN;
2748 }
2749 } else {
2750 return EAGAIN;
2751 }
2752
2753 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2754 : XFS_IFLUSH_ASYNC_NOBLOCK);
2755 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2756
2757 return error;
2758 }
2759
2760
2761 int
2762 xfs_set_dmattrs(
2763 xfs_inode_t *ip,
2764 u_int evmask,
2765 u_int16_t state)
2766 {
2767 xfs_mount_t *mp = ip->i_mount;
2768 xfs_trans_t *tp;
2769 int error;
2770
2771 if (!capable(CAP_SYS_ADMIN))
2772 return XFS_ERROR(EPERM);
2773
2774 if (XFS_FORCED_SHUTDOWN(mp))
2775 return XFS_ERROR(EIO);
2776
2777 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2778 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2779 if (error) {
2780 xfs_trans_cancel(tp, 0);
2781 return error;
2782 }
2783 xfs_ilock(ip, XFS_ILOCK_EXCL);
2784 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2785
2786 ip->i_d.di_dmevmask = evmask;
2787 ip->i_d.di_dmstate = state;
2788
2789 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2790 IHOLD(ip);
2791 error = xfs_trans_commit(tp, 0);
2792
2793 return error;
2794 }
2795
2796 int
2797 xfs_reclaim(
2798 xfs_inode_t *ip)
2799 {
2800
2801 xfs_itrace_entry(ip);
2802
2803 ASSERT(!VN_MAPPED(VFS_I(ip)));
2804
2805 /* bad inode, get out here ASAP */
2806 if (VN_BAD(VFS_I(ip))) {
2807 xfs_ireclaim(ip);
2808 return 0;
2809 }
2810
2811 vn_iowait(ip);
2812
2813 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
2814
2815 /*
2816 * Make sure the atime in the XFS inode is correct before freeing the
2817 * Linux inode.
2818 */
2819 xfs_synchronize_atime(ip);
2820
2821 /*
2822 * If we have nothing to flush with this inode then complete the
2823 * teardown now, otherwise break the link between the xfs inode and the
2824 * linux inode and clean up the xfs inode later. This avoids flushing
2825 * the inode to disk during the delete operation itself.
2826 *
2827 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2828 * first to ensure that xfs_iunpin() will never see an xfs inode
2829 * that has a linux inode being reclaimed. Synchronisation is provided
2830 * by the i_flags_lock.
2831 */
2832 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2833 xfs_ilock(ip, XFS_ILOCK_EXCL);
2834 xfs_iflock(ip);
2835 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
2836 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2837 }
2838 xfs_inode_set_reclaim_tag(ip);
2839 return 0;
2840 }
2841
2842 /*
2843 * xfs_alloc_file_space()
2844 * This routine allocates disk space for the given file.
2845 *
2846 * If alloc_type == 0, this request is for an ALLOCSP type
2847 * request which will change the file size. In this case, no
2848 * DMAPI event will be generated by the call. A TRUNCATE event
2849 * will be generated later by xfs_setattr.
2850 *
2851 * If alloc_type != 0, this request is for a RESVSP type
2852 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2853 * lower block boundary byte address is less than the file's
2854 * length.
2855 *
2856 * RETURNS:
2857 * 0 on success
2858 * errno on error
2859 *
2860 */
2861 STATIC int
2862 xfs_alloc_file_space(
2863 xfs_inode_t *ip,
2864 xfs_off_t offset,
2865 xfs_off_t len,
2866 int alloc_type,
2867 int attr_flags)
2868 {
2869 xfs_mount_t *mp = ip->i_mount;
2870 xfs_off_t count;
2871 xfs_filblks_t allocated_fsb;
2872 xfs_filblks_t allocatesize_fsb;
2873 xfs_extlen_t extsz, temp;
2874 xfs_fileoff_t startoffset_fsb;
2875 xfs_fsblock_t firstfsb;
2876 int nimaps;
2877 int bmapi_flag;
2878 int quota_flag;
2879 int rt;
2880 xfs_trans_t *tp;
2881 xfs_bmbt_irec_t imaps[1], *imapp;
2882 xfs_bmap_free_t free_list;
2883 uint qblocks, resblks, resrtextents;
2884 int committed;
2885 int error;
2886
2887 xfs_itrace_entry(ip);
2888
2889 if (XFS_FORCED_SHUTDOWN(mp))
2890 return XFS_ERROR(EIO);
2891
2892 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2893 return error;
2894
2895 if (len <= 0)
2896 return XFS_ERROR(EINVAL);
2897
2898 rt = XFS_IS_REALTIME_INODE(ip);
2899 extsz = xfs_get_extsz_hint(ip);
2900
2901 count = len;
2902 imapp = &imaps[0];
2903 nimaps = 1;
2904 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
2905 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2906 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2907
2908 /* Generate a DMAPI event if needed. */
2909 if (alloc_type != 0 && offset < ip->i_size &&
2910 (attr_flags & XFS_ATTR_DMI) == 0 &&
2911 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2912 xfs_off_t end_dmi_offset;
2913
2914 end_dmi_offset = offset+len;
2915 if (end_dmi_offset > ip->i_size)
2916 end_dmi_offset = ip->i_size;
2917 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2918 end_dmi_offset - offset, 0, NULL);
2919 if (error)
2920 return error;
2921 }
2922
2923 /*
2924 * Allocate file space until done or until there is an error
2925 */
2926 retry:
2927 while (allocatesize_fsb && !error) {
2928 xfs_fileoff_t s, e;
2929
2930 /*
2931 * Determine space reservations for data/realtime.
2932 */
2933 if (unlikely(extsz)) {
2934 s = startoffset_fsb;
2935 do_div(s, extsz);
2936 s *= extsz;
2937 e = startoffset_fsb + allocatesize_fsb;
2938 if ((temp = do_mod(startoffset_fsb, extsz)))
2939 e += temp;
2940 if ((temp = do_mod(e, extsz)))
2941 e += extsz - temp;
2942 } else {
2943 s = 0;
2944 e = allocatesize_fsb;
2945 }
2946
2947 if (unlikely(rt)) {
2948 resrtextents = qblocks = (uint)(e - s);
2949 resrtextents /= mp->m_sb.sb_rextsize;
2950 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2951 quota_flag = XFS_QMOPT_RES_RTBLKS;
2952 } else {
2953 resrtextents = 0;
2954 resblks = qblocks = \
2955 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2956 quota_flag = XFS_QMOPT_RES_REGBLKS;
2957 }
2958
2959 /*
2960 * Allocate and setup the transaction.
2961 */
2962 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2963 error = xfs_trans_reserve(tp, resblks,
2964 XFS_WRITE_LOG_RES(mp), resrtextents,
2965 XFS_TRANS_PERM_LOG_RES,
2966 XFS_WRITE_LOG_COUNT);
2967 /*
2968 * Check for running out of space
2969 */
2970 if (error) {
2971 /*
2972 * Free the transaction structure.
2973 */
2974 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2975 xfs_trans_cancel(tp, 0);
2976 break;
2977 }
2978 xfs_ilock(ip, XFS_ILOCK_EXCL);
2979 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2980 qblocks, 0, quota_flag);
2981 if (error)
2982 goto error1;
2983
2984 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2985 xfs_trans_ihold(tp, ip);
2986
2987 /*
2988 * Issue the xfs_bmapi() call to allocate the blocks
2989 */
2990 XFS_BMAP_INIT(&free_list, &firstfsb);
2991 error = xfs_bmapi(tp, ip, startoffset_fsb,
2992 allocatesize_fsb, bmapi_flag,
2993 &firstfsb, 0, imapp, &nimaps,
2994 &free_list, NULL);
2995 if (error) {
2996 goto error0;
2997 }
2998
2999 /*
3000 * Complete the transaction
3001 */
3002 error = xfs_bmap_finish(&tp, &free_list, &committed);
3003 if (error) {
3004 goto error0;
3005 }
3006
3007 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3008 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3009 if (error) {
3010 break;
3011 }
3012
3013 allocated_fsb = imapp->br_blockcount;
3014
3015 if (nimaps == 0) {
3016 error = XFS_ERROR(ENOSPC);
3017 break;
3018 }
3019
3020 startoffset_fsb += allocated_fsb;
3021 allocatesize_fsb -= allocated_fsb;
3022 }
3023 dmapi_enospc_check:
3024 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
3025 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
3026 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3027 ip, DM_RIGHT_NULL,
3028 ip, DM_RIGHT_NULL,
3029 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3030 if (error == 0)
3031 goto retry; /* Maybe DMAPI app. has made space */
3032 /* else fall through with error from XFS_SEND_DATA */
3033 }
3034
3035 return error;
3036
3037 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3038 xfs_bmap_cancel(&free_list);
3039 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3040
3041 error1: /* Just cancel transaction */
3042 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3043 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3044 goto dmapi_enospc_check;
3045 }
3046
3047 /*
3048 * Zero file bytes between startoff and endoff inclusive.
3049 * The iolock is held exclusive and no blocks are buffered.
3050 *
3051 * This function is used by xfs_free_file_space() to zero
3052 * partial blocks when the range to free is not block aligned.
3053 * When unreserving space with boundaries that are not block
3054 * aligned we round up the start and round down the end
3055 * boundaries and then use this function to zero the parts of
3056 * the blocks that got dropped during the rounding.
3057 */
3058 STATIC int
3059 xfs_zero_remaining_bytes(
3060 xfs_inode_t *ip,
3061 xfs_off_t startoff,
3062 xfs_off_t endoff)
3063 {
3064 xfs_bmbt_irec_t imap;
3065 xfs_fileoff_t offset_fsb;
3066 xfs_off_t lastoffset;
3067 xfs_off_t offset;
3068 xfs_buf_t *bp;
3069 xfs_mount_t *mp = ip->i_mount;
3070 int nimap;
3071 int error = 0;
3072
3073 /*
3074 * Avoid doing I/O beyond eof - it's not necessary
3075 * since nothing can read beyond eof. The space will
3076 * be zeroed when the file is extended anyway.
3077 */
3078 if (startoff >= ip->i_size)
3079 return 0;
3080
3081 if (endoff > ip->i_size)
3082 endoff = ip->i_size;
3083
3084 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
3085 XFS_IS_REALTIME_INODE(ip) ?
3086 mp->m_rtdev_targp : mp->m_ddev_targp);
3087
3088 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3089 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3090 nimap = 1;
3091 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
3092 NULL, 0, &imap, &nimap, NULL, NULL);
3093 if (error || nimap < 1)
3094 break;
3095 ASSERT(imap.br_blockcount >= 1);
3096 ASSERT(imap.br_startoff == offset_fsb);
3097 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3098 if (lastoffset > endoff)
3099 lastoffset = endoff;
3100 if (imap.br_startblock == HOLESTARTBLOCK)
3101 continue;
3102 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3103 if (imap.br_state == XFS_EXT_UNWRITTEN)
3104 continue;
3105 XFS_BUF_UNDONE(bp);
3106 XFS_BUF_UNWRITE(bp);
3107 XFS_BUF_READ(bp);
3108 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3109 xfsbdstrat(mp, bp);
3110 error = xfs_iowait(bp);
3111 if (error) {
3112 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3113 mp, bp, XFS_BUF_ADDR(bp));
3114 break;
3115 }
3116 memset(XFS_BUF_PTR(bp) +
3117 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3118 0, lastoffset - offset + 1);
3119 XFS_BUF_UNDONE(bp);
3120 XFS_BUF_UNREAD(bp);
3121 XFS_BUF_WRITE(bp);
3122 xfsbdstrat(mp, bp);
3123 error = xfs_iowait(bp);
3124 if (error) {
3125 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3126 mp, bp, XFS_BUF_ADDR(bp));
3127 break;
3128 }
3129 }
3130 xfs_buf_free(bp);
3131 return error;
3132 }
3133
3134 /*
3135 * xfs_free_file_space()
3136 * This routine frees disk space for the given file.
3137 *
3138 * This routine is only called by xfs_change_file_space
3139 * for an UNRESVSP type call.
3140 *
3141 * RETURNS:
3142 * 0 on success
3143 * errno on error
3144 *
3145 */
3146 STATIC int
3147 xfs_free_file_space(
3148 xfs_inode_t *ip,
3149 xfs_off_t offset,
3150 xfs_off_t len,
3151 int attr_flags)
3152 {
3153 int committed;
3154 int done;
3155 xfs_off_t end_dmi_offset;
3156 xfs_fileoff_t endoffset_fsb;
3157 int error;
3158 xfs_fsblock_t firstfsb;
3159 xfs_bmap_free_t free_list;
3160 xfs_bmbt_irec_t imap;
3161 xfs_off_t ioffset;
3162 xfs_extlen_t mod=0;
3163 xfs_mount_t *mp;
3164 int nimap;
3165 uint resblks;
3166 uint rounding;
3167 int rt;
3168 xfs_fileoff_t startoffset_fsb;
3169 xfs_trans_t *tp;
3170 int need_iolock = 1;
3171
3172 mp = ip->i_mount;
3173
3174 xfs_itrace_entry(ip);
3175
3176 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3177 return error;
3178
3179 error = 0;
3180 if (len <= 0) /* if nothing being freed */
3181 return error;
3182 rt = XFS_IS_REALTIME_INODE(ip);
3183 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3184 end_dmi_offset = offset + len;
3185 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3186
3187 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
3188 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3189 if (end_dmi_offset > ip->i_size)
3190 end_dmi_offset = ip->i_size;
3191 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
3192 offset, end_dmi_offset - offset,
3193 AT_DELAY_FLAG(attr_flags), NULL);
3194 if (error)
3195 return error;
3196 }
3197
3198 if (attr_flags & XFS_ATTR_NOLOCK)
3199 need_iolock = 0;
3200 if (need_iolock) {
3201 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3202 vn_iowait(ip); /* wait for the completion of any pending DIOs */
3203 }
3204
3205 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
3206 ioffset = offset & ~(rounding - 1);
3207
3208 if (VN_CACHED(VFS_I(ip)) != 0) {
3209 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3210 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
3211 if (error)
3212 goto out_unlock_iolock;
3213 }
3214
3215 /*
3216 * Need to zero the stuff we're not freeing, on disk.
3217 * If its a realtime file & can't use unwritten extents then we
3218 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3219 * will take care of it for us.
3220 */
3221 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
3222 nimap = 1;
3223 error = xfs_bmapi(NULL, ip, startoffset_fsb,
3224 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3225 if (error)
3226 goto out_unlock_iolock;
3227 ASSERT(nimap == 0 || nimap == 1);
3228 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3229 xfs_daddr_t block;
3230
3231 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3232 block = imap.br_startblock;
3233 mod = do_div(block, mp->m_sb.sb_rextsize);
3234 if (mod)
3235 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3236 }
3237 nimap = 1;
3238 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
3239 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3240 if (error)
3241 goto out_unlock_iolock;
3242 ASSERT(nimap == 0 || nimap == 1);
3243 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3244 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3245 mod++;
3246 if (mod && (mod != mp->m_sb.sb_rextsize))
3247 endoffset_fsb -= mod;
3248 }
3249 }
3250 if ((done = (endoffset_fsb <= startoffset_fsb)))
3251 /*
3252 * One contiguous piece to clear
3253 */
3254 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3255 else {
3256 /*
3257 * Some full blocks, possibly two pieces to clear
3258 */
3259 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3260 error = xfs_zero_remaining_bytes(ip, offset,
3261 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3262 if (!error &&
3263 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3264 error = xfs_zero_remaining_bytes(ip,
3265 XFS_FSB_TO_B(mp, endoffset_fsb),
3266 offset + len - 1);
3267 }
3268
3269 /*
3270 * free file space until done or until there is an error
3271 */
3272 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3273 while (!error && !done) {
3274
3275 /*
3276 * allocate and setup the transaction. Allow this
3277 * transaction to dip into the reserve blocks to ensure
3278 * the freeing of the space succeeds at ENOSPC.
3279 */
3280 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3281 tp->t_flags |= XFS_TRANS_RESERVE;
3282 error = xfs_trans_reserve(tp,
3283 resblks,
3284 XFS_WRITE_LOG_RES(mp),
3285 0,
3286 XFS_TRANS_PERM_LOG_RES,
3287 XFS_WRITE_LOG_COUNT);
3288
3289 /*
3290 * check for running out of space
3291 */
3292 if (error) {
3293 /*
3294 * Free the transaction structure.
3295 */
3296 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3297 xfs_trans_cancel(tp, 0);
3298 break;
3299 }
3300 xfs_ilock(ip, XFS_ILOCK_EXCL);
3301 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
3302 ip->i_udquot, ip->i_gdquot, resblks, 0,
3303 XFS_QMOPT_RES_REGBLKS);
3304 if (error)
3305 goto error1;
3306
3307 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3308 xfs_trans_ihold(tp, ip);
3309
3310 /*
3311 * issue the bunmapi() call to free the blocks
3312 */
3313 XFS_BMAP_INIT(&free_list, &firstfsb);
3314 error = xfs_bunmapi(tp, ip, startoffset_fsb,
3315 endoffset_fsb - startoffset_fsb,
3316 0, 2, &firstfsb, &free_list, NULL, &done);
3317 if (error) {
3318 goto error0;
3319 }
3320
3321 /*
3322 * complete the transaction
3323 */
3324 error = xfs_bmap_finish(&tp, &free_list, &committed);
3325 if (error) {
3326 goto error0;
3327 }
3328
3329 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3330 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3331 }
3332
3333 out_unlock_iolock:
3334 if (need_iolock)
3335 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3336 return error;
3337
3338 error0:
3339 xfs_bmap_cancel(&free_list);
3340 error1:
3341 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3342 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3343 XFS_ILOCK_EXCL);
3344 return error;
3345 }
3346
3347 /*
3348 * xfs_change_file_space()
3349 * This routine allocates or frees disk space for the given file.
3350 * The user specified parameters are checked for alignment and size
3351 * limitations.
3352 *
3353 * RETURNS:
3354 * 0 on success
3355 * errno on error
3356 *
3357 */
3358 int
3359 xfs_change_file_space(
3360 xfs_inode_t *ip,
3361 int cmd,
3362 xfs_flock64_t *bf,
3363 xfs_off_t offset,
3364 int attr_flags)
3365 {
3366 xfs_mount_t *mp = ip->i_mount;
3367 int clrprealloc;
3368 int error;
3369 xfs_fsize_t fsize;
3370 int setprealloc;
3371 xfs_off_t startoffset;
3372 xfs_off_t llen;
3373 xfs_trans_t *tp;
3374 struct iattr iattr;
3375
3376 xfs_itrace_entry(ip);
3377
3378 if (!S_ISREG(ip->i_d.di_mode))
3379 return XFS_ERROR(EINVAL);
3380
3381 switch (bf->l_whence) {
3382 case 0: /*SEEK_SET*/
3383 break;
3384 case 1: /*SEEK_CUR*/
3385 bf->l_start += offset;
3386 break;
3387 case 2: /*SEEK_END*/
3388 bf->l_start += ip->i_size;
3389 break;
3390 default:
3391 return XFS_ERROR(EINVAL);
3392 }
3393
3394 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3395
3396 if ( (bf->l_start < 0)
3397 || (bf->l_start > XFS_MAXIOFFSET(mp))
3398 || (bf->l_start + llen < 0)
3399 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3400 return XFS_ERROR(EINVAL);
3401
3402 bf->l_whence = 0;
3403
3404 startoffset = bf->l_start;
3405 fsize = ip->i_size;
3406
3407 /*
3408 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3409 * file space.
3410 * These calls do NOT zero the data space allocated to the file,
3411 * nor do they change the file size.
3412 *
3413 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3414 * space.
3415 * These calls cause the new file data to be zeroed and the file
3416 * size to be changed.
3417 */
3418 setprealloc = clrprealloc = 0;
3419
3420 switch (cmd) {
3421 case XFS_IOC_RESVSP:
3422 case XFS_IOC_RESVSP64:
3423 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3424 1, attr_flags);
3425 if (error)
3426 return error;
3427 setprealloc = 1;
3428 break;
3429
3430 case XFS_IOC_UNRESVSP:
3431 case XFS_IOC_UNRESVSP64:
3432 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3433 attr_flags)))
3434 return error;
3435 break;
3436
3437 case XFS_IOC_ALLOCSP:
3438 case XFS_IOC_ALLOCSP64:
3439 case XFS_IOC_FREESP:
3440 case XFS_IOC_FREESP64:
3441 if (startoffset > fsize) {
3442 error = xfs_alloc_file_space(ip, fsize,
3443 startoffset - fsize, 0, attr_flags);
3444 if (error)
3445 break;
3446 }
3447
3448 iattr.ia_valid = ATTR_SIZE;
3449 iattr.ia_size = startoffset;
3450
3451 error = xfs_setattr(ip, &iattr, attr_flags);
3452
3453 if (error)
3454 return error;
3455
3456 clrprealloc = 1;
3457 break;
3458
3459 default:
3460 ASSERT(0);
3461 return XFS_ERROR(EINVAL);
3462 }
3463
3464 /*
3465 * update the inode timestamp, mode, and prealloc flag bits
3466 */
3467 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3468
3469 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3470 0, 0, 0))) {
3471 /* ASSERT(0); */
3472 xfs_trans_cancel(tp, 0);
3473 return error;
3474 }
3475
3476 xfs_ilock(ip, XFS_ILOCK_EXCL);
3477
3478 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3479 xfs_trans_ihold(tp, ip);
3480
3481 if ((attr_flags & XFS_ATTR_DMI) == 0) {
3482 ip->i_d.di_mode &= ~S_ISUID;
3483
3484 /*
3485 * Note that we don't have to worry about mandatory
3486 * file locking being disabled here because we only
3487 * clear the S_ISGID bit if the Group execute bit is
3488 * on, but if it was on then mandatory locking wouldn't
3489 * have been enabled.
3490 */
3491 if (ip->i_d.di_mode & S_IXGRP)
3492 ip->i_d.di_mode &= ~S_ISGID;
3493
3494 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3495 }
3496 if (setprealloc)
3497 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3498 else if (clrprealloc)
3499 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3500
3501 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3502 xfs_trans_set_sync(tp);
3503
3504 error = xfs_trans_commit(tp, 0);
3505
3506 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3507
3508 return error;
3509 }
This page took 0.10604 seconds and 5 git commands to generate.