Linux Audio

Check our new training course

Loading...
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4 * All Rights Reserved.
  5 */
  6
  7#include "xfs.h"
  8#include "xfs_fs.h"
  9#include "xfs_shared.h"
 10#include "xfs_format.h"
 11#include "xfs_log_format.h"
 12#include "xfs_trans_resv.h"
 13#include "xfs_mount.h"
 14#include "xfs_inode.h"
 15#include "xfs_trans.h"
 16#include "xfs_inode_item.h"
 17#include "xfs_btree.h"
 18#include "xfs_bmap_btree.h"
 19#include "xfs_bmap.h"
 20#include "xfs_error.h"
 21#include "xfs_trace.h"
 22#include "xfs_da_format.h"
 23#include "xfs_da_btree.h"
 24#include "xfs_dir2_priv.h"
 25#include "xfs_attr_leaf.h"
 26#include "xfs_types.h"
 27#include "xfs_errortag.h"
 28#include "xfs_health.h"
 29#include "xfs_symlink_remote.h"
 30
 31struct kmem_cache *xfs_ifork_cache;
 32
 33void
 34xfs_init_local_fork(
 35	struct xfs_inode	*ip,
 36	int			whichfork,
 37	const void		*data,
 38	int64_t			size)
 39{
 40	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
 41	int			mem_size = size;
 42	bool			zero_terminate;
 43
 44	/*
 45	 * If we are using the local fork to store a symlink body we need to
 46	 * zero-terminate it so that we can pass it back to the VFS directly.
 47	 * Overallocate the in-memory fork by one for that and add a zero
 48	 * to terminate it below.
 49	 */
 50	zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
 51	if (zero_terminate)
 52		mem_size++;
 53
 54	if (size) {
 55		char *new_data = kmalloc(mem_size,
 56				GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
 57
 58		memcpy(new_data, data, size);
 59		if (zero_terminate)
 60			new_data[size] = '\0';
 61
 62		ifp->if_data = new_data;
 63	} else {
 64		ifp->if_data = NULL;
 65	}
 66
 67	ifp->if_bytes = size;
 68}
 69
 70/*
 71 * The file is in-lined in the on-disk inode.
 72 */
 73STATIC int
 74xfs_iformat_local(
 75	struct xfs_inode	*ip,
 76	struct xfs_dinode	*dip,
 77	int			whichfork,
 78	int			size)
 79{
 80	/*
 81	 * If the size is unreasonable, then something
 82	 * is wrong and we just bail out rather than crash in
 83	 * kmalloc() or memcpy() below.
 84	 */
 85	if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
 86		xfs_warn(ip->i_mount,
 87	"corrupt inode %llu (bad size %d for local fork, size = %zd).",
 88			(unsigned long long) ip->i_ino, size,
 89			XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
 90		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
 91				"xfs_iformat_local", dip, sizeof(*dip),
 92				__this_address);
 93		xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
 94		return -EFSCORRUPTED;
 95	}
 96
 97	xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
 98	return 0;
 99}
100
101/*
102 * The file consists of a set of extents all of which fit into the on-disk
103 * inode.
104 */
105STATIC int
106xfs_iformat_extents(
107	struct xfs_inode	*ip,
108	struct xfs_dinode	*dip,
109	int			whichfork)
110{
111	struct xfs_mount	*mp = ip->i_mount;
112	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
113	int			state = xfs_bmap_fork_to_state(whichfork);
114	xfs_extnum_t		nex = xfs_dfork_nextents(dip, whichfork);
115	int			size = nex * sizeof(xfs_bmbt_rec_t);
116	struct xfs_iext_cursor	icur;
117	struct xfs_bmbt_rec	*dp;
118	struct xfs_bmbt_irec	new;
119	int			i;
120
121	/*
122	 * If the number of extents is unreasonable, then something is wrong and
123	 * we just bail out rather than crash in kmalloc() or memcpy() below.
124	 */
125	if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
126		xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).",
127			ip->i_ino, nex);
128		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
129				"xfs_iformat_extents(1)", dip, sizeof(*dip),
130				__this_address);
131		xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
132		return -EFSCORRUPTED;
133	}
134
135	ifp->if_bytes = 0;
136	ifp->if_data = NULL;
137	ifp->if_height = 0;
138	if (size) {
139		dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
140
141		xfs_iext_first(ifp, &icur);
142		for (i = 0; i < nex; i++, dp++) {
143			xfs_failaddr_t	fa;
144
145			xfs_bmbt_disk_get_all(dp, &new);
146			fa = xfs_bmap_validate_extent(ip, whichfork, &new);
147			if (fa) {
148				xfs_inode_verifier_error(ip, -EFSCORRUPTED,
149						"xfs_iformat_extents(2)",
150						dp, sizeof(*dp), fa);
151				xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
152				return xfs_bmap_complain_bad_rec(ip, whichfork,
153						fa, &new);
154			}
155
156			xfs_iext_insert(ip, &icur, &new, state);
157			trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
158			xfs_iext_next(ifp, &icur);
159		}
160	}
161	return 0;
162}
163
164/*
165 * The file has too many extents to fit into
166 * the inode, so they are in B-tree format.
167 * Allocate a buffer for the root of the B-tree
168 * and copy the root into it.  The i_extents
169 * field will remain NULL until all of the
170 * extents are read in (when they are needed).
171 */
172STATIC int
173xfs_iformat_btree(
174	struct xfs_inode	*ip,
175	struct xfs_dinode	*dip,
176	int			whichfork)
177{
178	struct xfs_mount	*mp = ip->i_mount;
179	xfs_bmdr_block_t	*dfp;
180	struct xfs_ifork	*ifp;
181	/* REFERENCED */
182	int			nrecs;
183	int			size;
184	int			level;
185
186	ifp = xfs_ifork_ptr(ip, whichfork);
187	dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
188	size = XFS_BMAP_BROOT_SPACE(mp, dfp);
189	nrecs = be16_to_cpu(dfp->bb_numrecs);
190	level = be16_to_cpu(dfp->bb_level);
191
192	/*
193	 * blow out if -- fork has less extents than can fit in
194	 * fork (fork shouldn't be a btree format), root btree
195	 * block has more records than can fit into the fork,
196	 * or the number of extents is greater than the number of
197	 * blocks.
198	 */
199	if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
200		     nrecs == 0 ||
201		     XFS_BMDR_SPACE_CALC(nrecs) >
202					XFS_DFORK_SIZE(dip, mp, whichfork) ||
203		     ifp->if_nextents > ip->i_nblocks) ||
204		     level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
205		xfs_warn(mp, "corrupt inode %llu (btree).",
206					(unsigned long long) ip->i_ino);
207		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
208				"xfs_iformat_btree", dfp, size,
209				__this_address);
210		xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
211		return -EFSCORRUPTED;
212	}
213
214	ifp->if_broot_bytes = size;
215	ifp->if_broot = kmalloc(size,
216				GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
217	ASSERT(ifp->if_broot != NULL);
218	/*
219	 * Copy and convert from the on-disk structure
220	 * to the in-memory structure.
221	 */
222	xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
223			 ifp->if_broot, size);
224
225	ifp->if_bytes = 0;
226	ifp->if_data = NULL;
227	ifp->if_height = 0;
228	return 0;
229}
230
231int
232xfs_iformat_data_fork(
233	struct xfs_inode	*ip,
234	struct xfs_dinode	*dip)
235{
236	struct inode		*inode = VFS_I(ip);
237	int			error;
238
239	/*
240	 * Initialize the extent count early, as the per-format routines may
241	 * depend on it.  Use release semantics to set needextents /after/ we
242	 * set the format. This ensures that we can use acquire semantics on
243	 * needextents in xfs_need_iread_extents() and be guaranteed to see a
244	 * valid format value after that load.
245	 */
246	ip->i_df.if_format = dip->di_format;
247	ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
248	smp_store_release(&ip->i_df.if_needextents,
249			   ip->i_df.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
250
251	switch (inode->i_mode & S_IFMT) {
252	case S_IFIFO:
253	case S_IFCHR:
254	case S_IFBLK:
255	case S_IFSOCK:
256		ip->i_disk_size = 0;
257		inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
258		return 0;
259	case S_IFREG:
260	case S_IFLNK:
261	case S_IFDIR:
262		switch (ip->i_df.if_format) {
263		case XFS_DINODE_FMT_LOCAL:
264			error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
265					be64_to_cpu(dip->di_size));
266			if (!error)
267				error = xfs_ifork_verify_local_data(ip);
268			return error;
269		case XFS_DINODE_FMT_EXTENTS:
270			return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
271		case XFS_DINODE_FMT_BTREE:
272			return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
273		default:
274			xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
275					dip, sizeof(*dip), __this_address);
276			xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
277			return -EFSCORRUPTED;
278		}
279		break;
280	default:
281		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
282				sizeof(*dip), __this_address);
283		xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
284		return -EFSCORRUPTED;
285	}
286}
287
288static uint16_t
289xfs_dfork_attr_shortform_size(
290	struct xfs_dinode		*dip)
291{
292	struct xfs_attr_sf_hdr		*sf = XFS_DFORK_APTR(dip);
 
293
294	return be16_to_cpu(sf->totsize);
295}
296
297void
298xfs_ifork_init_attr(
299	struct xfs_inode	*ip,
300	enum xfs_dinode_fmt	format,
301	xfs_extnum_t		nextents)
302{
303	/*
304	 * Initialize the extent count early, as the per-format routines may
305	 * depend on it.  Use release semantics to set needextents /after/ we
306	 * set the format. This ensures that we can use acquire semantics on
307	 * needextents in xfs_need_iread_extents() and be guaranteed to see a
308	 * valid format value after that load.
309	 */
310	ip->i_af.if_format = format;
311	ip->i_af.if_nextents = nextents;
312	smp_store_release(&ip->i_af.if_needextents,
313			   ip->i_af.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
314}
315
316void
317xfs_ifork_zap_attr(
318	struct xfs_inode	*ip)
319{
320	xfs_idestroy_fork(&ip->i_af);
321	memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
322	ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
323}
324
325int
326xfs_iformat_attr_fork(
327	struct xfs_inode	*ip,
328	struct xfs_dinode	*dip)
329{
330	xfs_extnum_t		naextents = xfs_dfork_attr_extents(dip);
331	int			error = 0;
332
333	/*
334	 * Initialize the extent count early, as the per-format routines may
335	 * depend on it.
336	 */
337	xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
 
338
339	switch (ip->i_af.if_format) {
340	case XFS_DINODE_FMT_LOCAL:
341		error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
342				xfs_dfork_attr_shortform_size(dip));
343		if (!error)
344			error = xfs_ifork_verify_local_attr(ip);
345		break;
346	case XFS_DINODE_FMT_EXTENTS:
347		error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
348		break;
349	case XFS_DINODE_FMT_BTREE:
350		error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
351		break;
352	default:
353		xfs_inode_verifier_error(ip, error, __func__, dip,
354				sizeof(*dip), __this_address);
355		xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
356		error = -EFSCORRUPTED;
357		break;
358	}
359
360	if (error)
361		xfs_ifork_zap_attr(ip);
 
 
362	return error;
363}
364
365/*
366 * Reallocate the space for if_broot based on the number of records
367 * being added or deleted as indicated in rec_diff.  Move the records
368 * and pointers in if_broot to fit the new size.  When shrinking this
369 * will eliminate holes between the records and pointers created by
370 * the caller.  When growing this will create holes to be filled in
371 * by the caller.
372 *
373 * The caller must not request to add more records than would fit in
374 * the on-disk inode root.  If the if_broot is currently NULL, then
375 * if we are adding records, one will be allocated.  The caller must also
376 * not request that the number of records go below zero, although
377 * it can go to zero.
378 *
379 * ip -- the inode whose if_broot area is changing
380 * ext_diff -- the change in the number of records, positive or negative,
381 *	 requested for the if_broot array.
382 */
383void
384xfs_iroot_realloc(
385	xfs_inode_t		*ip,
386	int			rec_diff,
387	int			whichfork)
388{
389	struct xfs_mount	*mp = ip->i_mount;
390	int			cur_max;
391	struct xfs_ifork	*ifp;
392	struct xfs_btree_block	*new_broot;
393	int			new_max;
394	size_t			new_size;
395	char			*np;
396	char			*op;
397
398	/*
399	 * Handle the degenerate case quietly.
400	 */
401	if (rec_diff == 0) {
402		return;
403	}
404
405	ifp = xfs_ifork_ptr(ip, whichfork);
406	if (rec_diff > 0) {
407		/*
408		 * If there wasn't any memory allocated before, just
409		 * allocate it now and get out.
410		 */
411		if (ifp->if_broot_bytes == 0) {
412			new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
413			ifp->if_broot = kmalloc(new_size,
414						GFP_KERNEL | __GFP_NOFAIL);
415			ifp->if_broot_bytes = (int)new_size;
416			return;
417		}
418
419		/*
420		 * If there is already an existing if_broot, then we need
421		 * to realloc() it and shift the pointers to their new
422		 * location.  The records don't change location because
423		 * they are kept butted up against the btree block header.
424		 */
425		cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
426		new_max = cur_max + rec_diff;
427		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
428		ifp->if_broot = krealloc(ifp->if_broot, new_size,
429					 GFP_KERNEL | __GFP_NOFAIL);
430		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
431						     ifp->if_broot_bytes);
432		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
433						     (int)new_size);
434		ifp->if_broot_bytes = (int)new_size;
435		ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
436			xfs_inode_fork_size(ip, whichfork));
437		memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
438		return;
439	}
440
441	/*
442	 * rec_diff is less than 0.  In this case, we are shrinking the
443	 * if_broot buffer.  It must already exist.  If we go to zero
444	 * records, just get rid of the root and clear the status bit.
445	 */
446	ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
447	cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
448	new_max = cur_max + rec_diff;
449	ASSERT(new_max >= 0);
450	if (new_max > 0)
451		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
452	else
453		new_size = 0;
454	if (new_size > 0) {
455		new_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
456		/*
457		 * First copy over the btree block header.
458		 */
459		memcpy(new_broot, ifp->if_broot,
460			XFS_BMBT_BLOCK_LEN(ip->i_mount));
461	} else {
462		new_broot = NULL;
463	}
464
465	/*
466	 * Only copy the records and pointers if there are any.
467	 */
468	if (new_max > 0) {
469		/*
470		 * First copy the records.
471		 */
472		op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
473		np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
474		memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
475
476		/*
477		 * Then copy the pointers.
478		 */
479		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
480						     ifp->if_broot_bytes);
481		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
482						     (int)new_size);
483		memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
484	}
485	kfree(ifp->if_broot);
486	ifp->if_broot = new_broot;
487	ifp->if_broot_bytes = (int)new_size;
488	if (ifp->if_broot)
489		ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
490			xfs_inode_fork_size(ip, whichfork));
491	return;
492}
493
494
495/*
496 * This is called when the amount of space needed for if_data
497 * is increased or decreased.  The change in size is indicated by
498 * the number of bytes that need to be added or deleted in the
499 * byte_diff parameter.
500 *
501 * If the amount of space needed has decreased below the size of the
502 * inline buffer, then switch to using the inline buffer.  Otherwise,
503 * use krealloc() or kmalloc() to adjust the size of the buffer
504 * to what is needed.
505 *
506 * ip -- the inode whose if_data area is changing
507 * byte_diff -- the change in the number of bytes, positive or negative,
508 *	 requested for the if_data array.
509 */
510void *
511xfs_idata_realloc(
512	struct xfs_inode	*ip,
513	int64_t			byte_diff,
514	int			whichfork)
515{
516	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
517	int64_t			new_size = ifp->if_bytes + byte_diff;
518
519	ASSERT(new_size >= 0);
520	ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
521
522	if (byte_diff) {
523		ifp->if_data = krealloc(ifp->if_data, new_size,
524					GFP_KERNEL | __GFP_NOFAIL);
525		if (new_size == 0)
526			ifp->if_data = NULL;
527		ifp->if_bytes = new_size;
 
 
528	}
529
530	return ifp->if_data;
 
 
 
 
 
 
 
531}
532
533/* Free all memory and reset a fork back to its initial state. */
534void
535xfs_idestroy_fork(
536	struct xfs_ifork	*ifp)
537{
538	if (ifp->if_broot != NULL) {
539		kfree(ifp->if_broot);
540		ifp->if_broot = NULL;
541	}
542
543	switch (ifp->if_format) {
544	case XFS_DINODE_FMT_LOCAL:
545		kfree(ifp->if_data);
546		ifp->if_data = NULL;
547		break;
548	case XFS_DINODE_FMT_EXTENTS:
549	case XFS_DINODE_FMT_BTREE:
550		if (ifp->if_height)
551			xfs_iext_destroy(ifp);
552		break;
553	}
554}
555
556/*
557 * Convert in-core extents to on-disk form
558 *
559 * In the case of the data fork, the in-core and on-disk fork sizes can be
560 * different due to delayed allocation extents. We only copy on-disk extents
561 * here, so callers must always use the physical fork size to determine the
562 * size of the buffer passed to this routine.  We will return the size actually
563 * used.
564 */
565int
566xfs_iextents_copy(
567	struct xfs_inode	*ip,
568	struct xfs_bmbt_rec	*dp,
569	int			whichfork)
570{
571	int			state = xfs_bmap_fork_to_state(whichfork);
572	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
573	struct xfs_iext_cursor	icur;
574	struct xfs_bmbt_irec	rec;
575	int64_t			copied = 0;
576
577	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED);
578	ASSERT(ifp->if_bytes > 0);
579
580	for_each_xfs_iext(ifp, &icur, &rec) {
581		if (isnullstartblock(rec.br_startblock))
582			continue;
583		ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
584		xfs_bmbt_disk_set_all(dp, &rec);
585		trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
586		copied += sizeof(struct xfs_bmbt_rec);
587		dp++;
588	}
589
590	ASSERT(copied > 0);
591	ASSERT(copied <= ifp->if_bytes);
592	return copied;
593}
594
595/*
596 * Each of the following cases stores data into the same region
597 * of the on-disk inode, so only one of them can be valid at
598 * any given time. While it is possible to have conflicting formats
599 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
600 * in EXTENTS format, this can only happen when the fork has
601 * changed formats after being modified but before being flushed.
602 * In these cases, the format always takes precedence, because the
603 * format indicates the current state of the fork.
604 */
605void
606xfs_iflush_fork(
607	struct xfs_inode	*ip,
608	struct xfs_dinode	*dip,
609	struct xfs_inode_log_item *iip,
610	int			whichfork)
611{
612	char			*cp;
613	struct xfs_ifork	*ifp;
614	xfs_mount_t		*mp;
615	static const short	brootflag[2] =
616		{ XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
617	static const short	dataflag[2] =
618		{ XFS_ILOG_DDATA, XFS_ILOG_ADATA };
619	static const short	extflag[2] =
620		{ XFS_ILOG_DEXT, XFS_ILOG_AEXT };
621
622	if (!iip)
623		return;
624	ifp = xfs_ifork_ptr(ip, whichfork);
625	/*
626	 * This can happen if we gave up in iformat in an error path,
627	 * for the attribute fork.
628	 */
629	if (!ifp) {
630		ASSERT(whichfork == XFS_ATTR_FORK);
631		return;
632	}
633	cp = XFS_DFORK_PTR(dip, whichfork);
634	mp = ip->i_mount;
635	switch (ifp->if_format) {
636	case XFS_DINODE_FMT_LOCAL:
637		if ((iip->ili_fields & dataflag[whichfork]) &&
638		    (ifp->if_bytes > 0)) {
639			ASSERT(ifp->if_data != NULL);
640			ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
641			memcpy(cp, ifp->if_data, ifp->if_bytes);
642		}
643		break;
644
645	case XFS_DINODE_FMT_EXTENTS:
646		if ((iip->ili_fields & extflag[whichfork]) &&
647		    (ifp->if_bytes > 0)) {
648			ASSERT(ifp->if_nextents > 0);
649			(void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
650				whichfork);
651		}
652		break;
653
654	case XFS_DINODE_FMT_BTREE:
655		if ((iip->ili_fields & brootflag[whichfork]) &&
656		    (ifp->if_broot_bytes > 0)) {
657			ASSERT(ifp->if_broot != NULL);
658			ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
659			        xfs_inode_fork_size(ip, whichfork));
660			xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
661				(xfs_bmdr_block_t *)cp,
662				XFS_DFORK_SIZE(dip, mp, whichfork));
663		}
664		break;
665
666	case XFS_DINODE_FMT_DEV:
667		if (iip->ili_fields & XFS_ILOG_DEV) {
668			ASSERT(whichfork == XFS_DATA_FORK);
669			xfs_dinode_put_rdev(dip,
670					linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
671		}
672		break;
673
674	default:
675		ASSERT(0);
676		break;
677	}
678}
679
680/* Convert bmap state flags to an inode fork. */
681struct xfs_ifork *
682xfs_iext_state_to_fork(
683	struct xfs_inode	*ip,
684	int			state)
685{
686	if (state & BMAP_COWFORK)
687		return ip->i_cowfp;
688	else if (state & BMAP_ATTRFORK)
689		return &ip->i_af;
690	return &ip->i_df;
691}
692
693/*
694 * Initialize an inode's copy-on-write fork.
695 */
696void
697xfs_ifork_init_cow(
698	struct xfs_inode	*ip)
699{
700	if (ip->i_cowfp)
701		return;
702
703	ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
704				GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
705	ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
706}
707
708/* Verify the inline contents of the data fork of an inode. */
709int
710xfs_ifork_verify_local_data(
711	struct xfs_inode	*ip)
712{
713	xfs_failaddr_t		fa = NULL;
714
715	switch (VFS_I(ip)->i_mode & S_IFMT) {
716	case S_IFDIR: {
717		struct xfs_mount	*mp = ip->i_mount;
718		struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
719		struct xfs_dir2_sf_hdr	*sfp = ifp->if_data;
720
721		fa = xfs_dir2_sf_verify(mp, sfp, ifp->if_bytes);
722		break;
723	}
724	case S_IFLNK: {
725		struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
726
727		fa = xfs_symlink_shortform_verify(ifp->if_data, ifp->if_bytes);
728		break;
729	}
730	default:
731		break;
732	}
733
734	if (fa) {
735		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
736				ip->i_df.if_data, ip->i_df.if_bytes, fa);
737		return -EFSCORRUPTED;
738	}
739
740	return 0;
741}
742
743/* Verify the inline contents of the attr fork of an inode. */
744int
745xfs_ifork_verify_local_attr(
746	struct xfs_inode	*ip)
747{
748	struct xfs_ifork	*ifp = &ip->i_af;
749	xfs_failaddr_t		fa;
750
751	if (!xfs_inode_has_attr_fork(ip)) {
752		fa = __this_address;
753	} else {
754		struct xfs_ifork		*ifp = &ip->i_af;
755
756		ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
757		fa = xfs_attr_shortform_verify(ifp->if_data, ifp->if_bytes);
758	}
759	if (fa) {
760		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
761				ifp->if_data, ifp->if_bytes, fa);
 
762		return -EFSCORRUPTED;
763	}
764
765	return 0;
766}
767
768int
769xfs_iext_count_may_overflow(
770	struct xfs_inode	*ip,
771	int			whichfork,
772	int			nr_to_add)
773{
774	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
775	uint64_t		max_exts;
776	uint64_t		nr_exts;
777
778	if (whichfork == XFS_COW_FORK)
779		return 0;
780
781	max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip),
782				whichfork);
783
784	if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
785		max_exts = 10;
786
787	nr_exts = ifp->if_nextents + nr_to_add;
788	if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
789		return -EFBIG;
790
791	return 0;
792}
793
794/*
795 * Upgrade this inode's extent counter fields to be able to handle a potential
796 * increase in the extent count by nr_to_add.  Normally this is the same
797 * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
798 */
799int
800xfs_iext_count_upgrade(
801	struct xfs_trans	*tp,
802	struct xfs_inode	*ip,
803	uint			nr_to_add)
804{
805	ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
806
807	if (!xfs_has_large_extent_counts(ip->i_mount) ||
808	    xfs_inode_has_large_extent_counts(ip) ||
809	    XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
810		return -EFBIG;
811
812	ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
813	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
814
815	return 0;
816}
817
818/* Decide if a file mapping is on the realtime device or not. */
819bool
820xfs_ifork_is_realtime(
821	struct xfs_inode	*ip,
822	int			whichfork)
823{
824	return XFS_IS_REALTIME_INODE(ip) && whichfork != XFS_ATTR_FORK;
825}
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4 * All Rights Reserved.
  5 */
  6
  7#include "xfs.h"
  8#include "xfs_fs.h"
  9#include "xfs_shared.h"
 10#include "xfs_format.h"
 11#include "xfs_log_format.h"
 12#include "xfs_trans_resv.h"
 13#include "xfs_mount.h"
 14#include "xfs_inode.h"
 15#include "xfs_trans.h"
 16#include "xfs_inode_item.h"
 17#include "xfs_btree.h"
 18#include "xfs_bmap_btree.h"
 19#include "xfs_bmap.h"
 20#include "xfs_error.h"
 21#include "xfs_trace.h"
 22#include "xfs_da_format.h"
 23#include "xfs_da_btree.h"
 24#include "xfs_dir2_priv.h"
 25#include "xfs_attr_leaf.h"
 26#include "xfs_types.h"
 27#include "xfs_errortag.h"
 
 
 28
 29kmem_zone_t *xfs_ifork_zone;
 30
 31void
 32xfs_init_local_fork(
 33	struct xfs_inode	*ip,
 34	int			whichfork,
 35	const void		*data,
 36	int64_t			size)
 37{
 38	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
 39	int			mem_size = size, real_size = 0;
 40	bool			zero_terminate;
 41
 42	/*
 43	 * If we are using the local fork to store a symlink body we need to
 44	 * zero-terminate it so that we can pass it back to the VFS directly.
 45	 * Overallocate the in-memory fork by one for that and add a zero
 46	 * to terminate it below.
 47	 */
 48	zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
 49	if (zero_terminate)
 50		mem_size++;
 51
 52	if (size) {
 53		real_size = roundup(mem_size, 4);
 54		ifp->if_u1.if_data = kmem_alloc(real_size, KM_NOFS);
 55		memcpy(ifp->if_u1.if_data, data, size);
 
 56		if (zero_terminate)
 57			ifp->if_u1.if_data[size] = '\0';
 
 
 58	} else {
 59		ifp->if_u1.if_data = NULL;
 60	}
 61
 62	ifp->if_bytes = size;
 63}
 64
 65/*
 66 * The file is in-lined in the on-disk inode.
 67 */
 68STATIC int
 69xfs_iformat_local(
 70	xfs_inode_t	*ip,
 71	xfs_dinode_t	*dip,
 72	int		whichfork,
 73	int		size)
 74{
 75	/*
 76	 * If the size is unreasonable, then something
 77	 * is wrong and we just bail out rather than crash in
 78	 * kmem_alloc() or memcpy() below.
 79	 */
 80	if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
 81		xfs_warn(ip->i_mount,
 82	"corrupt inode %Lu (bad size %d for local fork, size = %zd).",
 83			(unsigned long long) ip->i_ino, size,
 84			XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
 85		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
 86				"xfs_iformat_local", dip, sizeof(*dip),
 87				__this_address);
 
 88		return -EFSCORRUPTED;
 89	}
 90
 91	xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
 92	return 0;
 93}
 94
 95/*
 96 * The file consists of a set of extents all of which fit into the on-disk
 97 * inode.
 98 */
 99STATIC int
100xfs_iformat_extents(
101	struct xfs_inode	*ip,
102	struct xfs_dinode	*dip,
103	int			whichfork)
104{
105	struct xfs_mount	*mp = ip->i_mount;
106	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
107	int			state = xfs_bmap_fork_to_state(whichfork);
108	int			nex = XFS_DFORK_NEXTENTS(dip, whichfork);
109	int			size = nex * sizeof(xfs_bmbt_rec_t);
110	struct xfs_iext_cursor	icur;
111	struct xfs_bmbt_rec	*dp;
112	struct xfs_bmbt_irec	new;
113	int			i;
114
115	/*
116	 * If the number of extents is unreasonable, then something is wrong and
117	 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
118	 */
119	if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
120		xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
121			(unsigned long long) ip->i_ino, nex);
122		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
123				"xfs_iformat_extents(1)", dip, sizeof(*dip),
124				__this_address);
 
125		return -EFSCORRUPTED;
126	}
127
128	ifp->if_bytes = 0;
129	ifp->if_u1.if_root = NULL;
130	ifp->if_height = 0;
131	if (size) {
132		dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
133
134		xfs_iext_first(ifp, &icur);
135		for (i = 0; i < nex; i++, dp++) {
136			xfs_failaddr_t	fa;
137
138			xfs_bmbt_disk_get_all(dp, &new);
139			fa = xfs_bmap_validate_extent(ip, whichfork, &new);
140			if (fa) {
141				xfs_inode_verifier_error(ip, -EFSCORRUPTED,
142						"xfs_iformat_extents(2)",
143						dp, sizeof(*dp), fa);
144				return -EFSCORRUPTED;
 
 
145			}
146
147			xfs_iext_insert(ip, &icur, &new, state);
148			trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
149			xfs_iext_next(ifp, &icur);
150		}
151	}
152	return 0;
153}
154
155/*
156 * The file has too many extents to fit into
157 * the inode, so they are in B-tree format.
158 * Allocate a buffer for the root of the B-tree
159 * and copy the root into it.  The i_extents
160 * field will remain NULL until all of the
161 * extents are read in (when they are needed).
162 */
163STATIC int
164xfs_iformat_btree(
165	xfs_inode_t		*ip,
166	xfs_dinode_t		*dip,
167	int			whichfork)
168{
169	struct xfs_mount	*mp = ip->i_mount;
170	xfs_bmdr_block_t	*dfp;
171	struct xfs_ifork	*ifp;
172	/* REFERENCED */
173	int			nrecs;
174	int			size;
175	int			level;
176
177	ifp = XFS_IFORK_PTR(ip, whichfork);
178	dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
179	size = XFS_BMAP_BROOT_SPACE(mp, dfp);
180	nrecs = be16_to_cpu(dfp->bb_numrecs);
181	level = be16_to_cpu(dfp->bb_level);
182
183	/*
184	 * blow out if -- fork has less extents than can fit in
185	 * fork (fork shouldn't be a btree format), root btree
186	 * block has more records than can fit into the fork,
187	 * or the number of extents is greater than the number of
188	 * blocks.
189	 */
190	if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
191		     nrecs == 0 ||
192		     XFS_BMDR_SPACE_CALC(nrecs) >
193					XFS_DFORK_SIZE(dip, mp, whichfork) ||
194		     ifp->if_nextents > ip->i_nblocks) ||
195		     level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
196		xfs_warn(mp, "corrupt inode %Lu (btree).",
197					(unsigned long long) ip->i_ino);
198		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
199				"xfs_iformat_btree", dfp, size,
200				__this_address);
 
201		return -EFSCORRUPTED;
202	}
203
204	ifp->if_broot_bytes = size;
205	ifp->if_broot = kmem_alloc(size, KM_NOFS);
 
206	ASSERT(ifp->if_broot != NULL);
207	/*
208	 * Copy and convert from the on-disk structure
209	 * to the in-memory structure.
210	 */
211	xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
212			 ifp->if_broot, size);
213
214	ifp->if_bytes = 0;
215	ifp->if_u1.if_root = NULL;
216	ifp->if_height = 0;
217	return 0;
218}
219
220int
221xfs_iformat_data_fork(
222	struct xfs_inode	*ip,
223	struct xfs_dinode	*dip)
224{
225	struct inode		*inode = VFS_I(ip);
226	int			error;
227
228	/*
229	 * Initialize the extent count early, as the per-format routines may
230	 * depend on it.
 
 
 
231	 */
232	ip->i_df.if_format = dip->di_format;
233	ip->i_df.if_nextents = be32_to_cpu(dip->di_nextents);
 
 
234
235	switch (inode->i_mode & S_IFMT) {
236	case S_IFIFO:
237	case S_IFCHR:
238	case S_IFBLK:
239	case S_IFSOCK:
240		ip->i_disk_size = 0;
241		inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
242		return 0;
243	case S_IFREG:
244	case S_IFLNK:
245	case S_IFDIR:
246		switch (ip->i_df.if_format) {
247		case XFS_DINODE_FMT_LOCAL:
248			error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
249					be64_to_cpu(dip->di_size));
250			if (!error)
251				error = xfs_ifork_verify_local_data(ip);
252			return error;
253		case XFS_DINODE_FMT_EXTENTS:
254			return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
255		case XFS_DINODE_FMT_BTREE:
256			return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
257		default:
258			xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
259					dip, sizeof(*dip), __this_address);
 
260			return -EFSCORRUPTED;
261		}
262		break;
263	default:
264		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
265				sizeof(*dip), __this_address);
 
266		return -EFSCORRUPTED;
267	}
268}
269
270static uint16_t
271xfs_dfork_attr_shortform_size(
272	struct xfs_dinode		*dip)
273{
274	struct xfs_attr_shortform	*atp =
275		(struct xfs_attr_shortform *)XFS_DFORK_APTR(dip);
276
277	return be16_to_cpu(atp->hdr.totsize);
278}
279
280struct xfs_ifork *
281xfs_ifork_alloc(
 
282	enum xfs_dinode_fmt	format,
283	xfs_extnum_t		nextents)
284{
285	struct xfs_ifork	*ifp;
 
 
 
 
 
 
 
 
 
 
 
286
287	ifp = kmem_cache_zalloc(xfs_ifork_zone, GFP_NOFS | __GFP_NOFAIL);
288	ifp->if_format = format;
289	ifp->if_nextents = nextents;
290	return ifp;
 
 
 
291}
292
293int
294xfs_iformat_attr_fork(
295	struct xfs_inode	*ip,
296	struct xfs_dinode	*dip)
297{
 
298	int			error = 0;
299
300	/*
301	 * Initialize the extent count early, as the per-format routines may
302	 * depend on it.
303	 */
304	ip->i_afp = xfs_ifork_alloc(dip->di_aformat,
305				be16_to_cpu(dip->di_anextents));
306
307	switch (ip->i_afp->if_format) {
308	case XFS_DINODE_FMT_LOCAL:
309		error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
310				xfs_dfork_attr_shortform_size(dip));
311		if (!error)
312			error = xfs_ifork_verify_local_attr(ip);
313		break;
314	case XFS_DINODE_FMT_EXTENTS:
315		error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
316		break;
317	case XFS_DINODE_FMT_BTREE:
318		error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
319		break;
320	default:
321		xfs_inode_verifier_error(ip, error, __func__, dip,
322				sizeof(*dip), __this_address);
 
323		error = -EFSCORRUPTED;
324		break;
325	}
326
327	if (error) {
328		kmem_cache_free(xfs_ifork_zone, ip->i_afp);
329		ip->i_afp = NULL;
330	}
331	return error;
332}
333
334/*
335 * Reallocate the space for if_broot based on the number of records
336 * being added or deleted as indicated in rec_diff.  Move the records
337 * and pointers in if_broot to fit the new size.  When shrinking this
338 * will eliminate holes between the records and pointers created by
339 * the caller.  When growing this will create holes to be filled in
340 * by the caller.
341 *
342 * The caller must not request to add more records than would fit in
343 * the on-disk inode root.  If the if_broot is currently NULL, then
344 * if we are adding records, one will be allocated.  The caller must also
345 * not request that the number of records go below zero, although
346 * it can go to zero.
347 *
348 * ip -- the inode whose if_broot area is changing
349 * ext_diff -- the change in the number of records, positive or negative,
350 *	 requested for the if_broot array.
351 */
352void
353xfs_iroot_realloc(
354	xfs_inode_t		*ip,
355	int			rec_diff,
356	int			whichfork)
357{
358	struct xfs_mount	*mp = ip->i_mount;
359	int			cur_max;
360	struct xfs_ifork	*ifp;
361	struct xfs_btree_block	*new_broot;
362	int			new_max;
363	size_t			new_size;
364	char			*np;
365	char			*op;
366
367	/*
368	 * Handle the degenerate case quietly.
369	 */
370	if (rec_diff == 0) {
371		return;
372	}
373
374	ifp = XFS_IFORK_PTR(ip, whichfork);
375	if (rec_diff > 0) {
376		/*
377		 * If there wasn't any memory allocated before, just
378		 * allocate it now and get out.
379		 */
380		if (ifp->if_broot_bytes == 0) {
381			new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
382			ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
 
383			ifp->if_broot_bytes = (int)new_size;
384			return;
385		}
386
387		/*
388		 * If there is already an existing if_broot, then we need
389		 * to realloc() it and shift the pointers to their new
390		 * location.  The records don't change location because
391		 * they are kept butted up against the btree block header.
392		 */
393		cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
394		new_max = cur_max + rec_diff;
395		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
396		ifp->if_broot = krealloc(ifp->if_broot, new_size,
397					 GFP_NOFS | __GFP_NOFAIL);
398		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
399						     ifp->if_broot_bytes);
400		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
401						     (int)new_size);
402		ifp->if_broot_bytes = (int)new_size;
403		ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
404			XFS_IFORK_SIZE(ip, whichfork));
405		memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
406		return;
407	}
408
409	/*
410	 * rec_diff is less than 0.  In this case, we are shrinking the
411	 * if_broot buffer.  It must already exist.  If we go to zero
412	 * records, just get rid of the root and clear the status bit.
413	 */
414	ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
415	cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
416	new_max = cur_max + rec_diff;
417	ASSERT(new_max >= 0);
418	if (new_max > 0)
419		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
420	else
421		new_size = 0;
422	if (new_size > 0) {
423		new_broot = kmem_alloc(new_size, KM_NOFS);
424		/*
425		 * First copy over the btree block header.
426		 */
427		memcpy(new_broot, ifp->if_broot,
428			XFS_BMBT_BLOCK_LEN(ip->i_mount));
429	} else {
430		new_broot = NULL;
431	}
432
433	/*
434	 * Only copy the records and pointers if there are any.
435	 */
436	if (new_max > 0) {
437		/*
438		 * First copy the records.
439		 */
440		op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
441		np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
442		memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
443
444		/*
445		 * Then copy the pointers.
446		 */
447		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
448						     ifp->if_broot_bytes);
449		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
450						     (int)new_size);
451		memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
452	}
453	kmem_free(ifp->if_broot);
454	ifp->if_broot = new_broot;
455	ifp->if_broot_bytes = (int)new_size;
456	if (ifp->if_broot)
457		ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
458			XFS_IFORK_SIZE(ip, whichfork));
459	return;
460}
461
462
463/*
464 * This is called when the amount of space needed for if_data
465 * is increased or decreased.  The change in size is indicated by
466 * the number of bytes that need to be added or deleted in the
467 * byte_diff parameter.
468 *
469 * If the amount of space needed has decreased below the size of the
470 * inline buffer, then switch to using the inline buffer.  Otherwise,
471 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
472 * to what is needed.
473 *
474 * ip -- the inode whose if_data area is changing
475 * byte_diff -- the change in the number of bytes, positive or negative,
476 *	 requested for the if_data array.
477 */
478void
479xfs_idata_realloc(
480	struct xfs_inode	*ip,
481	int64_t			byte_diff,
482	int			whichfork)
483{
484	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
485	int64_t			new_size = ifp->if_bytes + byte_diff;
486
487	ASSERT(new_size >= 0);
488	ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
489
490	if (byte_diff == 0)
491		return;
492
493	if (new_size == 0) {
494		kmem_free(ifp->if_u1.if_data);
495		ifp->if_u1.if_data = NULL;
496		ifp->if_bytes = 0;
497		return;
498	}
499
500	/*
501	 * For inline data, the underlying buffer must be a multiple of 4 bytes
502	 * in size so that it can be logged and stay on word boundaries.
503	 * We enforce that here.
504	 */
505	ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
506				      GFP_NOFS | __GFP_NOFAIL);
507	ifp->if_bytes = new_size;
508}
509
 
510void
511xfs_idestroy_fork(
512	struct xfs_ifork	*ifp)
513{
514	if (ifp->if_broot != NULL) {
515		kmem_free(ifp->if_broot);
516		ifp->if_broot = NULL;
517	}
518
519	switch (ifp->if_format) {
520	case XFS_DINODE_FMT_LOCAL:
521		kmem_free(ifp->if_u1.if_data);
522		ifp->if_u1.if_data = NULL;
523		break;
524	case XFS_DINODE_FMT_EXTENTS:
525	case XFS_DINODE_FMT_BTREE:
526		if (ifp->if_height)
527			xfs_iext_destroy(ifp);
528		break;
529	}
530}
531
532/*
533 * Convert in-core extents to on-disk form
534 *
535 * In the case of the data fork, the in-core and on-disk fork sizes can be
536 * different due to delayed allocation extents. We only copy on-disk extents
537 * here, so callers must always use the physical fork size to determine the
538 * size of the buffer passed to this routine.  We will return the size actually
539 * used.
540 */
541int
542xfs_iextents_copy(
543	struct xfs_inode	*ip,
544	struct xfs_bmbt_rec	*dp,
545	int			whichfork)
546{
547	int			state = xfs_bmap_fork_to_state(whichfork);
548	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
549	struct xfs_iext_cursor	icur;
550	struct xfs_bmbt_irec	rec;
551	int64_t			copied = 0;
552
553	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
554	ASSERT(ifp->if_bytes > 0);
555
556	for_each_xfs_iext(ifp, &icur, &rec) {
557		if (isnullstartblock(rec.br_startblock))
558			continue;
559		ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
560		xfs_bmbt_disk_set_all(dp, &rec);
561		trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
562		copied += sizeof(struct xfs_bmbt_rec);
563		dp++;
564	}
565
566	ASSERT(copied > 0);
567	ASSERT(copied <= ifp->if_bytes);
568	return copied;
569}
570
571/*
572 * Each of the following cases stores data into the same region
573 * of the on-disk inode, so only one of them can be valid at
574 * any given time. While it is possible to have conflicting formats
575 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
576 * in EXTENTS format, this can only happen when the fork has
577 * changed formats after being modified but before being flushed.
578 * In these cases, the format always takes precedence, because the
579 * format indicates the current state of the fork.
580 */
581void
582xfs_iflush_fork(
583	xfs_inode_t		*ip,
584	xfs_dinode_t		*dip,
585	struct xfs_inode_log_item *iip,
586	int			whichfork)
587{
588	char			*cp;
589	struct xfs_ifork	*ifp;
590	xfs_mount_t		*mp;
591	static const short	brootflag[2] =
592		{ XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
593	static const short	dataflag[2] =
594		{ XFS_ILOG_DDATA, XFS_ILOG_ADATA };
595	static const short	extflag[2] =
596		{ XFS_ILOG_DEXT, XFS_ILOG_AEXT };
597
598	if (!iip)
599		return;
600	ifp = XFS_IFORK_PTR(ip, whichfork);
601	/*
602	 * This can happen if we gave up in iformat in an error path,
603	 * for the attribute fork.
604	 */
605	if (!ifp) {
606		ASSERT(whichfork == XFS_ATTR_FORK);
607		return;
608	}
609	cp = XFS_DFORK_PTR(dip, whichfork);
610	mp = ip->i_mount;
611	switch (ifp->if_format) {
612	case XFS_DINODE_FMT_LOCAL:
613		if ((iip->ili_fields & dataflag[whichfork]) &&
614		    (ifp->if_bytes > 0)) {
615			ASSERT(ifp->if_u1.if_data != NULL);
616			ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
617			memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
618		}
619		break;
620
621	case XFS_DINODE_FMT_EXTENTS:
622		if ((iip->ili_fields & extflag[whichfork]) &&
623		    (ifp->if_bytes > 0)) {
624			ASSERT(ifp->if_nextents > 0);
625			(void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
626				whichfork);
627		}
628		break;
629
630	case XFS_DINODE_FMT_BTREE:
631		if ((iip->ili_fields & brootflag[whichfork]) &&
632		    (ifp->if_broot_bytes > 0)) {
633			ASSERT(ifp->if_broot != NULL);
634			ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
635			        XFS_IFORK_SIZE(ip, whichfork));
636			xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
637				(xfs_bmdr_block_t *)cp,
638				XFS_DFORK_SIZE(dip, mp, whichfork));
639		}
640		break;
641
642	case XFS_DINODE_FMT_DEV:
643		if (iip->ili_fields & XFS_ILOG_DEV) {
644			ASSERT(whichfork == XFS_DATA_FORK);
645			xfs_dinode_put_rdev(dip,
646					linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
647		}
648		break;
649
650	default:
651		ASSERT(0);
652		break;
653	}
654}
655
656/* Convert bmap state flags to an inode fork. */
657struct xfs_ifork *
658xfs_iext_state_to_fork(
659	struct xfs_inode	*ip,
660	int			state)
661{
662	if (state & BMAP_COWFORK)
663		return ip->i_cowfp;
664	else if (state & BMAP_ATTRFORK)
665		return ip->i_afp;
666	return &ip->i_df;
667}
668
669/*
670 * Initialize an inode's copy-on-write fork.
671 */
672void
673xfs_ifork_init_cow(
674	struct xfs_inode	*ip)
675{
676	if (ip->i_cowfp)
677		return;
678
679	ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_zone,
680				       GFP_NOFS | __GFP_NOFAIL);
681	ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
682}
683
684/* Verify the inline contents of the data fork of an inode. */
685int
686xfs_ifork_verify_local_data(
687	struct xfs_inode	*ip)
688{
689	xfs_failaddr_t		fa = NULL;
690
691	switch (VFS_I(ip)->i_mode & S_IFMT) {
692	case S_IFDIR:
693		fa = xfs_dir2_sf_verify(ip);
 
 
 
 
694		break;
695	case S_IFLNK:
696		fa = xfs_symlink_shortform_verify(ip);
 
 
 
697		break;
 
698	default:
699		break;
700	}
701
702	if (fa) {
703		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
704				ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa);
705		return -EFSCORRUPTED;
706	}
707
708	return 0;
709}
710
711/* Verify the inline contents of the attr fork of an inode. */
712int
713xfs_ifork_verify_local_attr(
714	struct xfs_inode	*ip)
715{
716	struct xfs_ifork	*ifp = ip->i_afp;
717	xfs_failaddr_t		fa;
718
719	if (!ifp)
720		fa = __this_address;
721	else
722		fa = xfs_attr_shortform_verify(ip);
723
 
 
 
724	if (fa) {
725		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
726				ifp ? ifp->if_u1.if_data : NULL,
727				ifp ? ifp->if_bytes : 0, fa);
728		return -EFSCORRUPTED;
729	}
730
731	return 0;
732}
733
734int
735xfs_iext_count_may_overflow(
736	struct xfs_inode	*ip,
737	int			whichfork,
738	int			nr_to_add)
739{
740	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
741	uint64_t		max_exts;
742	uint64_t		nr_exts;
743
744	if (whichfork == XFS_COW_FORK)
745		return 0;
746
747	max_exts = (whichfork == XFS_ATTR_FORK) ? MAXAEXTNUM : MAXEXTNUM;
 
748
749	if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
750		max_exts = 10;
751
752	nr_exts = ifp->if_nextents + nr_to_add;
753	if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
754		return -EFBIG;
755
756	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
757}