Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * inode.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * PURPOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Inode handling routines for the OSTA-UDF(tm) filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *  This file is distributed under the terms of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  License (GPL). Copies of the GPL can be obtained from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *    ftp://prep.ai.mit.edu/pub/gnu/GPL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *  Each contributing author retains all rights to their own work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *  (C) 1998 Dave Boynton
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *  (C) 1998-2004 Ben Fennema
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *  (C) 1999-2000 Stelias Computing Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * HISTORY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *  10/04/98 dgb  Added rudimentary directory functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *  10/07/98      Fully working udf_block_map! It works!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *  11/25/98      bmap altered to better support extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *  12/06/98 blf  partition support in udf_iget, udf_block_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *                and udf_read_inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  *  12/12/98      rewrote udf_block_map to handle next extents and descs across
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *                block boundaries (which is not actually allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  *  12/20/98      added support for strategy 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  *  03/07/99      rewrote udf_block_map (again)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *                New funcs, inode_bmap, udf_next_aext
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  *  04/19/99      Support for writing device EA's for major/minor #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "udfdecl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/crc-itu-t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/mpage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include "udf_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include "udf_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define EXTENT_MERGE_SIZE 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #define FE_MAPPED_PERMS	(FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 			 FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 			 FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define FE_DELETE_PERMS	(FE_PERM_U_DELETE | FE_PERM_G_DELETE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 			 FE_PERM_O_DELETE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static umode_t udf_convert_permissions(struct fileEntry *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) static int udf_update_inode(struct inode *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) static int udf_sync_inode(struct inode *inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) static int udf_alloc_i_data(struct inode *inode, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) static int8_t udf_insert_aext(struct inode *, struct extent_position,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 			      struct kernel_lb_addr, uint32_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 			      struct kernel_long_ad *, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static void udf_prealloc_extents(struct inode *, int, int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 				 struct kernel_long_ad *, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 			       int, struct extent_position *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) static void __udf_clear_extent_cache(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	if (iinfo->cached_extent.lstart != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		brelse(iinfo->cached_extent.epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		iinfo->cached_extent.lstart = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) /* Invalidate extent cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) static void udf_clear_extent_cache(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	spin_lock(&iinfo->i_extent_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	__udf_clear_extent_cache(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	spin_unlock(&iinfo->i_extent_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) /* Return contents of extent cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 				 loff_t *lbcount, struct extent_position *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	spin_lock(&iinfo->i_extent_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	if ((iinfo->cached_extent.lstart <= bcount) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	    (iinfo->cached_extent.lstart != -1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		/* Cache hit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		*lbcount = iinfo->cached_extent.lstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		memcpy(pos, &iinfo->cached_extent.epos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		       sizeof(struct extent_position));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		if (pos->bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 			get_bh(pos->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	spin_unlock(&iinfo->i_extent_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) /* Add extent to extent cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) static void udf_update_extent_cache(struct inode *inode, loff_t estart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 				    struct extent_position *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	spin_lock(&iinfo->i_extent_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	/* Invalidate previously cached extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	__udf_clear_extent_cache(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	if (pos->bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		get_bh(pos->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	iinfo->cached_extent.lstart = estart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	switch (iinfo->i_alloc_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	case ICBTAG_FLAG_AD_SHORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	case ICBTAG_FLAG_AD_LONG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	spin_unlock(&iinfo->i_extent_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) void udf_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	int want_delete = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (!is_bad_inode(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		if (!inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 			want_delete = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 			udf_setsize(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 			udf_update_inode(inode, IS_SYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 		    inode->i_size != iinfo->i_lenExtents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 			udf_warn(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 				 "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 				 inode->i_ino, inode->i_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 				 (unsigned long long)inode->i_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 				 (unsigned long long)iinfo->i_lenExtents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	invalidate_inode_buffers(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	kfree(iinfo->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	iinfo->i_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	udf_clear_extent_cache(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	if (want_delete) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		udf_free_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) static void udf_write_failed(struct address_space *mapping, loff_t to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	loff_t isize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	if (to > isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		truncate_pagecache(inode, isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 			down_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 			udf_clear_extent_cache(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			udf_truncate_extents(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) static int udf_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	return block_write_full_page(page, udf_get_block, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) static int udf_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 			struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	return mpage_writepages(mapping, wbc, udf_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static int udf_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	return mpage_readpage(page, udf_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) static void udf_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	mpage_readahead(rac, udf_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) static int udf_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		udf_write_failed(mapping, pos + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	struct address_space *mapping = file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	size_t count = iov_iter_count(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		udf_write_failed(mapping, iocb->ki_pos + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) static sector_t udf_bmap(struct address_space *mapping, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	return generic_block_bmap(mapping, block, udf_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) const struct address_space_operations udf_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	.readpage	= udf_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	.readahead	= udf_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	.writepage	= udf_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	.writepages	= udf_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	.write_begin	= udf_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	.write_end	= generic_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	.direct_IO	= udf_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	.bmap		= udf_bmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  * Expand file stored in ICB to a normal one-block-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251)  * This function requires i_data_sem for writing and releases it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252)  * This function requires i_mutex held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) int udf_expand_file_adinicb(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	WARN_ON_ONCE(!inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	if (!iinfo->i_lenAlloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		/* from now on we have normal address_space methods */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		inode->i_data.a_ops = &udf_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	 * Release i_data_sem so that we can lock a page - page lock ranks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	 * above i_data_sem. i_mutex still protects us against file changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		kaddr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		memset(kaddr + iinfo->i_lenAlloc, 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		       PAGE_SIZE - iinfo->i_lenAlloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			iinfo->i_lenAlloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	down_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	       iinfo->i_lenAlloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	iinfo->i_lenAlloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	/* from now on we have normal address_space methods */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	inode->i_data.a_ops = &udf_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	set_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	err = filemap_fdatawrite(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		/* Restore everything back so that we don't lose data... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		down_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		kaddr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		inode->i_data.a_ops = &udf_adinicb_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		iinfo->i_lenAlloc = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) struct buffer_head *udf_expand_dir_adinicb(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 					    udf_pblk_t *block, int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	udf_pblk_t newblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	struct buffer_head *dbh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	struct kernel_lb_addr eloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	uint8_t alloctype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	struct extent_position epos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	struct udf_fileident_bh sfibh, dfibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	loff_t f_pos = udf_ext0_offset(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	int size = udf_ext0_offset(inode) + inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	struct fileIdentDesc cfi, *sfi, *dfi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		alloctype = ICBTAG_FLAG_AD_SHORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		alloctype = ICBTAG_FLAG_AD_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	if (!inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		iinfo->i_alloc_type = alloctype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	/* alloc block, and copy data to it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	*block = udf_new_block(inode->i_sb, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 			       iinfo->i_location.partitionReferenceNum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			       iinfo->i_location.logicalBlockNum, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	if (!(*block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	newblock = udf_get_pblock(inode->i_sb, *block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				  iinfo->i_location.partitionReferenceNum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 				0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	if (!newblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	dbh = udf_tgetblk(inode->i_sb, newblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	if (!dbh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	lock_buffer(dbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	set_buffer_uptodate(dbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	unlock_buffer(dbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	mark_buffer_dirty_inode(dbh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	sfibh.soffset = sfibh.eoffset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			f_pos & (inode->i_sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	sfibh.sbh = sfibh.ebh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	dfibh.soffset = dfibh.eoffset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	dfibh.sbh = dfibh.ebh = dbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	while (f_pos < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 					 NULL, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		if (!sfi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 			brelse(dbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		iinfo->i_alloc_type = alloctype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		sfi->descTag.tagLocation = cpu_to_le32(*block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		dfibh.soffset = dfibh.eoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 				 sfi->fileIdent +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 					le16_to_cpu(sfi->lengthOfImpUse))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			brelse(dbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	mark_buffer_dirty_inode(dbh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	memset(iinfo->i_data + iinfo->i_lenEAttr, 0, iinfo->i_lenAlloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	iinfo->i_lenAlloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	eloc.logicalBlockNum = *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	eloc.partitionReferenceNum =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 				iinfo->i_location.partitionReferenceNum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	iinfo->i_lenExtents = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	epos.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	epos.block = iinfo->i_location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	epos.offset = udf_file_entry_alloc_offset(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	/* UniqueID stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	brelse(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	return dbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) static int udf_get_block(struct inode *inode, sector_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			 struct buffer_head *bh_result, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	int err, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	sector_t phys = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	struct udf_inode_info *iinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (!create) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		phys = udf_block_map(inode, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		if (phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			map_bh(bh_result, inode->i_sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	new = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	down_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	if (block == iinfo->i_next_alloc_block + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		iinfo->i_next_alloc_block++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		iinfo->i_next_alloc_goal++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	udf_clear_extent_cache(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	phys = inode_getblk(inode, block, &err, &new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	if (!phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	if (new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		set_buffer_new(bh_result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	map_bh(bh_result, inode->i_sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) abort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) static struct buffer_head *udf_getblk(struct inode *inode, udf_pblk_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 				      int create, int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	struct buffer_head dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	dummy.b_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	dummy.b_blocknr = -1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	*err = udf_get_block(inode, block, &dummy, create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	if (!*err && buffer_mapped(&dummy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		if (buffer_new(&dummy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 			lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 			unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) /* Extend the file with new blocks totaling 'new_block_bytes',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480)  * return the number of extents added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) static int udf_do_extend_file(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			      struct extent_position *last_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 			      struct kernel_long_ad *last_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			      loff_t new_block_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	uint32_t add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	struct kernel_lb_addr prealloc_loc = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	uint32_t prealloc_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	struct udf_inode_info *iinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	/* The previous extent is fake and we should not extend by anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	 * - there's nothing to do... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (!new_block_bytes && fake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	/* Round the last extent up to a multiple of block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	if (last_ext->extLength & (sb->s_blocksize - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		last_ext->extLength =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 			(last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			(((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			  sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		iinfo->i_lenExtents =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			(iinfo->i_lenExtents + sb->s_blocksize - 1) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			~(sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	/* Last extent are just preallocated blocks? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 						EXT_NOT_RECORDED_ALLOCATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		/* Save the extent so that we can reattach it to the end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		prealloc_loc = last_ext->extLocation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		prealloc_len = last_ext->extLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		/* Mark the extent as a hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		last_ext->extLocation.logicalBlockNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		last_ext->extLocation.partitionReferenceNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	/* Can we merge with the previous extent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 					EXT_NOT_RECORDED_NOT_ALLOCATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		add = (1 << 30) - sb->s_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 			(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		if (add > new_block_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 			add = new_block_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		new_block_bytes -= add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		last_ext->extLength += add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (fake) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		udf_add_aext(inode, last_pos, &last_ext->extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 			     last_ext->extLength, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		struct kernel_lb_addr tmploc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		uint32_t tmplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		udf_write_aext(inode, last_pos, &last_ext->extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 				last_ext->extLength, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		 * We've rewritten the last extent. If we are going to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		 * more extents, we may need to enter possible following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		 * empty indirect extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		if (new_block_bytes || prealloc_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	/* Managed to do everything necessary? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	if (!new_block_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	/* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	last_ext->extLocation.logicalBlockNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	last_ext->extLocation.partitionReferenceNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	add = (1 << 30) - sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	/* Create enough extents to cover the whole hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	while (new_block_bytes > add) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		new_block_bytes -= add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 				   last_ext->extLength, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	if (new_block_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 			new_block_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				   last_ext->extLength, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	/* Do we have some preallocated blocks saved? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	if (prealloc_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		err = udf_add_aext(inode, last_pos, &prealloc_loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 				   prealloc_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		last_ext->extLocation = prealloc_loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		last_ext->extLength = prealloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	/* last_pos should point to the last written extent... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		last_pos->offset -= sizeof(struct short_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		last_pos->offset -= sizeof(struct long_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) /* Extend the final block of the file to final_block_len bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) static void udf_do_extend_final_block(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 				      struct extent_position *last_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 				      struct kernel_long_ad *last_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 				      uint32_t final_block_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	uint32_t added_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	added_bytes = final_block_len -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		      (last_ext->extLength & (sb->s_blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	last_ext->extLength += added_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	UDF_I(inode)->i_lenExtents += added_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	udf_write_aext(inode, last_pos, &last_ext->extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			last_ext->extLength, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) static int udf_extend_file(struct inode *inode, loff_t newsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	struct extent_position epos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	struct kernel_lb_addr eloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	uint32_t elen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	int8_t etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	unsigned long partial_final_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	int adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	struct kernel_long_ad extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	int within_final_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		adsize = sizeof(struct short_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		adsize = sizeof(struct long_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	within_final_block = (etype != -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	    (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		/* File has no extents at all or has empty last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		 * indirect extent! Create a fake extent... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		extent.extLocation.logicalBlockNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		extent.extLocation.partitionReferenceNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		epos.offset -= adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		etype = udf_next_aext(inode, &epos, &extent.extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 				      &extent.extLength, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		extent.extLength |= etype << 30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	partial_final_block = newsize & (sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	/* File has extent covering the new size (could happen when extending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	 * inside a block)?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	if (within_final_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		/* Extending file within the last file block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		udf_do_extend_final_block(inode, &epos, &extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 					  partial_final_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			     partial_final_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		err = udf_do_extend_file(inode, &epos, &extent, add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	iinfo->i_lenExtents = newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	brelse(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) static sector_t inode_getblk(struct inode *inode, sector_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			     int *err, int *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct extent_position prev_epos, cur_epos, next_epos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	int count = 0, startnum = 0, endnum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	uint32_t elen = 0, tmpelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	struct kernel_lb_addr eloc, tmpeloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	int c = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	loff_t lbcount = 0, b_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	udf_pblk_t newblocknum, newblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	sector_t offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	int8_t etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	int lastblock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	bool isBeyondEOF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	*err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	*new = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	prev_epos.offset = udf_file_entry_alloc_offset(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	prev_epos.block = iinfo->i_location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	prev_epos.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	cur_epos = next_epos = prev_epos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	/* find the extent which contains the block we are looking for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	   alternate between laarr[0] and laarr[1] for locations of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	   current extent, and the previous extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		if (prev_epos.bh != cur_epos.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 			brelse(prev_epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 			get_bh(cur_epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			prev_epos.bh = cur_epos.bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		if (cur_epos.bh != next_epos.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			brelse(cur_epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			get_bh(next_epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			cur_epos.bh = next_epos.bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		lbcount += elen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		prev_epos.block = cur_epos.block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		cur_epos.block = next_epos.block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		prev_epos.offset = cur_epos.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		cur_epos.offset = next_epos.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		if (etype == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		c = !c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		laarr[c].extLength = (etype << 30) | elen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		laarr[c].extLocation = eloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			pgoal = eloc.logicalBlockNum +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 				((elen + inode->i_sb->s_blocksize - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 				 inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	} while (lbcount + elen <= b_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	b_off -= lbcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	offset = b_off >> inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	 * Move prev_epos and cur_epos into indirect extent if we are at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	 * the pointer to it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	/* if the extent is allocated and recorded, return the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	   if the extent is not a multiple of the blocksize, round up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		if (elen & (inode->i_sb->s_blocksize - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			elen = EXT_RECORDED_ALLOCATED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 				((elen + inode->i_sb->s_blocksize - 1) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 				 ~(inode->i_sb->s_blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 			udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	/* Are we beyond EOF? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (etype == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		loff_t hole_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		isBeyondEOF = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		if (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 				laarr[0] = laarr[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			startnum = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			/* Create a fake extent when there's not one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			memset(&laarr[0].extLocation, 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 				sizeof(struct kernel_lb_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			/* Will udf_do_extend_file() create real extent from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 			   a fake one? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			startnum = (offset > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		/* Create extents for the hole between EOF and offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		hole_len = (loff_t)offset << inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			*err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			newblock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		c = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		count += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		/* We are not covered by a preallocated extent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 						EXT_NOT_RECORDED_ALLOCATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			/* Is there any real extent? - otherwise we overwrite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 			 * the fake one... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			if (count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 				c = !c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 				inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			memset(&laarr[c].extLocation, 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 				sizeof(struct kernel_lb_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		endnum = c + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		lastblock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		isBeyondEOF = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		endnum = startnum = ((count > 2) ? 2 : count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		/* if the current extent is in position 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		   swap it with the previous */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		if (!c && count != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			laarr[2] = laarr[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			laarr[0] = laarr[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			laarr[1] = laarr[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			c = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		/* if the current block is located in an extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		   read the next extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		if (etype != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			laarr[c + 1].extLength = (etype << 30) | elen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			laarr[c + 1].extLocation = eloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 			startnum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			endnum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			lastblock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	/* if the current extent is not recorded but allocated, get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	 * block in the extent corresponding to the requested block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	else { /* otherwise, allocate a new block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		if (iinfo->i_next_alloc_block == block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			goal = iinfo->i_next_alloc_goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		if (!goal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			if (!(goal = pgoal)) /* XXX: what was intended here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 				goal = iinfo->i_location.logicalBlockNum + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		newblocknum = udf_new_block(inode->i_sb, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 				iinfo->i_location.partitionReferenceNum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 				goal, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		if (!newblocknum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			*err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			newblock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		if (isBeyondEOF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			iinfo->i_lenExtents += inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	/* if the extent the requsted block is located in contains multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	 * blocks, split the extent into at most three extents. blocks prior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	 * to requested block, requested block, and blocks after requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	 * block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	/* We preallocate blocks only for regular files. It also makes sense
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	 * for directories but there's a problem when to drop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	 * preallocation. We might use some delayed work for that but I feel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	 * it's overengineering for a filesystem like UDF. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	/* merge any continuous blocks in laarr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	udf_merge_extents(inode, laarr, &endnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	/* write back the new extents, inserting new extents if the new number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	 * of extents is greater than the old number, and deleting extents if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	 * the new number of extents is less than the old number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	newblock = udf_get_pblock(inode->i_sb, newblocknum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 				iinfo->i_location.partitionReferenceNum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	if (!newblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		*err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	*new = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	iinfo->i_next_alloc_block = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	iinfo->i_next_alloc_goal = newblocknum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	if (IS_SYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		udf_sync_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	brelse(prev_epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	brelse(cur_epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	brelse(next_epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	return newblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) static void udf_split_extents(struct inode *inode, int *c, int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			       udf_pblk_t newblocknum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			       struct kernel_long_ad *laarr, int *endnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	unsigned long blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	    (laarr[*c].extLength >> 30) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		int curr = *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			    blocksize - 1) >> blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		int8_t etype = (laarr[curr].extLength >> 30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		if (blen == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		else if (!offset || blen == offset + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			laarr[curr + 2] = laarr[curr + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			laarr[curr + 1] = laarr[curr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			laarr[curr + 3] = laarr[curr + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 				udf_free_blocks(inode->i_sb, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 						&laarr[curr].extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 						0, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 				laarr[curr].extLength =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 					EXT_NOT_RECORDED_NOT_ALLOCATED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 					(offset << blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 				laarr[curr].extLocation.logicalBlockNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 				laarr[curr].extLocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 						partitionReferenceNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 				laarr[curr].extLength = (etype << 30) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 					(offset << blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			curr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			(*c)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			(*endnum)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		laarr[curr].extLocation.logicalBlockNum = newblocknum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			laarr[curr].extLocation.partitionReferenceNum =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 				UDF_I(inode)->i_location.partitionReferenceNum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		curr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		if (blen != offset + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 				laarr[curr].extLocation.logicalBlockNum +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 								offset + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			laarr[curr].extLength = (etype << 30) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 				((blen - (offset + 1)) << blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			curr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			(*endnum)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 				 struct kernel_long_ad *laarr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 				 int *endnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	int start, length = 0, currlength = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (*endnum >= (c + 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		if (!lastblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			start = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		if ((laarr[c + 1].extLength >> 30) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			start = c + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			length = currlength =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 				(((laarr[c + 1].extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 					UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 				inode->i_sb->s_blocksize - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 				inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			start = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	for (i = start + 1; i <= *endnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		if (i == *endnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			if (lastblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 				length += UDF_DEFAULT_PREALLOC_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		} else if ((laarr[i].extLength >> 30) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			length += (((laarr[i].extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 						UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 				    inode->i_sb->s_blocksize - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 				    inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	if (length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		int next = laarr[start].extLocation.logicalBlockNum +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 			(((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			  inode->i_sb->s_blocksize - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			  inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 				laarr[start].extLocation.partitionReferenceNum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 				next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 				length : UDF_DEFAULT_PREALLOC_BLOCKS) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 				currlength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		if (numalloc) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 			if (start == (c + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 				laarr[start].extLength +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 					(numalloc <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 					 inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 				memmove(&laarr[c + 2], &laarr[c + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 					sizeof(struct long_ad) * (*endnum - (c + 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				(*endnum)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 				laarr[c + 1].extLocation.logicalBlockNum = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 				laarr[c + 1].extLocation.partitionReferenceNum =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 					laarr[c].extLocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 							partitionReferenceNum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 				laarr[c + 1].extLength =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 					EXT_NOT_RECORDED_ALLOCATED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 					(numalloc <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 					 inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 				start = c + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			for (i = start + 1; numalloc && i < *endnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 				int elen = ((laarr[i].extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 						UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 					    inode->i_sb->s_blocksize - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 					    inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				if (elen > numalloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 					laarr[i].extLength -=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 						(numalloc <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 						 inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 					numalloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 					numalloc -= elen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 					if (*endnum > (i + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 						memmove(&laarr[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 							&laarr[i + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 							sizeof(struct long_ad) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 							(*endnum - (i + 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 					i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 					(*endnum)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			UDF_I(inode)->i_lenExtents +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 				numalloc << inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			      int *endnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	unsigned long blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	for (i = 0; i < (*endnum - 1); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 			(((li->extLength >> 30) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 			((lip1->extLocation.logicalBlockNum -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			  li->extLocation.logicalBlockNum) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			(((li->extLength & UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			blocksize - 1) >> blocksize_bits)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 				(lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 				blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 				lip1->extLength = (lip1->extLength -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 						  (li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 						   UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 						   UDF_EXTENT_LENGTH_MASK) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 							~(blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 				li->extLength = (li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 						 UDF_EXTENT_FLAG_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 						(UDF_EXTENT_LENGTH_MASK + 1) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 						blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 				lip1->extLocation.logicalBlockNum =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 					li->extLocation.logicalBlockNum +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 					((li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 						UDF_EXTENT_LENGTH_MASK) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 						blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 				li->extLength = lip1->extLength +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 					(((li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 						UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 					 blocksize - 1) & ~(blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 				if (*endnum > (i + 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 					memmove(&laarr[i + 1], &laarr[i + 2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 						sizeof(struct long_ad) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 						(*endnum - (i + 2)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 				i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 				(*endnum)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		} else if (((li->extLength >> 30) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 				(EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			   ((lip1->extLength >> 30) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 					((li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 					  UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 					 blocksize - 1) >> blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 			li->extLocation.logicalBlockNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			li->extLocation.partitionReferenceNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			     (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			     blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 				lip1->extLength = (lip1->extLength -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 						   (li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 						   UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 						   UDF_EXTENT_LENGTH_MASK) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 						   ~(blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 				li->extLength = (li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 						 UDF_EXTENT_FLAG_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 						(UDF_EXTENT_LENGTH_MASK + 1) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 						blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 				li->extLength = lip1->extLength +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 					(((li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 						UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 					  blocksize - 1) & ~(blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 				if (*endnum > (i + 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 					memmove(&laarr[i + 1], &laarr[i + 2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 						sizeof(struct long_ad) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 						(*endnum - (i + 2)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 				i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 				(*endnum)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		} else if ((li->extLength >> 30) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			udf_free_blocks(inode->i_sb, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 					&li->extLocation, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 					((li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 						UDF_EXTENT_LENGTH_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 					 blocksize - 1) >> blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 			li->extLocation.logicalBlockNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 			li->extLocation.partitionReferenceNum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			li->extLength = (li->extLength &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 						UDF_EXTENT_LENGTH_MASK) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 						EXT_NOT_RECORDED_NOT_ALLOCATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			       int startnum, int endnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			       struct extent_position *epos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	int start = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	struct kernel_lb_addr tmploc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	uint32_t tmplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	if (startnum > endnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		for (i = 0; i < (startnum - endnum); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 			udf_delete_aext(inode, *epos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	} else if (startnum < endnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		for (i = 0; i < (endnum - startnum); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			udf_insert_aext(inode, *epos, laarr[i].extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 					laarr[i].extLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 			udf_next_aext(inode, epos, &laarr[i].extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 				      &laarr[i].extLength, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			start++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	for (i = start; i < endnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		udf_write_aext(inode, epos, &laarr[i].extLocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			       laarr[i].extLength, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			      int create, int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	bh = udf_getblk(inode, block, create, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	if (buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	ll_rw_block(REQ_OP_READ, 0, 1, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	wait_on_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	if (buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	*err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) int udf_setsize(struct inode *inode, loff_t newsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	struct udf_inode_info *iinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	unsigned int bsize = i_blocksize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	      S_ISLNK(inode->i_mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	if (newsize > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		down_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			if (bsize <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			    (udf_file_entry_alloc_offset(inode) + newsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 				err = udf_expand_file_adinicb(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 				if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 					return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 				down_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 				iinfo->i_lenAlloc = newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 				goto set_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		err = udf_extend_file(inode, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) set_size:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		truncate_setsize(inode, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			down_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			udf_clear_extent_cache(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			       0x00, bsize - newsize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 			       udf_file_entry_alloc_offset(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			iinfo->i_lenAlloc = newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 			truncate_setsize(inode, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 			up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			goto update_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		err = block_truncate_page(inode->i_mapping, newsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 					  udf_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		truncate_setsize(inode, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		down_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		udf_clear_extent_cache(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		err = udf_truncate_extents(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		up_write(&iinfo->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) update_time:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	if (IS_SYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		udf_sync_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)  * Maximum length of linked list formed by ICB hierarchy. The chosen number is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)  * arbitrary - just that we hopefully don't limit any real use of rewritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)  * inode on write-once media but avoid looping for too long on corrupted media.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) #define UDF_MAX_ICB_NESTING 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) static int udf_read_inode(struct inode *inode, bool hidden_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	struct fileEntry *fe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	struct extendedFileEntry *efe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	uint16_t ident;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	struct kernel_lb_addr *iloc = &iinfo->i_location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	unsigned int link_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	unsigned int indirections = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	int bs = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	int ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	uint32_t uid, gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) reread:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	if (iloc->partitionReferenceNum >= sbi->s_partitions) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		udf_debug("partition reference: %u > logical volume partitions: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			  iloc->partitionReferenceNum, sbi->s_partitions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	if (iloc->logicalBlockNum >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	    sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		udf_debug("block=%u, partition=%u out of range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			  iloc->logicalBlockNum, iloc->partitionReferenceNum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	 * Set defaults, but the inode is still incomplete!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	 * Note: get_new_inode() sets the following on a new inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	 *      i_sb = sb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	 *      i_no = ino
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	 *      i_flags = sb->s_flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	 *      i_state = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	 * clean_inode(): zero fills and sets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	 *      i_count = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	 *      i_nlink = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	 *      i_op = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	    ident != TAG_IDENT_USE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			inode->i_ino, ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	fe = (struct fileEntry *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	efe = (struct extendedFileEntry *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		struct buffer_head *ibh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		if (ident == TAG_IDENT_IE && ibh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			struct kernel_lb_addr loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 			struct indirectEntry *ie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			ie = (struct indirectEntry *)ibh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			loc = lelb_to_cpu(ie->indirectICB.extLocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			if (ie->indirectICB.extLength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 				brelse(ibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 				memcpy(&iinfo->i_location, &loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 				       sizeof(struct kernel_lb_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 				if (++indirections > UDF_MAX_ICB_NESTING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 					udf_err(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 						"too many ICBs in ICB hierarchy"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 						" (max %d supported)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 						UDF_MAX_ICB_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 				brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 				goto reread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		brelse(ibh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	} else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		udf_err(inode->i_sb, "unsupported strategy type: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 			le16_to_cpu(fe->icbTag.strategyType));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	if (fe->icbTag.strategyType == cpu_to_le16(4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		iinfo->i_strat4096 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		iinfo->i_strat4096 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 							ICBTAG_FLAG_AD_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	    iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	    iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	iinfo->i_unique = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	iinfo->i_lenEAttr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	iinfo->i_lenExtents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	iinfo->i_lenAlloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	iinfo->i_next_alloc_block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	iinfo->i_next_alloc_goal = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		iinfo->i_efe = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		iinfo->i_use = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		ret = udf_alloc_i_data(inode, bs -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 					sizeof(struct extendedFileEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		memcpy(iinfo->i_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		       bh->b_data + sizeof(struct extendedFileEntry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		       bs - sizeof(struct extendedFileEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	} else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		iinfo->i_efe = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		iinfo->i_use = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		memcpy(iinfo->i_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		       bh->b_data + sizeof(struct fileEntry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		       bs - sizeof(struct fileEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	} else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		iinfo->i_efe = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		iinfo->i_use = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		iinfo->i_lenAlloc = le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 				((struct unallocSpaceEntry *)bh->b_data)->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 				 lengthAllocDescs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		ret = udf_alloc_i_data(inode, bs -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 					sizeof(struct unallocSpaceEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		memcpy(iinfo->i_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		       bh->b_data + sizeof(struct unallocSpaceEntry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		       bs - sizeof(struct unallocSpaceEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	read_lock(&sbi->s_cred_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	uid = le32_to_cpu(fe->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (uid == UDF_INVALID_ID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		inode->i_uid = sbi->s_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		i_uid_write(inode, uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	gid = le32_to_cpu(fe->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (gid == UDF_INVALID_ID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		inode->i_gid = sbi->s_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		i_gid_write(inode, gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 			sbi->s_fmode != UDF_INVALID_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		inode->i_mode = sbi->s_fmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 			sbi->s_dmode != UDF_INVALID_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		inode->i_mode = sbi->s_dmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		inode->i_mode = udf_convert_permissions(fe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	inode->i_mode &= ~sbi->s_umask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	read_unlock(&sbi->s_cred_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	link_count = le16_to_cpu(fe->fileLinkCount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	if (!link_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		if (!hidden_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 			ret = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		link_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	set_nlink(inode, link_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	inode->i_size = le64_to_cpu(fe->informationLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	iinfo->i_lenExtents = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (iinfo->i_efe == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			(inode->i_sb->s_blocksize_bits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 		udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		iinfo->i_unique = le64_to_cpu(fe->uniqueID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		iinfo->i_streamdir = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		iinfo->i_lenStreams = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		    (inode->i_sb->s_blocksize_bits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		iinfo->i_unique = le64_to_cpu(efe->uniqueID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		/* Named streams */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		iinfo->i_locStreamdir =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 			lelb_to_cpu(efe->streamDirectoryICB.extLocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		if (iinfo->i_lenStreams >= inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 			iinfo->i_lenStreams -= inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 			iinfo->i_lenStreams = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	inode->i_generation = iinfo->i_unique;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	 * Sanity check length of allocation descriptors and extended attrs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	 * avoid integer overflows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	/* Now do exact checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	/* Sanity checks for files in ICB so that we don't get confused later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		 * For file in ICB data is stored in allocation descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		 * so sizes should match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		if (iinfo->i_lenAlloc != inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		/* File in ICB has to fit in there... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	switch (fe->icbTag.fileType) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	case ICBTAG_FILE_TYPE_DIRECTORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		inode->i_op = &udf_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		inode->i_fop = &udf_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		inode->i_mode |= S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	case ICBTAG_FILE_TYPE_REALTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	case ICBTAG_FILE_TYPE_REGULAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	case ICBTAG_FILE_TYPE_UNDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	case ICBTAG_FILE_TYPE_VAT20:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 			inode->i_data.a_ops = &udf_adinicb_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			inode->i_data.a_ops = &udf_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		inode->i_op = &udf_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		inode->i_fop = &udf_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		inode->i_mode |= S_IFREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	case ICBTAG_FILE_TYPE_BLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		inode->i_mode |= S_IFBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	case ICBTAG_FILE_TYPE_CHAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		inode->i_mode |= S_IFCHR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	case ICBTAG_FILE_TYPE_FIFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 		init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	case ICBTAG_FILE_TYPE_SOCKET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	case ICBTAG_FILE_TYPE_SYMLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		inode->i_data.a_ops = &udf_symlink_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 		inode->i_op = &udf_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		inode->i_mode = S_IFLNK | 0777;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	case ICBTAG_FILE_TYPE_MAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		udf_debug("METADATA FILE-----\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	case ICBTAG_FILE_TYPE_MIRROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		udf_debug("METADATA MIRROR FILE-----\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	case ICBTAG_FILE_TYPE_BITMAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		udf_debug("METADATA BITMAP FILE-----\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 			inode->i_ino, fe->icbTag.fileType);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		struct deviceSpec *dsea =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 			(struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		if (dsea) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 			init_special_inode(inode, inode->i_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 				MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 				      le32_to_cpu(dsea->minorDeviceIdent)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 			/* Developer ID ??? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) static int udf_alloc_i_data(struct inode *inode, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	iinfo->i_data = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	if (!iinfo->i_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) static umode_t udf_convert_permissions(struct fileEntry *fe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	umode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	uint32_t permissions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	uint32_t flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	permissions = le32_to_cpu(fe->permissions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	flags = le16_to_cpu(fe->icbTag.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	mode =	((permissions) & 0007) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		((permissions >> 2) & 0070) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		((permissions >> 4) & 0700) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	return mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) void udf_update_extra_perms(struct inode *inode, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	 * UDF 2.01 sec. 3.3.3.3 Note 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	 * In Unix, delete permission tracks write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	if (mode & 0200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		iinfo->i_extraPerms |= FE_PERM_U_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	if (mode & 0020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		iinfo->i_extraPerms |= FE_PERM_G_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (mode & 0002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		iinfo->i_extraPerms |= FE_PERM_O_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) static int udf_sync_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	return udf_update_inode(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	if (iinfo->i_crtime.tv_sec > time.tv_sec ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	    (iinfo->i_crtime.tv_sec == time.tv_sec &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	     iinfo->i_crtime.tv_nsec > time.tv_nsec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		iinfo->i_crtime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) static int udf_update_inode(struct inode *inode, int do_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	struct fileEntry *fe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	struct extendedFileEntry *efe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	uint64_t lb_recorded;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	uint32_t udfperms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	uint16_t icbflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	uint16_t crclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	bh = udf_tgetblk(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 			udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		udf_debug("getblk failure\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	memset(bh->b_data, 0, inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	fe = (struct fileEntry *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	efe = (struct extendedFileEntry *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	if (iinfo->i_use) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		struct unallocSpaceEntry *use =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 			(struct unallocSpaceEntry *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		       iinfo->i_data, inode->i_sb->s_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 					sizeof(struct unallocSpaceEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		crclen = sizeof(struct unallocSpaceEntry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		fe->uid = cpu_to_le32(UDF_INVALID_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		fe->uid = cpu_to_le32(i_uid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		fe->gid = cpu_to_le32(UDF_INVALID_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		fe->gid = cpu_to_le32(i_gid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	udfperms = ((inode->i_mode & 0007)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		   ((inode->i_mode & 0070) << 2) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		   ((inode->i_mode & 0700) << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	udfperms |= iinfo->i_extraPerms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	fe->permissions = cpu_to_le32(udfperms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	fe->informationLength = cpu_to_le64(inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		struct regid *eid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		struct deviceSpec *dsea =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 			(struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		if (!dsea) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 			dsea = (struct deviceSpec *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 				udf_add_extendedattr(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 						     sizeof(struct deviceSpec) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 						     sizeof(struct regid), 12, 0x3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 			dsea->attrType = cpu_to_le32(12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 			dsea->attrSubtype = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 			dsea->attrLength = cpu_to_le32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 						sizeof(struct deviceSpec) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 						sizeof(struct regid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 			dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		eid = (struct regid *)dsea->impUse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		memset(eid, 0, sizeof(*eid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		strcpy(eid->ident, UDF_ID_DEVELOPER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 		eid->identSuffix[1] = UDF_OS_ID_LINUX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 		dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		lb_recorded = 0; /* No extents => no blocks! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		lb_recorded =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 			(inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 			(blocksize_bits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	if (iinfo->i_efe == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 		memcpy(bh->b_data + sizeof(struct fileEntry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		       iinfo->i_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		       inode->i_sb->s_blocksize - sizeof(struct fileEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		memset(&(fe->impIdent), 0, sizeof(struct regid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		fe->uniqueID = cpu_to_le64(iinfo->i_unique);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		crclen = sizeof(struct fileEntry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		memcpy(bh->b_data + sizeof(struct extendedFileEntry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		       iinfo->i_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		       inode->i_sb->s_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 					sizeof(struct extendedFileEntry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		efe->objectSize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 			cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		if (iinfo->i_streamdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 			struct long_ad *icb_lad = &efe->streamDirectoryICB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 			icb_lad->extLocation =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 				cpu_to_lelb(iinfo->i_locStreamdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			icb_lad->extLength =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 				cpu_to_le32(inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		udf_adjust_time(iinfo, inode->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		udf_adjust_time(iinfo, inode->i_mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		udf_adjust_time(iinfo, inode->i_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		efe->uniqueID = cpu_to_le64(iinfo->i_unique);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		crclen = sizeof(struct extendedFileEntry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) finish:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	if (iinfo->i_strat4096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		fe->icbTag.strategyType = cpu_to_le16(4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		fe->icbTag.strategyParameter = cpu_to_le16(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		fe->icbTag.numEntries = cpu_to_le16(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		fe->icbTag.strategyType = cpu_to_le16(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		fe->icbTag.numEntries = cpu_to_le16(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	if (iinfo->i_use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	else if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	else if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	else if (S_ISLNK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	else if (S_ISBLK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	else if (S_ISCHR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	else if (S_ISFIFO(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	else if (S_ISSOCK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	icbflags =	iinfo->i_alloc_type |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 			((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 			((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 			((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 			(le16_to_cpu(fe->icbTag.flags) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 				~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 				ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	fe->icbTag.flags = cpu_to_le16(icbflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	if (sbi->s_udfrev >= 0x0200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		fe->descTag.descVersion = cpu_to_le16(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		fe->descTag.descVersion = cpu_to_le16(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	fe->descTag.tagLocation = cpu_to_le32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 					iinfo->i_location.logicalBlockNum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	fe->descTag.descCRCLength = cpu_to_le16(crclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 						  crclen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	/* write the data blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	if (do_sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 		sync_dirty_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		if (buffer_write_io_error(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 			udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 				 inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 			 bool hidden_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	unsigned long block = udf_get_lb_pblock(sb, ino, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	struct inode *inode = iget_locked(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	if (!(inode->i_state & I_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	err = udf_read_inode(inode, hidden_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		iget_failed(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 			    struct extent_position *epos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	struct allocExtDesc *aed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	struct extent_position nepos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	struct kernel_lb_addr neloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	int ver, adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		adsize = sizeof(struct short_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		adsize = sizeof(struct long_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	neloc.logicalBlockNum = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	memset(bh->b_data, 0x00, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	aed = (struct allocExtDesc *)(bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		aed->previousAllocExtLocation =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 				cpu_to_le32(epos->block.logicalBlockNum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	aed->lengthAllocDescs = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	if (UDF_SB(sb)->s_udfrev >= 0x0200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 		ver = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		ver = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		    sizeof(struct tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	nepos.block = neloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	nepos.offset = sizeof(struct allocExtDesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	nepos.bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	 * Do we have to copy current last extent to make space for indirect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	 * one?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	if (epos->offset + adsize > sb->s_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		struct kernel_lb_addr cp_loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		uint32_t cp_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		int cp_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 		epos->offset -= adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		cp_len |= ((uint32_t)cp_type) << 30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		__udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		udf_write_aext(inode, epos, &nepos.block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 			       sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		__udf_add_aext(inode, epos, &nepos.block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 			       sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	brelse(epos->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	*epos = nepos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)  * Append extent at the given position - should be the first free one in inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)  * / indirect extent. This function assumes there is enough space in the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)  * or indirect extent. Use udf_add_aext() if you didn't check for this before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) int __udf_add_aext(struct inode *inode, struct extent_position *epos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		   struct kernel_lb_addr *eloc, uint32_t elen, int inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	struct allocExtDesc *aed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	int adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		adsize = sizeof(struct short_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		adsize = sizeof(struct long_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	if (!epos->bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		WARN_ON(iinfo->i_lenAlloc !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 			epos->offset - udf_file_entry_alloc_offset(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		aed = (struct allocExtDesc *)epos->bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 			epos->offset - sizeof(struct allocExtDesc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 		WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	udf_write_aext(inode, epos, eloc, elen, inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	if (!epos->bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 		iinfo->i_lenAlloc += adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		aed = (struct allocExtDesc *)epos->bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		le32_add_cpu(&aed->lengthAllocDescs, adsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 				UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 			udf_update_tag(epos->bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 					epos->offset + (inc ? 0 : adsize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 			udf_update_tag(epos->bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 					sizeof(struct allocExtDesc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		mark_buffer_dirty_inode(epos->bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049)  * Append extent at given position - should be the first free one in inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050)  * / indirect extent. Takes care of allocating and linking indirect blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) int udf_add_aext(struct inode *inode, struct extent_position *epos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 		 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	int adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 		adsize = sizeof(struct short_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		adsize = sizeof(struct long_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	if (epos->offset + (2 * adsize) > sb->s_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		udf_pblk_t new_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		new_block = udf_new_block(sb, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 					  epos->block.partitionReferenceNum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 					  epos->block.logicalBlockNum, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 		if (!new_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 			return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		err = udf_setup_indirect_aext(inode, new_block, epos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	return __udf_add_aext(inode, epos, eloc, elen, inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) void udf_write_aext(struct inode *inode, struct extent_position *epos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 		    struct kernel_lb_addr *eloc, uint32_t elen, int inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	int adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	uint8_t *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	struct short_ad *sad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	struct long_ad *lad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	if (!epos->bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 		ptr = iinfo->i_data + epos->offset -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 			udf_file_entry_alloc_offset(inode) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 			iinfo->i_lenEAttr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 		ptr = epos->bh->b_data + epos->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	switch (iinfo->i_alloc_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	case ICBTAG_FLAG_AD_SHORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 		sad = (struct short_ad *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 		sad->extLength = cpu_to_le32(elen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 		adsize = sizeof(struct short_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	case ICBTAG_FLAG_AD_LONG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 		lad = (struct long_ad *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		lad->extLength = cpu_to_le32(elen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 		lad->extLocation = cpu_to_lelb(*eloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		memset(lad->impUse, 0x00, sizeof(lad->impUse));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 		adsize = sizeof(struct long_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	if (epos->bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 		if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 			struct allocExtDesc *aed =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 				(struct allocExtDesc *)epos->bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 			udf_update_tag(epos->bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 				       le32_to_cpu(aed->lengthAllocDescs) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 				       sizeof(struct allocExtDesc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 		mark_buffer_dirty_inode(epos->bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	if (inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 		epos->offset += adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136)  * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137)  * someone does some weird stuff.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) #define UDF_MAX_INDIR_EXTS 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		     struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	int8_t etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	unsigned int indirections = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	       (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		udf_pblk_t block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 		if (++indirections > UDF_MAX_INDIR_EXTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 			udf_err(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 				"too many indirect extents in inode %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 				inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		epos->block = *eloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		epos->offset = sizeof(struct allocExtDesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		brelse(epos->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 		epos->bh = udf_tread(inode->i_sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		if (!epos->bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 			udf_debug("reading block %u failed!\n", block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	return etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 			struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	int alen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	int8_t etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	uint8_t *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	struct short_ad *sad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	struct long_ad *lad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	if (!epos->bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		if (!epos->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 			epos->offset = udf_file_entry_alloc_offset(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		ptr = iinfo->i_data + epos->offset -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 			udf_file_entry_alloc_offset(inode) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 			iinfo->i_lenEAttr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 		alen = udf_file_entry_alloc_offset(inode) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 							iinfo->i_lenAlloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		if (!epos->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 			epos->offset = sizeof(struct allocExtDesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 		ptr = epos->bh->b_data + epos->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		alen = sizeof(struct allocExtDesc) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 			le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 							lengthAllocDescs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	switch (iinfo->i_alloc_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	case ICBTAG_FLAG_AD_SHORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 		if (!sad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		etype = le32_to_cpu(sad->extLength) >> 30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 		eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 		eloc->partitionReferenceNum =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 				iinfo->i_location.partitionReferenceNum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 		*elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	case ICBTAG_FLAG_AD_LONG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 		lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		if (!lad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 		etype = le32_to_cpu(lad->extLength) >> 30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		*eloc = lelb_to_cpu(lad->extLocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 		*elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	return etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 			      struct kernel_lb_addr neloc, uint32_t nelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	struct kernel_lb_addr oeloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	uint32_t oelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	int8_t etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	if (epos.bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 		get_bh(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 		udf_write_aext(inode, &epos, &neloc, nelen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 		neloc = oeloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 		nelen = (etype << 30) | oelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	udf_add_aext(inode, &epos, &neloc, nelen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	brelse(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	return (nelen >> 30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	struct extent_position oepos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	int adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	int8_t etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	struct allocExtDesc *aed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	struct udf_inode_info *iinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	struct kernel_lb_addr eloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	uint32_t elen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	if (epos.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 		get_bh(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		get_bh(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 		adsize = sizeof(struct short_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		adsize = sizeof(struct long_ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 		adsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 	oepos = epos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 	if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 	while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		if (oepos.bh != epos.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 			oepos.block = epos.block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			brelse(oepos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 			get_bh(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 			oepos.bh = epos.bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 			oepos.offset = epos.offset - adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	elen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	if (epos.bh != oepos.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 		if (!oepos.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 			iinfo->i_lenAlloc -= (adsize * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 			mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 			aed = (struct allocExtDesc *)oepos.bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 			le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 				udf_update_tag(oepos.bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 						oepos.offset - (2 * adsize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 				udf_update_tag(oepos.bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 						sizeof(struct allocExtDesc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 			mark_buffer_dirty_inode(oepos.bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 		if (!oepos.bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 			iinfo->i_lenAlloc -= adsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 			mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 			aed = (struct allocExtDesc *)oepos.bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 			le32_add_cpu(&aed->lengthAllocDescs, -adsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 				udf_update_tag(oepos.bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 						epos.offset - adsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 				udf_update_tag(oepos.bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 						sizeof(struct allocExtDesc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 			mark_buffer_dirty_inode(oepos.bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	brelse(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	brelse(oepos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	return (elen >> 30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) int8_t inode_bmap(struct inode *inode, sector_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 		  struct extent_position *pos, struct kernel_lb_addr *eloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 		  uint32_t *elen, sector_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	int8_t etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	struct udf_inode_info *iinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 		pos->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 		pos->block = iinfo->i_location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 		pos->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	*elen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 		etype = udf_next_aext(inode, pos, eloc, elen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 		if (etype == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 			*offset = (bcount - lbcount) >> blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 			iinfo->i_lenExtents = lbcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		lbcount += *elen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	} while (lbcount <= bcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	/* update extent cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	udf_update_extent_cache(inode, lbcount - *elen, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	*offset = (bcount + *elen - lbcount) >> blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 	return etype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	struct kernel_lb_addr eloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	uint32_t elen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	sector_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	struct extent_position epos = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	udf_pblk_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	down_read(&UDF_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 						(EXT_RECORDED_ALLOCATED >> 30))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	up_read(&UDF_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	brelse(epos.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		return udf_fixed_to_variable(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) }