34 #include <linux/module.h>
38 #include <linux/slab.h>
49 #define EXTENT_MERGE_SIZE 5
52 static int udf_update_inode(
struct inode *,
int);
53 static void udf_fill_inode(
struct inode *,
struct buffer_head *);
54 static int udf_sync_inode(
struct inode *
inode);
59 static void udf_split_extents(
struct inode *,
int *,
int,
int,
61 static void udf_prealloc_extents(
struct inode *,
int,
int,
63 static void udf_merge_extents(
struct inode *,
65 static void udf_update_extents(
struct inode *,
68 static int udf_get_block(
struct inode *,
sector_t,
struct buffer_head *,
int);
79 udf_update_inode(inode,
IS_SYNC(inode));
86 udf_warn(inode->
i_sb,
"Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
88 (
unsigned long long)inode->
i_size,
102 loff_t isize = inode->
i_size;
137 loff_t
pos,
unsigned len,
unsigned flags,
138 struct page **pagep,
void **fsdata)
144 udf_write_failed(mapping, pos + len);
149 const struct iovec *iov,
150 loff_t
offset,
unsigned long nr_segs)
152 struct file *file = iocb->
ki_filp;
154 struct inode *inode = mapping->
host;
157 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
160 udf_write_failed(mapping, offset + iov_length(iov, nr_segs));
170 .readpage = udf_readpage,
171 .readpages = udf_readpages,
172 .writepage = udf_writepage,
173 .writepages = udf_writepages,
174 .write_begin = udf_write_begin,
176 .direct_IO = udf_direct_IO,
205 mark_inode_dirty(inode);
218 if (!PageUptodate(page)) {
225 SetPageUptodate(page);
239 err = inode->
i_data.a_ops->writepage(page, &udf_wbc);
254 mark_inode_dirty(inode);
263 struct buffer_head *dbh =
NULL;
269 loff_t f_pos = udf_ext0_offset(inode);
270 int size = udf_ext0_offset(inode) + inode->
i_size;
281 mark_inode_dirty(inode);
300 memset(dbh->b_data, 0x00, inode->
i_sb->s_blocksize);
301 set_buffer_uptodate(dbh);
306 f_pos & (inode->
i_sb->s_blocksize - 1);
309 dfibh.
sbh = dfibh.
ebh = dbh;
310 while (f_pos < size) {
342 epos.
offset = udf_file_entry_alloc_offset(inode);
347 mark_inode_dirty(inode);
351 static int udf_get_block(
struct inode *inode,
sector_t block,
352 struct buffer_head *bh_result,
int create)
361 map_bh(bh_result, inode->
i_sb, phys);
367 iinfo = UDF_I(inode);
376 phys = inode_getblk(inode, block, &err, &
new);
381 set_buffer_new(bh_result);
382 map_bh(bh_result, inode->
i_sb, phys);
389 static struct buffer_head *udf_getblk(
struct inode *inode,
long block,
390 int create,
int *err)
392 struct buffer_head *bh;
393 struct buffer_head
dummy;
396 dummy.b_blocknr = -1000;
397 *err = udf_get_block(inode, block, &
dummy, create);
398 if (!*err && buffer_mapped(&
dummy)) {
399 bh = sb_getblk(inode->
i_sb,
dummy.b_blocknr);
400 if (buffer_new(&
dummy)) {
402 memset(bh->b_data, 0x00, inode->
i_sb->s_blocksize);
403 set_buffer_uptodate(bh);
414 static int udf_do_extend_file(
struct inode *inode,
423 int prealloc_len = 0;
432 iinfo = UDF_I(inode);
489 while (blocks > add) {
530 static int udf_extend_file(
struct inode *inode, loff_t newsize)
547 adsize =
sizeof(
struct long_ad);
551 etype =
inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
564 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
565 (epos.bh && epos.offset ==
sizeof(
struct allocExtDesc))) {
568 extent.extLocation.logicalBlockNum = 0;
569 extent.extLocation.partitionReferenceNum = 0;
572 epos.offset -= adsize;
575 extent.extLength |= etype << 30;
577 err = udf_do_extend_file(inode, &epos, &
extent, offset);
593 int count = 0, startnum = 0, endnum = 0;
597 loff_t lbcount = 0, b_off = 0;
602 int goal = 0, pgoal = iinfo->
i_location.logicalBlockNum;
607 prev_epos.offset = udf_file_entry_alloc_offset(inode);
610 cur_epos = next_epos = prev_epos;
611 b_off = (loff_t)block << inode->
i_sb->s_blocksize_bits;
617 if (prev_epos.bh != cur_epos.bh) {
618 brelse(prev_epos.bh);
620 prev_epos.bh = cur_epos.bh;
622 if (cur_epos.bh != next_epos.bh) {
624 get_bh(next_epos.bh);
625 cur_epos.bh = next_epos.bh;
630 prev_epos.block = cur_epos.block;
631 cur_epos.block = next_epos.block;
633 prev_epos.offset = cur_epos.offset;
634 cur_epos.offset = next_epos.offset;
642 laarr[
c].extLength = (etype << 30) | elen;
643 laarr[
c].extLocation = eloc;
646 pgoal = eloc.logicalBlockNum +
647 ((elen + inode->
i_sb->s_blocksize - 1) >>
648 inode->
i_sb->s_blocksize_bits);
651 }
while (lbcount + elen <= b_off);
654 offset = b_off >> inode->
i_sb->s_blocksize_bits;
666 if (elen & (inode->
i_sb->s_blocksize - 1)) {
668 ((elen + inode->
i_sb->s_blocksize - 1) &
669 ~(inode->
i_sb->s_blocksize - 1));
672 brelse(prev_epos.bh);
674 brelse(next_epos.bh);
675 newblock = udf_get_lb_pblock(inode->
i_sb, &eloc, offset);
695 startnum = (offset > 0);
698 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
700 brelse(prev_epos.bh);
702 brelse(next_epos.bh);
717 inode->
i_sb->s_blocksize;
726 endnum = startnum = ((count > 2) ? 2 : count);
730 if (!c && count != 1) {
741 laarr[c + 1].extLength = (etype << 30) | elen;
742 laarr[c + 1].extLocation = eloc;
753 newblocknum = laarr[
c].extLocation.logicalBlockNum +
offset;
767 brelse(prev_epos.bh);
778 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
780 #ifdef UDF_PREALLOCATE
786 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
790 udf_merge_extents(inode, laarr, &endnum);
795 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
797 brelse(prev_epos.bh);
811 udf_sync_inode(inode);
813 mark_inode_dirty(inode);
818 static void udf_split_extents(
struct inode *inode,
int *c,
int offset,
823 unsigned long blocksize = inode->
i_sb->s_blocksize;
824 unsigned char blocksize_bits = inode->
i_sb->s_blocksize_bits;
831 blocksize - 1) >> blocksize_bits;
836 else if (!offset || blen == offset + 1) {
837 laarr[curr + 2] = laarr[curr + 1];
838 laarr[curr + 1] = laarr[
curr];
840 laarr[curr + 3] = laarr[curr + 1];
841 laarr[curr + 2] = laarr[curr + 1] = laarr[
curr];
847 &laarr[curr].extLocation,
849 laarr[
curr].extLength =
851 (offset << blocksize_bits);
852 laarr[
curr].extLocation.logicalBlockNum = 0;
853 laarr[
curr].extLocation.
856 laarr[
curr].extLength = (etype << 30) |
857 (offset << blocksize_bits);
863 laarr[
curr].extLocation.logicalBlockNum = newblocknum;
866 UDF_I(inode)->i_location.partitionReferenceNum;
871 if (blen != offset + 1) {
875 laarr[curr].extLength = (etype << 30) |
876 ((blen - (offset + 1)) << blocksize_bits);
883 static void udf_prealloc_extents(
struct inode *inode,
int c,
int lastblock,
889 if (*endnum >= (c + 1)) {
898 length = currlength =
899 (((laarr[c + 1].extLength &
901 inode->
i_sb->s_blocksize - 1) >>
902 inode->
i_sb->s_blocksize_bits);
907 for (
i = start + 1;
i <= *endnum;
i++) {
913 length += (((laarr[
i].extLength &
915 inode->
i_sb->s_blocksize - 1) >>
916 inode->
i_sb->s_blocksize_bits);
922 int next = laarr[
start].extLocation.logicalBlockNum +
924 inode->
i_sb->s_blocksize - 1) >>
925 inode->
i_sb->s_blocksize_bits);
927 laarr[start].extLocation.partitionReferenceNum,
932 if (start == (c + 1))
933 laarr[
start].extLength +=
935 inode->
i_sb->s_blocksize_bits);
937 memmove(&laarr[c + 2], &laarr[c + 1],
938 sizeof(
struct long_ad) * (*endnum - (c + 1)));
940 laarr[c + 1].extLocation.logicalBlockNum =
next;
941 laarr[c + 1].extLocation.partitionReferenceNum =
942 laarr[
c].extLocation.
944 laarr[c + 1].extLength =
947 inode->
i_sb->s_blocksize_bits);
951 for (
i = start + 1; numalloc &&
i < *endnum;
i++) {
952 int elen = ((laarr[
i].extLength &
954 inode->
i_sb->s_blocksize - 1) >>
955 inode->
i_sb->s_blocksize_bits;
957 if (elen > numalloc) {
958 laarr[
i].extLength -=
960 inode->
i_sb->s_blocksize_bits);
964 if (*endnum > (
i + 1))
968 (*endnum - (i + 1)));
973 UDF_I(inode)->i_lenExtents +=
974 numalloc << inode->
i_sb->s_blocksize_bits;
979 static void udf_merge_extents(
struct inode *inode,
984 unsigned long blocksize = inode->
i_sb->s_blocksize;
985 unsigned char blocksize_bits = inode->
i_sb->s_blocksize_bits;
987 for (i = 0; i < (*endnum - 1); i++) {
997 blocksize - 1) >> blocksize_bits)))) {
1020 blocksize - 1) & ~(blocksize - 1));
1021 if (*endnum > (i + 2))
1022 memmove(&laarr[i + 1], &laarr[i + 2],
1024 (*endnum - (i + 2)));
1035 blocksize - 1) >> blocksize_bits);
1055 blocksize - 1) & ~(blocksize - 1));
1056 if (*endnum > (i + 2))
1057 memmove(&laarr[i + 1], &laarr[i + 2],
1059 (*endnum - (i + 2)));
1069 blocksize - 1) >> blocksize_bits);
1079 static void udf_update_extents(
struct inode *inode,
1081 int startnum,
int endnum,
1088 if (startnum > endnum) {
1089 for (i = 0; i < (startnum - endnum); i++)
1092 }
else if (startnum < endnum) {
1093 for (i = 0; i < (endnum - startnum); i++) {
1094 udf_insert_aext(inode, *epos, laarr[i].
extLocation,
1102 for (i = start; i < endnum; i++) {
1109 struct buffer_head *
udf_bread(
struct inode *inode,
int block,
1110 int create,
int *err)
1112 struct buffer_head *bh =
NULL;
1114 bh = udf_getblk(inode, block, create, err);
1118 if (buffer_uptodate(bh))
1124 if (buffer_uptodate(bh))
1144 iinfo = UDF_I(inode);
1145 if (newsize > inode->
i_size) {
1149 (udf_file_entry_alloc_offset(inode) + newsize)) {
1159 err = udf_extend_file(inode, newsize);
1171 0x00, bsize - newsize -
1172 udf_file_entry_alloc_offset(inode));
1190 udf_sync_inode(inode);
1192 mark_inode_dirty(inode);
1196 static void __udf_read_inode(
struct inode *inode)
1198 struct buffer_head *bh =
NULL;
1224 udf_err(inode->
i_sb,
"(ino %ld) failed ident=%d\n",
1225 inode->
i_ino, ident);
1234 struct buffer_head *ibh;
1239 struct buffer_head *nbh =
NULL;
1257 __udf_read_inode(inode);
1265 udf_err(inode->
i_sb,
"unsupported strategy type: %d\n",
1271 udf_fill_inode(inode, bh);
1276 static void udf_fill_inode(
struct inode *inode,
struct buffer_head *bh)
1282 unsigned int link_count;
1303 if (udf_alloc_i_data(inode, inode->
i_sb->s_blocksize -
1310 inode->
i_sb->s_blocksize -
1315 if (udf_alloc_i_data(inode, inode->
i_sb->s_blocksize -
1329 if (udf_alloc_i_data(inode, inode->
i_sb->s_blocksize -
1336 inode->
i_sb->s_blocksize -
1343 if (!uid_valid(inode->
i_uid) ||
1346 inode->
i_uid = UDF_SB(inode->
i_sb)->s_uid;
1349 if (!gid_valid(inode->
i_gid) ||
1352 inode->
i_gid = UDF_SB(inode->
i_sb)->s_gid;
1361 inode->
i_mode = udf_convert_permissions(fe);
1373 if (iinfo->
i_efe == 0) {
1375 (inode->
i_sb->s_blocksize_bits - 9);
1393 (inode->
i_sb->s_blocksize_bits - 9);
1414 switch (fe->
icbTag.fileType) {
1454 udf_debug(
"METADATA MIRROR FILE-----\n");
1457 udf_debug(
"METADATA BITMAP FILE-----\n");
1460 udf_err(inode->
i_sb,
"(ino %ld) failed unknown file type=%d\n",
1478 static int udf_alloc_i_data(
struct inode *inode,
size_t size)
1484 udf_err(inode->
i_sb,
"(ino %ld) no free memory\n",
1502 ((permissions >> 2) &
S_IRWXG) |
1503 ((permissions >> 4) &
S_IRWXU) |
1516 static int udf_sync_inode(
struct inode *inode)
1518 return udf_update_inode(inode, 1);
1521 static int udf_update_inode(
struct inode *inode,
int do_sync)
1523 struct buffer_head *bh =
NULL;
1532 unsigned char blocksize_bits = inode->
i_sb->s_blocksize_bits;
1543 memset(bh->b_data, 0, inode->
i_sb->s_blocksize);
1604 sizeof(
struct regid), 12, 0x3);
1609 sizeof(
struct regid));
1625 (inode->
i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1626 (blocksize_bits - 9);
1628 if (iinfo->
i_efe == 0) {
1650 inode->
i_sb->s_blocksize -
1733 set_buffer_uptodate(bh);
1740 if (buffer_write_io_error(bh)) {
1741 udf_warn(inode->
i_sb,
"IO error syncing udf inode [%08lx]\n",
1753 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1761 __udf_read_inode(inode);
1770 udf_debug(
"block=%d, partition=%d out of range\n",
1795 udf_file_entry_alloc_offset(inode) +
1798 ptr = epos->
bh->b_data + epos->
offset;
1803 adsize =
sizeof(
struct long_ad);
1807 if (epos->
offset + (2 * adsize) > inode->
i_sb->s_blocksize) {
1808 unsigned char *sptr, *dptr;
1809 struct buffer_head *nbh;
1816 if (!epos->
block.logicalBlockNum)
1824 memset(nbh->b_data, 0x00, inode->
i_sb->s_blocksize);
1825 set_buffer_uptodate(nbh);
1833 if (epos->
offset + adsize > inode->
i_sb->s_blocksize) {
1836 sptr = ptr - adsize;
1838 memcpy(dptr, sptr, adsize);
1841 loffset = epos->
offset + adsize;
1851 mark_inode_dirty(inode);
1854 if (UDF_SB(inode->
i_sb)->s_udfrev >= 0x0200)
1856 epos->
block.logicalBlockNum,
sizeof(
struct tag));
1859 epos->
block.logicalBlockNum,
sizeof(
struct tag));
1864 inode->
i_sb->s_blocksize);
1871 inode->
i_sb->s_blocksize);
1878 UDF_SB(inode->
i_sb)->s_udfrev >= 0x0201)
1886 mark_inode_dirty(inode);
1895 mark_inode_dirty(inode);
1900 UDF_SB(inode->
i_sb)->s_udfrev >= 0x0201)
1902 epos->
offset + (inc ? 0 : adsize));
1923 udf_file_entry_alloc_offset(inode) +
1926 ptr = epos->
bh->b_data + epos->
offset;
1940 adsize =
sizeof(
struct long_ad);
1948 UDF_SB(inode->
i_sb)->s_udfrev >= 0x0201) {
1957 mark_inode_dirty(inode);
1972 epos->
block = *eloc;
1975 block = udf_get_lb_pblock(inode->
i_sb, &epos->
block, 0);
1978 udf_debug(
"reading block %d failed!\n", block);
1998 epos->
offset = udf_file_entry_alloc_offset(inode);
2000 udf_file_entry_alloc_offset(inode) +
2002 alen = udf_file_entry_alloc_offset(inode) +
2007 ptr = epos->
bh->b_data + epos->
offset;
2009 le32_to_cpu(((struct allocExtDesc *)epos->
bh->b_data)->
2050 while ((etype =
udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2053 nelen = (etype << 30) | oelen;
2058 return (nelen >> 30);
2075 iinfo = UDF_I(inode);
2079 adsize =
sizeof(
struct long_ad);
2087 while ((etype =
udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2089 if (oepos.
bh != epos.
bh) {
2100 if (epos.
bh != oepos.
bh) {
2106 mark_inode_dirty(inode);
2111 UDF_SB(inode->
i_sb)->s_udfrev >= 0x0201)
2113 oepos.
offset - (2 * adsize));
2123 mark_inode_dirty(inode);
2128 UDF_SB(inode->
i_sb)->s_udfrev >= 0x0201)
2141 return (elen >> 30);
2148 unsigned char blocksize_bits = inode->
i_sb->s_blocksize_bits;
2149 loff_t lbcount = 0,
bcount =
2150 (loff_t) block << blocksize_bits;
2154 iinfo = UDF_I(inode);
2163 *offset = (
bcount - lbcount) >> blocksize_bits;
2168 }
while (lbcount <=
bcount);
2170 *offset = (
bcount + *elen - lbcount) >> blocksize_bits;
2185 if (
inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2187 ret = udf_get_lb_pblock(inode->
i_sb, &eloc, offset);
2191 up_read(&UDF_I(inode)->i_data_sem);