blob: aad46401ede5d881053aac1cb64050adcda60204 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
16 *
17 * HISTORY
18 *
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
Marcin Slusarz4b111112008-02-08 04:20:36 -080022 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
30 */
31
32#include "udfdecl.h"
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/writeback.h>
37#include <linux/slab.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020038#include <linux/crc-itu-t.h>
Namjae Jeonbc112322011-10-03 14:02:59 +090039#include <linux/mpage.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080040#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "udf_i.h"
43#include "udf_sb.h"
44
45MODULE_AUTHOR("Ben Fennema");
46MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47MODULE_LICENSE("GPL");
48
49#define EXTENT_MERGE_SIZE 5
50
Al Virofaa17292011-07-26 03:18:29 -040051static umode_t udf_convert_permissions(struct fileEntry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int udf_update_inode(struct inode *, int);
Jan Kara49521de2010-10-20 17:42:44 +020053static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070054static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara7b0b0932011-12-10 01:43:33 +010055static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070056static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020057 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020059 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020061 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020063 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020065 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070066 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
68
Namjae Jeon99600052013-01-19 11:17:14 +090069static void __udf_clear_extent_cache(struct inode *inode)
70{
71 struct udf_inode_info *iinfo = UDF_I(inode);
72
73 if (iinfo->cached_extent.lstart != -1) {
74 brelse(iinfo->cached_extent.epos.bh);
75 iinfo->cached_extent.lstart = -1;
76 }
77}
78
79/* Invalidate extent cache */
80static void udf_clear_extent_cache(struct inode *inode)
81{
82 struct udf_inode_info *iinfo = UDF_I(inode);
83
84 spin_lock(&iinfo->i_extent_cache_lock);
85 __udf_clear_extent_cache(inode);
86 spin_unlock(&iinfo->i_extent_cache_lock);
87}
88
89/* Return contents of extent cache */
90static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
91 loff_t *lbcount, struct extent_position *pos)
92{
93 struct udf_inode_info *iinfo = UDF_I(inode);
94 int ret = 0;
95
96 spin_lock(&iinfo->i_extent_cache_lock);
97 if ((iinfo->cached_extent.lstart <= bcount) &&
98 (iinfo->cached_extent.lstart != -1)) {
99 /* Cache hit */
100 *lbcount = iinfo->cached_extent.lstart;
101 memcpy(pos, &iinfo->cached_extent.epos,
102 sizeof(struct extent_position));
103 if (pos->bh)
104 get_bh(pos->bh);
105 ret = 1;
106 }
107 spin_unlock(&iinfo->i_extent_cache_lock);
108 return ret;
109}
110
111/* Add extent to extent cache */
112static void udf_update_extent_cache(struct inode *inode, loff_t estart,
113 struct extent_position *pos, int next_epos)
114{
115 struct udf_inode_info *iinfo = UDF_I(inode);
116
117 spin_lock(&iinfo->i_extent_cache_lock);
118 /* Invalidate previously cached extent */
119 __udf_clear_extent_cache(inode);
120 if (pos->bh)
121 get_bh(pos->bh);
122 memcpy(&iinfo->cached_extent.epos, pos,
123 sizeof(struct extent_position));
124 iinfo->cached_extent.lstart = estart;
125 if (next_epos)
126 switch (iinfo->i_alloc_type) {
127 case ICBTAG_FLAG_AD_SHORT:
128 iinfo->cached_extent.epos.offset -=
129 sizeof(struct short_ad);
130 break;
131 case ICBTAG_FLAG_AD_LONG:
132 iinfo->cached_extent.epos.offset -=
133 sizeof(struct long_ad);
134 }
135 spin_unlock(&iinfo->i_extent_cache_lock);
136}
Christoph Hellwigb1e32122008-02-22 12:38:48 +0100137
Al Viro3aac2b62010-06-07 00:43:39 -0400138void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Jan Kara2c948b32009-12-03 13:39:28 +0100140 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400141 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +0100142
Al Viro3aac2b62010-06-07 00:43:39 -0400143 if (!inode->i_nlink && !is_bad_inode(inode)) {
144 want_delete = 1;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200145 udf_setsize(inode, 0);
Al Viro3aac2b62010-06-07 00:43:39 -0400146 udf_update_inode(inode, IS_SYNC(inode));
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700147 }
148 truncate_inode_pages_final(&inode->i_data);
Al Viro3aac2b62010-06-07 00:43:39 -0400149 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200150 clear_inode(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100151 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
152 inode->i_size != iinfo->i_lenExtents) {
Joe Perches78ace702011-10-10 01:08:05 -0700153 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",
154 inode->i_ino, inode->i_mode,
155 (unsigned long long)inode->i_size,
156 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800158 kfree(iinfo->i_ext.i_data);
159 iinfo->i_ext.i_data = NULL;
Namjae Jeon99600052013-01-19 11:17:14 +0900160 udf_clear_extent_cache(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400161 if (want_delete) {
Al Viro3aac2b62010-06-07 00:43:39 -0400162 udf_free_inode(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Ian Abbott5eec54f2012-09-05 17:44:31 +0100166static void udf_write_failed(struct address_space *mapping, loff_t to)
167{
168 struct inode *inode = mapping->host;
169 struct udf_inode_info *iinfo = UDF_I(inode);
170 loff_t isize = inode->i_size;
171
172 if (to > isize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700173 truncate_pagecache(inode, isize);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100174 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
175 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900176 udf_clear_extent_cache(inode);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100177 udf_truncate_extents(inode);
178 up_write(&iinfo->i_data_sem);
179 }
180 }
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int udf_writepage(struct page *page, struct writeback_control *wbc)
184{
185 return block_write_full_page(page, udf_get_block, wbc);
186}
187
Namjae Jeon378b8e12012-08-31 12:49:07 -0400188static int udf_writepages(struct address_space *mapping,
189 struct writeback_control *wbc)
190{
191 return mpage_writepages(mapping, wbc, udf_get_block);
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static int udf_readpage(struct file *file, struct page *page)
195{
Namjae Jeonbc112322011-10-03 14:02:59 +0900196 return mpage_readpage(page, udf_get_block);
197}
198
199static int udf_readpages(struct file *file, struct address_space *mapping,
200 struct list_head *pages, unsigned nr_pages)
201{
202 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Nick Pigginbe021ee2007-10-16 01:25:20 -0700205static int udf_write_begin(struct file *file, struct address_space *mapping,
206 loff_t pos, unsigned len, unsigned flags,
207 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200209 int ret;
210
211 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100212 if (unlikely(ret))
213 udf_write_failed(mapping, pos + len);
214 return ret;
215}
Jan Kara7e49b6f2010-10-22 00:30:26 +0200216
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700217static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Ian Abbott5eec54f2012-09-05 17:44:31 +0100218{
219 struct file *file = iocb->ki_filp;
220 struct address_space *mapping = file->f_mapping;
221 struct inode *inode = mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -0500222 size_t count = iov_iter_count(iter);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100223 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200224
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700225 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
Omar Sandoval6f673762015-03-16 04:33:52 -0700226 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700227 udf_write_failed(mapping, iocb->ki_pos + count);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231static sector_t udf_bmap(struct address_space *mapping, sector_t block)
232{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700233 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700236const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700237 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900238 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700239 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400240 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100241 .write_begin = udf_write_begin,
242 .write_end = generic_write_end,
243 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700244 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
Jan Karad2eb8c32011-12-10 02:30:48 +0100247/*
248 * Expand file stored in ICB to a normal one-block-file
249 *
250 * This function requires i_data_sem for writing and releases it.
251 * This function requires i_mutex held
252 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200253int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct page *page;
256 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800257 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct writeback_control udf_wbc = {
260 .sync_mode = WB_SYNC_NONE,
261 .nr_to_write = 1,
262 };
263
Al Viro59551022016-01-22 15:40:57 -0500264 WARN_ON_ONCE(!inode_is_locked(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800265 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800267 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800269 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200270 /* from now on we have normal address_space methods */
271 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100272 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100276 /*
277 * Release i_data_sem so that we can lock a page - page lock ranks
278 * above i_data_sem. i_mutex still protects us against file changes.
279 */
280 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Jan Kara7e49b6f2010-10-22 00:30:26 +0200282 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
283 if (!page)
284 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700285
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700286 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800288 memset(kaddr + iinfo->i_lenAlloc, 0x00,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300289 PAGE_SIZE - iinfo->i_lenAlloc);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800290 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
291 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 flush_dcache_page(page);
293 SetPageUptodate(page);
294 kunmap(page);
295 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100296 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800297 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
298 iinfo->i_lenAlloc);
299 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800301 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800303 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200304 /* from now on we have normal address_space methods */
305 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100306 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200307 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
308 if (err) {
309 /* Restore everything back so that we don't lose data... */
310 lock_page(page);
311 kaddr = kmap(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100312 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200313 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
314 inode->i_size);
315 kunmap(page);
316 unlock_page(page);
317 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
318 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100319 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200320 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300321 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200323
324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700327struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
328 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700331 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200332 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700334 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800337 loff_t f_pos = udf_ext0_offset(inode);
338 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800340 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
343 alloctype = ICBTAG_FLAG_AD_SHORT;
344 else
345 alloctype = ICBTAG_FLAG_AD_LONG;
346
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800348 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 mark_inode_dirty(inode);
350 return NULL;
351 }
352
353 /* alloc block, and copy data to it */
354 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800355 iinfo->i_location.partitionReferenceNum,
356 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!(*block))
358 return NULL;
359 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800360 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800361 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!newblock)
363 return NULL;
364 dbh = udf_tgetblk(inode->i_sb, newblock);
365 if (!dbh)
366 return NULL;
367 lock_buffer(dbh);
368 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
369 set_buffer_uptodate(dbh);
370 unlock_buffer(dbh);
371 mark_buffer_dirty_inode(dbh, inode);
372
Marcin Slusarz4b111112008-02-08 04:20:36 -0800373 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800374 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700375 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dfibh.soffset = dfibh.eoffset = 0;
377 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800378 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800379 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800380 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
381 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700382 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700383 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return NULL;
385 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800386 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sfi->descTag.tagLocation = cpu_to_le32(*block);
388 dfibh.soffset = dfibh.eoffset;
389 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
390 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
391 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800392 sfi->fileIdent +
393 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800394 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700395 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return NULL;
397 }
398 }
399 mark_buffer_dirty_inode(dbh, inode);
400
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800401 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
402 iinfo->i_lenAlloc);
403 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800405 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800406 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100407 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700408 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800409 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700410 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100411 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* UniqueID stuff */
413
Jan Kara3bf25cb2007-05-08 00:35:16 -0700414 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 mark_inode_dirty(inode);
416 return dbh;
417}
418
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700419static int udf_get_block(struct inode *inode, sector_t block,
420 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800423 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800424 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700426 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 phys = udf_block_map(inode, block);
428 if (phys)
429 map_bh(bh_result, inode->i_sb, phys);
430 return 0;
431 }
432
433 err = -EIO;
434 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800435 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100436
437 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800438 if (block == iinfo->i_next_alloc_block + 1) {
439 iinfo->i_next_alloc_block++;
440 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Namjae Jeon99600052013-01-19 11:17:14 +0900443 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100444 phys = inode_getblk(inode, block, &err, &new);
445 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (new)
449 set_buffer_new(bh_result);
450 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700451
452abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100453 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700457static struct buffer_head *udf_getblk(struct inode *inode, long block,
458 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700460 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct buffer_head dummy;
462
463 dummy.b_state = 0;
464 dummy.b_blocknr = -1000;
465 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700466 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700468 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 lock_buffer(bh);
470 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
471 set_buffer_uptodate(bh);
472 unlock_buffer(bh);
473 mark_buffer_dirty_inode(bh, inode);
474 }
475 return bh;
476 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return NULL;
479}
480
Jan Kara31170b62007-05-08 00:35:21 -0700481/* Extend the file by 'blocks' blocks, return the number of extents added */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200482static int udf_do_extend_file(struct inode *inode,
483 struct extent_position *last_pos,
484 struct kernel_long_ad *last_ext,
485 sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700486{
487 sector_t add;
488 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
489 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200490 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700491 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800492 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200493 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700494
495 /* The previous extent is fake and we should not extend by anything
496 * - there's nothing to do... */
497 if (!blocks && fake)
498 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700499
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800500 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700501 /* Round the last extent up to a multiple of block size */
502 if (last_ext->extLength & (sb->s_blocksize - 1)) {
503 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700504 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
505 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
506 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800507 iinfo->i_lenExtents =
508 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700509 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700510 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700511
Jan Kara31170b62007-05-08 00:35:21 -0700512 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800513 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
514 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700515 /* Save the extent so that we can reattach it to the end */
516 prealloc_loc = last_ext->extLocation;
517 prealloc_len = last_ext->extLength;
518 /* Mark the extent as a hole */
519 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700520 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700521 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800522 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700523 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700524
Jan Kara31170b62007-05-08 00:35:21 -0700525 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800526 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
527 EXT_NOT_RECORDED_NOT_ALLOCATED) {
528 add = ((1 << 30) - sb->s_blocksize -
529 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
530 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700531 if (add > blocks)
532 add = blocks;
533 blocks -= add;
534 last_ext->extLength += add << sb->s_blocksize_bits;
535 }
536
537 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200538 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700539 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700540 count++;
Jan Kara6c371572015-12-23 18:05:03 +0100541 } else {
542 struct kernel_lb_addr tmploc;
543 uint32_t tmplen;
544
Pekka Enberg97e961f2008-10-15 12:29:03 +0200545 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800546 last_ext->extLength, 1);
Jan Kara6c371572015-12-23 18:05:03 +0100547 /*
548 * We've rewritten the last extent but there may be empty
549 * indirect extent after it - enter it.
550 */
551 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
552 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700553
Jan Kara31170b62007-05-08 00:35:21 -0700554 /* Managed to do everything necessary? */
555 if (!blocks)
556 goto out;
557
558 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
559 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800560 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700561 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800562 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
563 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700564
Jan Kara31170b62007-05-08 00:35:21 -0700565 /* Create enough extents to cover the whole hole */
566 while (blocks > add) {
567 blocks -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200568 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
569 last_ext->extLength, 1);
570 if (err)
571 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700572 count++;
573 }
574 if (blocks) {
575 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700576 (blocks << sb->s_blocksize_bits);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200577 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
578 last_ext->extLength, 1);
579 if (err)
580 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700581 count++;
582 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700583
584out:
Jan Kara31170b62007-05-08 00:35:21 -0700585 /* Do we have some preallocated blocks saved? */
586 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200587 err = udf_add_aext(inode, last_pos, &prealloc_loc,
588 prealloc_len, 1);
589 if (err)
590 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700591 last_ext->extLocation = prealloc_loc;
592 last_ext->extLength = prealloc_len;
593 count++;
594 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700595
Jan Kara31170b62007-05-08 00:35:21 -0700596 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800597 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200598 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800599 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200600 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700601 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200602 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700603
Jan Kara31170b62007-05-08 00:35:21 -0700604 return count;
605}
606
Jan Kara7e49b6f2010-10-22 00:30:26 +0200607static int udf_extend_file(struct inode *inode, loff_t newsize)
608{
609
610 struct extent_position epos;
611 struct kernel_lb_addr eloc;
612 uint32_t elen;
613 int8_t etype;
614 struct super_block *sb = inode->i_sb;
615 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
616 int adsize;
617 struct udf_inode_info *iinfo = UDF_I(inode);
618 struct kernel_long_ad extent;
619 int err;
620
621 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
622 adsize = sizeof(struct short_ad);
623 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
624 adsize = sizeof(struct long_ad);
625 else
626 BUG();
627
628 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
629
630 /* File has extent covering the new size (could happen when extending
631 * inside a block)? */
632 if (etype != -1)
633 return 0;
634 if (newsize & (sb->s_blocksize - 1))
635 offset++;
636 /* Extended file just to the boundary of the last file block? */
637 if (offset == 0)
638 return 0;
639
640 /* Truncate is extending the file by 'offset' blocks */
641 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
642 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
643 /* File has no extents at all or has empty last
644 * indirect extent! Create a fake extent... */
645 extent.extLocation.logicalBlockNum = 0;
646 extent.extLocation.partitionReferenceNum = 0;
647 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
648 } else {
649 epos.offset -= adsize;
650 etype = udf_next_aext(inode, &epos, &extent.extLocation,
651 &extent.extLength, 0);
652 extent.extLength |= etype << 30;
653 }
654 err = udf_do_extend_file(inode, &epos, &extent, offset);
655 if (err < 0)
656 goto out;
657 err = 0;
658 iinfo->i_lenExtents = newsize;
659out:
660 brelse(epos.bh);
661 return err;
662}
663
Jan Kara7b0b0932011-12-10 01:43:33 +0100664static sector_t inode_getblk(struct inode *inode, sector_t block,
665 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200667 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700668 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700670 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200671 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700673 loff_t lbcount = 0, b_off = 0;
674 uint32_t newblocknum, newblock;
675 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800677 struct udf_inode_info *iinfo = UDF_I(inode);
678 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700679 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900680 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jan Kara7b0b0932011-12-10 01:43:33 +0100682 *err = 0;
683 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700684 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800685 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700686 prev_epos.bh = NULL;
687 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700688 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700691 alternate between laarr[0] and laarr[1] for locations of the
692 current extent, and the previous extent */
693 do {
694 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700695 brelse(prev_epos.bh);
696 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700697 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700699 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700700 brelse(cur_epos.bh);
701 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700702 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 lbcount += elen;
706
Jan Karaff116fc2007-05-08 00:35:14 -0700707 prev_epos.block = cur_epos.block;
708 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jan Karaff116fc2007-05-08 00:35:14 -0700710 prev_epos.offset = cur_epos.offset;
711 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Marcin Slusarz4b111112008-02-08 04:20:36 -0800713 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
714 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716
717 c = !c;
718
719 laarr[c].extLength = (etype << 30) | elen;
720 laarr[c].extLocation = eloc;
721
722 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
723 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700724 ((elen + inode->i_sb->s_blocksize - 1) >>
725 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700727 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 } while (lbcount + elen <= b_off);
729
730 b_off -= lbcount;
731 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700732 /*
733 * Move prev_epos and cur_epos into indirect extent if we are at
734 * the pointer to it
735 */
736 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
737 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700740 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700742 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
743 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700745 ((elen + inode->i_sb->s_blocksize - 1) &
746 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200747 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700749 brelse(prev_epos.bh);
750 brelse(cur_epos.bh);
751 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200752 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Jan Kara7b0b0932011-12-10 01:43:33 +0100753 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
Jan Kara31170b62007-05-08 00:35:21 -0700756 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700757 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700758 int ret;
Fabian Frederick69814982015-02-04 18:26:27 +0100759 isBeyondEOF = true;
Jan Kara31170b62007-05-08 00:35:21 -0700760 if (count) {
761 if (c)
762 laarr[0] = laarr[1];
763 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700764 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700765 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800766 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200767 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700768 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200769 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800770 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700771 startnum = (offset > 0);
772 }
773 /* Create extents for the hole between EOF and offset */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200774 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
775 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700776 brelse(prev_epos.bh);
777 brelse(cur_epos.bh);
778 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200779 *err = ret;
Jan Kara7b0b0932011-12-10 01:43:33 +0100780 return 0;
Jan Kara31170b62007-05-08 00:35:21 -0700781 }
782 c = 0;
783 offset = 0;
784 count += ret;
785 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800786 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
787 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700788 /* Is there any real extent? - otherwise we overwrite
789 * the fake one... */
790 if (count)
791 c = !c;
792 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700793 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800794 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200795 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700796 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700797 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700798 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700800 } else {
Fabian Frederick69814982015-02-04 18:26:27 +0100801 isBeyondEOF = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 endnum = startnum = ((count > 2) ? 2 : count);
803
Marcin Slusarz4b111112008-02-08 04:20:36 -0800804 /* if the current extent is in position 0,
805 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700806 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700807 laarr[2] = laarr[0];
808 laarr[0] = laarr[1];
809 laarr[1] = laarr[2];
810 c = 1;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Marcin Slusarz4b111112008-02-08 04:20:36 -0800813 /* if the current block is located in an extent,
814 read the next extent */
815 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
816 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700817 laarr[c + 1].extLength = (etype << 30) | elen;
818 laarr[c + 1].extLocation = eloc;
819 count++;
820 startnum++;
821 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800822 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 lastblock = 1;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700827 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800828 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800830 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800831 if (iinfo->i_next_alloc_block == block)
832 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700834 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800835 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800836 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Marcin Slusarz4b111112008-02-08 04:20:36 -0800839 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800840 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800841 goal, err);
842 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700843 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900844 brelse(cur_epos.bh);
845 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *err = -ENOSPC;
Jan Kara7b0b0932011-12-10 01:43:33 +0100847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900849 if (isBeyondEOF)
850 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Marcin Slusarz4b111112008-02-08 04:20:36 -0800853 /* if the extent the requsted block is located in contains multiple
854 * blocks, split the extent into at most three extents. blocks prior
855 * to requested block, requested block, and blocks after requested
856 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
858
859#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200860 /* We preallocate blocks only for regular files. It also makes sense
861 * for directories but there's a problem when to drop the
862 * preallocation. We might use some delayed work for that but I feel
863 * it's overengineering for a filesystem like UDF. */
864 if (S_ISREG(inode->i_mode))
865 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866#endif
867
868 /* merge any continuous blocks in laarr */
869 udf_merge_extents(inode, laarr, &endnum);
870
871 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700872 * of extents is greater than the old number, and deleting extents if
873 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700874 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Jan Kara3bf25cb2007-05-08 00:35:16 -0700876 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900877 brelse(cur_epos.bh);
878 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Marcin Slusarz4b111112008-02-08 04:20:36 -0800880 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800881 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100882 if (!newblock) {
883 *err = -EIO;
884 return 0;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800887 iinfo->i_next_alloc_block = block;
888 iinfo->i_next_alloc_goal = newblocknum;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700889 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (IS_SYNC(inode))
892 udf_sync_inode(inode);
893 else
894 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700895
Jan Kara7b0b0932011-12-10 01:43:33 +0100896 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700899static void udf_split_extents(struct inode *inode, int *c, int offset,
900 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200901 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700902 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800904 unsigned long blocksize = inode->i_sb->s_blocksize;
905 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800908 (laarr[*c].extLength >> 30) ==
909 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int curr = *c;
911 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800912 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int8_t etype = (laarr[curr].extLength >> 30);
914
Marcin Slusarz4b111112008-02-08 04:20:36 -0800915 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700916 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800917 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700918 laarr[curr + 2] = laarr[curr + 1];
919 laarr[curr + 1] = laarr[curr];
920 } else {
921 laarr[curr + 3] = laarr[curr + 1];
922 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700925 if (offset) {
926 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800927 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200928 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800929 0, offset);
930 laarr[curr].extLength =
931 EXT_NOT_RECORDED_NOT_ALLOCATED |
932 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800934 laarr[curr].extLocation.
935 partitionReferenceNum = 0;
936 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800938 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700939 curr++;
940 (*c)++;
941 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 laarr[curr].extLocation.logicalBlockNum = newblocknum;
945 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
946 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800947 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800949 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700950 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700952 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800954 laarr[curr].extLocation.logicalBlockNum +=
955 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700956 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800957 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700958 curr++;
959 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 }
962}
963
964static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200965 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700966 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 int start, length = 0, currlength = 0, i;
969
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700970 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!lastblock)
972 return;
973 else
974 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700975 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800976 if ((laarr[c + 1].extLength >> 30) ==
977 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700978 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800979 length = currlength =
980 (((laarr[c + 1].extLength &
981 UDF_EXTENT_LENGTH_MASK) +
982 inode->i_sb->s_blocksize - 1) >>
983 inode->i_sb->s_blocksize_bits);
984 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 start = c;
986 }
987
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700988 for (i = start + 1; i <= *endnum; i++) {
989 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (lastblock)
991 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800992 } else if ((laarr[i].extLength >> 30) ==
993 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
994 length += (((laarr[i].extLength &
995 UDF_EXTENT_LENGTH_MASK) +
996 inode->i_sb->s_blocksize - 1) >>
997 inode->i_sb->s_blocksize_bits);
998 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 break;
1000 }
1001
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001002 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001004 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001005 inode->i_sb->s_blocksize - 1) >>
1006 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001008 laarr[start].extLocation.partitionReferenceNum,
1009 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1010 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1011 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001012 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001013 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001015 (numalloc <<
1016 inode->i_sb->s_blocksize_bits);
1017 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001018 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001019 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001020 (*endnum)++;
1021 laarr[c + 1].extLocation.logicalBlockNum = next;
1022 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001023 laarr[c].extLocation.
1024 partitionReferenceNum;
1025 laarr[c + 1].extLength =
1026 EXT_NOT_RECORDED_ALLOCATED |
1027 (numalloc <<
1028 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001029 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001032 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001033 int elen = ((laarr[i].extLength &
1034 UDF_EXTENT_LENGTH_MASK) +
1035 inode->i_sb->s_blocksize - 1) >>
1036 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001038 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001040 (numalloc <<
1041 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001043 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001045 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001046 memmove(&laarr[i],
1047 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001048 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001049 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001050 i--;
1051 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001054 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001055 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 }
1058}
1059
1060static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001061 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001062 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001065 unsigned long blocksize = inode->i_sb->s_blocksize;
1066 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001068 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001069 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1070 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Marcin Slusarz4b111112008-02-08 04:20:36 -08001072 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1073 (((li->extLength >> 30) ==
1074 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1075 ((lip1->extLocation.logicalBlockNum -
1076 li->extLocation.logicalBlockNum) ==
1077 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1078 blocksize - 1) >> blocksize_bits)))) {
1079
1080 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1081 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1082 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1083 lip1->extLength = (lip1->extLength -
1084 (li->extLength &
1085 UDF_EXTENT_LENGTH_MASK) +
1086 UDF_EXTENT_LENGTH_MASK) &
1087 ~(blocksize - 1);
1088 li->extLength = (li->extLength &
1089 UDF_EXTENT_FLAG_MASK) +
1090 (UDF_EXTENT_LENGTH_MASK + 1) -
1091 blocksize;
1092 lip1->extLocation.logicalBlockNum =
1093 li->extLocation.logicalBlockNum +
1094 ((li->extLength &
1095 UDF_EXTENT_LENGTH_MASK) >>
1096 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001097 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001098 li->extLength = lip1->extLength +
1099 (((li->extLength &
1100 UDF_EXTENT_LENGTH_MASK) +
1101 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001102 if (*endnum > (i + 2))
1103 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001104 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001105 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001106 i--;
1107 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001109 } else if (((li->extLength >> 30) ==
1110 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1111 ((lip1->extLength >> 30) ==
1112 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001113 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001114 ((li->extLength &
1115 UDF_EXTENT_LENGTH_MASK) +
1116 blocksize - 1) >> blocksize_bits);
1117 li->extLocation.logicalBlockNum = 0;
1118 li->extLocation.partitionReferenceNum = 0;
1119
1120 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1121 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1122 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1123 lip1->extLength = (lip1->extLength -
1124 (li->extLength &
1125 UDF_EXTENT_LENGTH_MASK) +
1126 UDF_EXTENT_LENGTH_MASK) &
1127 ~(blocksize - 1);
1128 li->extLength = (li->extLength &
1129 UDF_EXTENT_FLAG_MASK) +
1130 (UDF_EXTENT_LENGTH_MASK + 1) -
1131 blocksize;
1132 } else {
1133 li->extLength = lip1->extLength +
1134 (((li->extLength &
1135 UDF_EXTENT_LENGTH_MASK) +
1136 blocksize - 1) & ~(blocksize - 1));
1137 if (*endnum > (i + 2))
1138 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001139 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001140 (*endnum - (i + 2)));
1141 i--;
1142 (*endnum)--;
1143 }
1144 } else if ((li->extLength >> 30) ==
1145 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1146 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001147 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001148 ((li->extLength &
1149 UDF_EXTENT_LENGTH_MASK) +
1150 blocksize - 1) >> blocksize_bits);
1151 li->extLocation.logicalBlockNum = 0;
1152 li->extLocation.partitionReferenceNum = 0;
1153 li->extLength = (li->extLength &
1154 UDF_EXTENT_LENGTH_MASK) |
1155 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 }
1158}
1159
1160static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001161 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001162 int startnum, int endnum,
1163 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001166 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 uint32_t tmplen;
1168
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001169 if (startnum > endnum) {
1170 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001171 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001172 laarr[i].extLength);
1173 } else if (startnum < endnum) {
1174 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001175 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001176 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001177 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001178 &laarr[i].extLength, 1);
1179 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 }
1182
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001183 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001184 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001185 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001186 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188}
1189
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001190struct buffer_head *udf_bread(struct inode *inode, int block,
1191 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001193 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 bh = udf_getblk(inode, block, create, err);
1196 if (!bh)
1197 return NULL;
1198
1199 if (buffer_uptodate(bh))
1200 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001201
Mike Christiedfec8a12016-06-05 14:31:44 -05001202 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 wait_on_buffer(bh);
1205 if (buffer_uptodate(bh))
1206 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 brelse(bh);
1209 *err = -EIO;
1210 return NULL;
1211}
1212
Jan Kara7e49b6f2010-10-22 00:30:26 +02001213int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001216 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +02001217 int bsize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001220 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001223 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001225 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001226 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001227 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001228 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1229 if (bsize <
1230 (udf_file_entry_alloc_offset(inode) + newsize)) {
1231 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001232 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001233 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001234 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001235 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001236 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001237 goto set_size;
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001240 err = udf_extend_file(inode, newsize);
1241 if (err) {
1242 up_write(&iinfo->i_data_sem);
1243 return err;
1244 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001245set_size:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001246 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001247 up_write(&iinfo->i_data_sem);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001248 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001249 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1250 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001251 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001252 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1253 0x00, bsize - newsize -
1254 udf_file_entry_alloc_offset(inode));
1255 iinfo->i_lenAlloc = newsize;
1256 truncate_setsize(inode, newsize);
1257 up_write(&iinfo->i_data_sem);
1258 goto update_time;
1259 }
1260 err = block_truncate_page(inode->i_mapping, newsize,
1261 udf_get_block);
1262 if (err)
1263 return err;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001264 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001265 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001266 truncate_setsize(inode, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001268 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001269 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001270update_time:
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001271 inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001273 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 else
1275 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Jan Karac03aa9f2014-09-04 14:06:55 +02001279/*
1280 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1281 * arbitrary - just that we hopefully don't limit any real use of rewritten
1282 * inode on write-once media but avoid looping for too long on corrupted media.
1283 */
1284#define UDF_MAX_ICB_NESTING 1024
1285
Jan Kara6174c2e2014-10-09 12:52:16 +02001286static int udf_read_inode(struct inode *inode, bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 struct buffer_head *bh = NULL;
1289 struct fileEntry *fe;
Jan Karabb7720a02014-09-04 13:32:50 +02001290 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001292 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Karabb7720a02014-09-04 13:32:50 +02001293 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001294 struct kernel_lb_addr *iloc = &iinfo->i_location;
Jan Karabb7720a02014-09-04 13:32:50 +02001295 unsigned int link_count;
Jan Karac03aa9f2014-09-04 14:06:55 +02001296 unsigned int indirections = 0;
Jan Kara79144952015-01-07 13:46:16 +01001297 int bs = inode->i_sb->s_blocksize;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001298 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Jan Karac03aa9f2014-09-04 14:06:55 +02001300reread:
Jan Kara6d3d5e82014-09-04 16:15:51 +02001301 if (iloc->logicalBlockNum >=
1302 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1303 udf_debug("block=%d, partition=%d out of range\n",
1304 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1305 return -EIO;
1306 }
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /*
1309 * Set defaults, but the inode is still incomplete!
1310 * Note: get_new_inode() sets the following on a new inode:
1311 * i_sb = sb
1312 * i_no = ino
1313 * i_flags = sb->s_flags
1314 * i_state = 0
1315 * clean_inode(): zero fills and sets
1316 * i_count = 1
1317 * i_nlink = 1
1318 * i_op = NULL;
1319 */
Jan Kara6d3d5e82014-09-04 16:15:51 +02001320 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001321 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001322 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001323 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
1326 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001327 ident != TAG_IDENT_USE) {
Joe Perches78ace702011-10-10 01:08:05 -07001328 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1329 inode->i_ino, ident);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
1333 fe = (struct fileEntry *)bh->b_data;
Jan Karabb7720a02014-09-04 13:32:50 +02001334 efe = (struct extendedFileEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001336 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001337 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Jan Kara6d3d5e82014-09-04 16:15:51 +02001339 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001340 if (ident == TAG_IDENT_IE && ibh) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001341 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001342 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001343
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001344 ie = (struct indirectEntry *)ibh->b_data;
1345 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001346
Jan Karac03aa9f2014-09-04 14:06:55 +02001347 if (ie->indirectICB.extLength) {
Jan Karac03aa9f2014-09-04 14:06:55 +02001348 brelse(ibh);
1349 memcpy(&iinfo->i_location, &loc,
1350 sizeof(struct kernel_lb_addr));
1351 if (++indirections > UDF_MAX_ICB_NESTING) {
1352 udf_err(inode->i_sb,
1353 "too many ICBs in ICB hierarchy"
1354 " (max %d supported)\n",
1355 UDF_MAX_ICB_NESTING);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001356 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001357 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001358 brelse(bh);
Jan Karac03aa9f2014-09-04 14:06:55 +02001359 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001361 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001362 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001363 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Joe Perches78ace702011-10-10 01:08:05 -07001364 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1365 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001366 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001368 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001369 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001370 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001371 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001373 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001374 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001375 iinfo->i_unique = 0;
1376 iinfo->i_lenEAttr = 0;
1377 iinfo->i_lenExtents = 0;
1378 iinfo->i_lenAlloc = 0;
1379 iinfo->i_next_alloc_block = 0;
1380 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001381 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001382 iinfo->i_efe = 1;
1383 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001384 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001385 sizeof(struct extendedFileEntry));
1386 if (ret)
1387 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001388 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001389 bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001390 bs - sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001391 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001392 iinfo->i_efe = 0;
1393 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001394 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001395 if (ret)
1396 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001397 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001398 bh->b_data + sizeof(struct fileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001399 bs - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001400 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001401 iinfo->i_efe = 0;
1402 iinfo->i_use = 1;
1403 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001404 ((struct unallocSpaceEntry *)bh->b_data)->
1405 lengthAllocDescs);
Jan Kara79144952015-01-07 13:46:16 +01001406 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001407 sizeof(struct unallocSpaceEntry));
1408 if (ret)
1409 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001410 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001411 bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara79144952015-01-07 13:46:16 +01001412 bs - sizeof(struct unallocSpaceEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
Jan Kara6d3d5e82014-09-04 16:15:51 +02001416 ret = -EIO;
Jan Karac03cad22010-10-20 22:17:28 +02001417 read_lock(&sbi->s_cred_lock);
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001418 i_uid_write(inode, le32_to_cpu(fe->uid));
1419 if (!uid_valid(inode->i_uid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001420 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1421 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001422 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001424 i_gid_write(inode, le32_to_cpu(fe->gid));
1425 if (!gid_valid(inode->i_gid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001426 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1427 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001428 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001430 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001431 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001432 inode->i_mode = sbi->s_fmode;
1433 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001434 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001435 inode->i_mode = sbi->s_dmode;
1436 else
1437 inode->i_mode = udf_convert_permissions(fe);
1438 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001439 read_unlock(&sbi->s_cred_lock);
1440
Miklos Szeredibfe86842011-10-28 14:13:29 +02001441 link_count = le16_to_cpu(fe->fileLinkCount);
Jan Kara4071b912014-09-04 16:19:47 +02001442 if (!link_count) {
Jan Kara6174c2e2014-10-09 12:52:16 +02001443 if (!hidden_inode) {
1444 ret = -ESTALE;
1445 goto out;
1446 }
1447 link_count = 1;
Jan Kara4071b912014-09-04 16:19:47 +02001448 }
Miklos Szeredibfe86842011-10-28 14:13:29 +02001449 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001450
1451 inode->i_size = le64_to_cpu(fe->informationLength);
1452 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001454 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001456 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Marcin Slusarz56774802008-02-10 11:25:31 +01001458 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001459 inode->i_atime = sbi->s_record_time;
1460
Marcin Slusarz56774802008-02-10 11:25:31 +01001461 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1462 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001463 inode->i_mtime = sbi->s_record_time;
1464
Marcin Slusarz56774802008-02-10 11:25:31 +01001465 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001466 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001468 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1469 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1470 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001471 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001472 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001473 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001474 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Marcin Slusarz56774802008-02-10 11:25:31 +01001476 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001477 inode->i_atime = sbi->s_record_time;
1478
Marcin Slusarz56774802008-02-10 11:25:31 +01001479 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1480 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001481 inode->i_mtime = sbi->s_record_time;
1482
Marcin Slusarz56774802008-02-10 11:25:31 +01001483 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001484 iinfo->i_crtime = sbi->s_record_time;
1485
Marcin Slusarz56774802008-02-10 11:25:31 +01001486 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001487 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001489 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1490 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1491 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001492 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Jan Kara470cca52014-09-04 16:26:19 +02001494 inode->i_generation = iinfo->i_unique;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Jan Kara23b133bd2015-01-07 13:49:08 +01001496 /*
1497 * Sanity check length of allocation descriptors and extended attrs to
1498 * avoid integer overflows
1499 */
1500 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1501 goto out;
1502 /* Now do exact checks */
1503 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1504 goto out;
Jan Karae1593322014-12-19 12:03:53 +01001505 /* Sanity checks for files in ICB so that we don't get confused later */
1506 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1507 /*
1508 * For file in ICB data is stored in allocation descriptor
1509 * so sizes should match
1510 */
1511 if (iinfo->i_lenAlloc != inode->i_size)
1512 goto out;
1513 /* File in ICB has to fit in there... */
Jan Kara79144952015-01-07 13:46:16 +01001514 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
Jan Karae1593322014-12-19 12:03:53 +01001515 goto out;
1516 }
1517
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001518 switch (fe->icbTag.fileType) {
1519 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001520 inode->i_op = &udf_dir_inode_operations;
1521 inode->i_fop = &udf_dir_operations;
1522 inode->i_mode |= S_IFDIR;
1523 inc_nlink(inode);
1524 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001525 case ICBTAG_FILE_TYPE_REALTIME:
1526 case ICBTAG_FILE_TYPE_REGULAR:
1527 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001528 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001529 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001530 inode->i_data.a_ops = &udf_adinicb_aops;
1531 else
1532 inode->i_data.a_ops = &udf_aops;
1533 inode->i_op = &udf_file_inode_operations;
1534 inode->i_fop = &udf_file_operations;
1535 inode->i_mode |= S_IFREG;
1536 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001537 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001538 inode->i_mode |= S_IFBLK;
1539 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001540 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001541 inode->i_mode |= S_IFCHR;
1542 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001543 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001544 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1545 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001546 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001547 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1548 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001549 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001550 inode->i_data.a_ops = &udf_symlink_aops;
Al Viroc73119c2015-11-13 20:33:18 -05001551 inode->i_op = &page_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05001552 inode_nohighmem(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001553 inode->i_mode = S_IFLNK | S_IRWXUGO;
1554 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001555 case ICBTAG_FILE_TYPE_MAIN:
1556 udf_debug("METADATA FILE-----\n");
1557 break;
1558 case ICBTAG_FILE_TYPE_MIRROR:
1559 udf_debug("METADATA MIRROR FILE-----\n");
1560 break;
1561 case ICBTAG_FILE_TYPE_BITMAP:
1562 udf_debug("METADATA BITMAP FILE-----\n");
1563 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001564 default:
Joe Perches78ace702011-10-10 01:08:05 -07001565 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1566 inode->i_ino, fe->icbTag.fileType);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001567 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001569 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001570 struct deviceSpec *dsea =
1571 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001572 if (dsea) {
1573 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001574 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1575 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001577 } else
Jan Kara6d3d5e82014-09-04 16:15:51 +02001578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001580 ret = 0;
1581out:
Jan Karabb7720a02014-09-04 13:32:50 +02001582 brelse(bh);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001583 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001586static int udf_alloc_i_data(struct inode *inode, size_t size)
1587{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001588 struct udf_inode_info *iinfo = UDF_I(inode);
1589 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001590
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001591 if (!iinfo->i_ext.i_data) {
Joe Perches78ace702011-10-10 01:08:05 -07001592 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1593 inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001594 return -ENOMEM;
1595 }
1596
1597 return 0;
1598}
1599
Al Virofaa17292011-07-26 03:18:29 -04001600static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Al Virofaa17292011-07-26 03:18:29 -04001602 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 uint32_t permissions;
1604 uint32_t flags;
1605
1606 permissions = le32_to_cpu(fe->permissions);
1607 flags = le16_to_cpu(fe->icbTag.flags);
1608
Marcin Slusarz4b111112008-02-08 04:20:36 -08001609 mode = ((permissions) & S_IRWXO) |
1610 ((permissions >> 2) & S_IRWXG) |
1611 ((permissions >> 4) & S_IRWXU) |
1612 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1613 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1614 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 return mode;
1617}
1618
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001619int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Jan Kara49521de2010-10-20 17:42:44 +02001621 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
Jan Kara49521de2010-10-20 17:42:44 +02001624static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 return udf_update_inode(inode, 1);
1627}
1628
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001629static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 struct buffer_head *bh = NULL;
1632 struct fileEntry *fe;
1633 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001634 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 uint32_t udfperms;
1636 uint16_t icbflags;
1637 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001639 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001640 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001641 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Jan Kara5833ded2010-01-08 16:52:59 +01001643 bh = udf_tgetblk(inode->i_sb,
1644 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001645 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001646 udf_debug("getblk failure\n");
Changwoo Min0fd2ba32015-03-22 19:17:49 -04001647 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Jan Karaaae917c2010-01-08 16:46:29 +01001650 lock_buffer(bh);
1651 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 fe = (struct fileEntry *)bh->b_data;
1653 efe = (struct extendedFileEntry *)bh->b_data;
1654
Jan Karaaae917c2010-01-08 16:46:29 +01001655 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001657 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001659 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001660 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001661 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001662 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001663 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001664 crclen = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001666 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
Phillip Susi4d6660e2006-03-07 21:55:24 -08001669 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1670 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001671 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001672 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Phillip Susi4d6660e2006-03-07 21:55:24 -08001674 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1675 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001676 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001677 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Marcin Slusarz4b111112008-02-08 04:20:36 -08001679 udfperms = ((inode->i_mode & S_IRWXO)) |
1680 ((inode->i_mode & S_IRWXG) << 2) |
1681 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Marcin Slusarz4b111112008-02-08 04:20:36 -08001683 udfperms |= (le32_to_cpu(fe->permissions) &
1684 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1685 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1686 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 fe->permissions = cpu_to_le32(udfperms);
1688
Jan Kara8a70ee32014-09-04 11:47:51 +02001689 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1691 else
1692 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1693
1694 fe->informationLength = cpu_to_le64(inode->i_size);
1695
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001696 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001697 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001698 struct deviceSpec *dsea =
1699 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001700 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001702 udf_add_extendedattr(inode,
1703 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001704 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 dsea->attrType = cpu_to_le32(12);
1706 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001707 dsea->attrLength = cpu_to_le32(
1708 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001709 sizeof(struct regid));
1710 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001712 eid = (struct regid *)dsea->impUse;
1713 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 strcpy(eid->ident, UDF_ID_DEVELOPER);
1715 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1716 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1717 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1718 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1719 }
1720
Steve Nickelb2527bf2012-02-16 12:53:53 -05001721 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1722 lb_recorded = 0; /* No extents => no blocks! */
1723 else
1724 lb_recorded =
1725 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1726 (blocksize_bits - 9);
1727
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001728 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001729 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001730 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001731 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001732 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Marcin Slusarz56774802008-02-10 11:25:31 +01001734 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1735 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1736 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001737 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1739 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1740 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001741 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1742 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1743 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001744 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1746 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001747 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001748 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001749 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001750 inode->i_sb->s_blocksize -
1751 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001753 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001755 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1756 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1757 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1758 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001759
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001760 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1761 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1762 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1763 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001764
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001765 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1766 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1767 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1768 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Marcin Slusarz56774802008-02-10 11:25:31 +01001770 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1771 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1772 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1773 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001775 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1777 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1778 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001779 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1780 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1781 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001782 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1784 crclen = sizeof(struct extendedFileEntry);
1785 }
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001786
1787finish:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001788 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 fe->icbTag.strategyType = cpu_to_le16(4096);
1790 fe->icbTag.strategyParameter = cpu_to_le16(1);
1791 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001792 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 fe->icbTag.strategyType = cpu_to_le16(4);
1794 fe->icbTag.numEntries = cpu_to_le16(1);
1795 }
1796
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001797 if (iinfo->i_use)
1798 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1799 else if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1801 else if (S_ISREG(inode->i_mode))
1802 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1803 else if (S_ISLNK(inode->i_mode))
1804 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1805 else if (S_ISBLK(inode->i_mode))
1806 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1807 else if (S_ISCHR(inode->i_mode))
1808 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1809 else if (S_ISFIFO(inode->i_mode))
1810 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1811 else if (S_ISSOCK(inode->i_mode))
1812 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1813
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001814 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001815 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1816 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1817 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1818 (le16_to_cpu(fe->icbTag.flags) &
1819 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1820 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001823 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 fe->descTag.descVersion = cpu_to_le16(3);
1825 else
1826 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001827 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001828 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001829 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001830 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001832 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001833 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001834 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jan Kara5833ded2010-01-08 16:52:59 +01001836 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001837 unlock_buffer(bh);
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /* write the data blocks */
1840 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001841 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001843 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001844 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1845 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 err = -EIO;
1847 }
1848 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001849 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return err;
1852}
1853
Jan Kara6174c2e2014-10-09 12:52:16 +02001854struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1855 bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1858 struct inode *inode = iget_locked(sb, block);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001859 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 if (!inode)
Jan Kara6d3d5e82014-09-04 16:15:51 +02001862 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Jan Kara6d3d5e82014-09-04 16:15:51 +02001864 if (!(inode->i_state & I_NEW))
1865 return inode;
1866
1867 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Jan Kara6174c2e2014-10-09 12:52:16 +02001868 err = udf_read_inode(inode, hidden_inode);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001869 if (err < 0) {
1870 iget_failed(inode);
1871 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001873 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Jan Karafcea62b2015-12-23 14:21:13 +01001878int udf_setup_indirect_aext(struct inode *inode, int block,
1879 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Jan Karafcea62b2015-12-23 14:21:13 +01001881 struct super_block *sb = inode->i_sb;
1882 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 struct allocExtDesc *aed;
Jan Karafcea62b2015-12-23 14:21:13 +01001884 struct extent_position nepos;
1885 struct kernel_lb_addr neloc;
1886 int ver, adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Jan Karafcea62b2015-12-23 14:21:13 +01001888 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1889 adsize = sizeof(struct short_ad);
1890 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1891 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 else
Arnd Bergmann4f1b1512016-01-01 15:21:54 +01001893 return -EIO;
Jan Karafcea62b2015-12-23 14:21:13 +01001894
1895 neloc.logicalBlockNum = block;
1896 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1897
1898 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1899 if (!bh)
1900 return -EIO;
1901 lock_buffer(bh);
1902 memset(bh->b_data, 0x00, sb->s_blocksize);
1903 set_buffer_uptodate(bh);
1904 unlock_buffer(bh);
1905 mark_buffer_dirty_inode(bh, inode);
1906
1907 aed = (struct allocExtDesc *)(bh->b_data);
1908 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1909 aed->previousAllocExtLocation =
1910 cpu_to_le32(epos->block.logicalBlockNum);
1911 }
1912 aed->lengthAllocDescs = cpu_to_le32(0);
1913 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1914 ver = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 else
Jan Karafcea62b2015-12-23 14:21:13 +01001916 ver = 2;
1917 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1918 sizeof(struct tag));
1919
1920 nepos.block = neloc;
1921 nepos.offset = sizeof(struct allocExtDesc);
1922 nepos.bh = bh;
1923
1924 /*
1925 * Do we have to copy current last extent to make space for indirect
1926 * one?
1927 */
1928 if (epos->offset + adsize > sb->s_blocksize) {
1929 struct kernel_lb_addr cp_loc;
1930 uint32_t cp_len;
1931 int cp_type;
1932
1933 epos->offset -= adsize;
1934 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1935 cp_len |= ((uint32_t)cp_type) << 30;
1936
1937 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1938 udf_write_aext(inode, epos, &nepos.block,
1939 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1940 } else {
1941 __udf_add_aext(inode, epos, &nepos.block,
1942 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1943 }
1944
1945 brelse(epos->bh);
1946 *epos = nepos;
1947
1948 return 0;
1949}
1950
1951/*
1952 * Append extent at the given position - should be the first free one in inode
1953 * / indirect extent. This function assumes there is enough space in the inode
1954 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1955 */
1956int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1957 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1958{
1959 struct udf_inode_info *iinfo = UDF_I(inode);
1960 struct allocExtDesc *aed;
1961 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001963 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001964 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001965 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001966 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001968 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Jan Karafcea62b2015-12-23 14:21:13 +01001970 if (!epos->bh) {
1971 WARN_ON(iinfo->i_lenAlloc !=
1972 epos->offset - udf_file_entry_alloc_offset(inode));
1973 } else {
1974 aed = (struct allocExtDesc *)epos->bh->b_data;
1975 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1976 epos->offset - sizeof(struct allocExtDesc));
1977 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
Jan Kara7e49b6f2010-10-22 00:30:26 +02001980 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001982 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001983 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001985 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001986 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001987 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001988 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1989 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1990 udf_update_tag(epos->bh->b_data,
1991 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001993 udf_update_tag(epos->bh->b_data,
1994 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001995 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997
Jan Kara7e49b6f2010-10-22 00:30:26 +02001998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Jan Karafcea62b2015-12-23 14:21:13 +01002001/*
2002 * Append extent at given position - should be the first free one in inode
2003 * / indirect extent. Takes care of allocating and linking indirect blocks.
2004 */
2005int udf_add_aext(struct inode *inode, struct extent_position *epos,
2006 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2007{
2008 int adsize;
2009 struct super_block *sb = inode->i_sb;
2010
2011 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2012 adsize = sizeof(struct short_ad);
2013 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2014 adsize = sizeof(struct long_ad);
2015 else
2016 return -EIO;
2017
2018 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2019 int err;
2020 int new_block;
2021
2022 new_block = udf_new_block(sb, NULL,
2023 epos->block.partitionReferenceNum,
2024 epos->block.logicalBlockNum, &err);
2025 if (!new_block)
2026 return -ENOSPC;
2027
2028 err = udf_setup_indirect_aext(inode, new_block, epos);
2029 if (err)
2030 return err;
2031 }
2032
2033 return __udf_add_aext(inode, epos, eloc, elen, inc);
2034}
2035
Jan Kara7e49b6f2010-10-22 00:30:26 +02002036void udf_write_aext(struct inode *inode, struct extent_position *epos,
2037 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 int adsize;
2040 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002041 struct short_ad *sad;
2042 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002043 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Jan Karaff116fc2007-05-08 00:35:14 -07002045 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002046 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002047 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002048 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 else
Jan Karaff116fc2007-05-08 00:35:14 -07002050 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002052 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002053 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002054 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002055 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002056 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002057 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002058 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002059 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002060 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002061 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002062 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002063 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002064 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002065 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002066 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002070 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002071 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002072 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002073 struct allocExtDesc *aed =
2074 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002075 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002076 le32_to_cpu(aed->lengthAllocDescs) +
2077 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Jan Karaff116fc2007-05-08 00:35:14 -07002079 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002080 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002085 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Vegard Nossumb0918d92015-12-11 15:54:16 +01002088/*
2089 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2090 * someone does some weird stuff.
2091 */
2092#define UDF_MAX_INDIR_EXTS 16
2093
Marcin Slusarz4b111112008-02-08 04:20:36 -08002094int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002095 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 int8_t etype;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002098 unsigned int indirections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Jan Karaff116fc2007-05-08 00:35:14 -07002100 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002101 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002102 int block;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002103
2104 if (++indirections > UDF_MAX_INDIR_EXTS) {
2105 udf_err(inode->i_sb,
2106 "too many indirect extents in inode %lu\n",
2107 inode->i_ino);
2108 return -1;
2109 }
2110
Jan Karaff116fc2007-05-08 00:35:14 -07002111 epos->block = *eloc;
2112 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002113 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002114 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002115 epos->bh = udf_tread(inode->i_sb, block);
2116 if (!epos->bh) {
2117 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return -1;
2119 }
2120 }
2121
2122 return etype;
2123}
2124
Marcin Slusarz4b111112008-02-08 04:20:36 -08002125int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002126 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int alen;
2129 int8_t etype;
2130 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002131 struct short_ad *sad;
2132 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002133 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002134
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002135 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002136 if (!epos->offset)
2137 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002138 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002139 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002140 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002141 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002142 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002143 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002144 if (!epos->offset)
2145 epos->offset = sizeof(struct allocExtDesc);
2146 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002147 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002148 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2149 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002152 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002153 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002154 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2155 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002157 etype = le32_to_cpu(sad->extLength) >> 30;
2158 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002159 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002160 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002161 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2162 break;
2163 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002164 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2165 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002166 return -1;
2167 etype = le32_to_cpu(lad->extLength) >> 30;
2168 *eloc = lelb_to_cpu(lad->extLocation);
2169 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2170 break;
2171 default:
Joe Perchesa983f362011-10-10 01:08:07 -07002172 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002173 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175
2176 return etype;
2177}
2178
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002179static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002180 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002182 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 uint32_t oelen;
2184 int8_t etype;
2185
Jan Karaff116fc2007-05-08 00:35:14 -07002186 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002187 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002189 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002190 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 neloc = oeloc;
2192 nelen = (etype << 30) | oelen;
2193 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002194 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002195 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return (nelen >> 30);
2198}
2199
Marcin Slusarz4b111112008-02-08 04:20:36 -08002200int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002201 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Jan Karaff116fc2007-05-08 00:35:14 -07002203 struct extent_position oepos;
2204 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 int8_t etype;
2206 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002207 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002209 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002210 get_bh(epos.bh);
2211 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002214 iinfo = UDF_I(inode);
2215 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002216 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002217 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002218 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 else
2220 adsize = 0;
2221
Jan Karaff116fc2007-05-08 00:35:14 -07002222 oepos = epos;
2223 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return -1;
2225
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002226 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002227 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002228 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002229 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002230 brelse(oepos.bh);
2231 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002232 oepos.bh = epos.bh;
2233 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
2235 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002236 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 elen = 0;
2238
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002239 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002240 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2241 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2242 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002243 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002244 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002246 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002247 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002248 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002249 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002250 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002251 udf_update_tag(oepos.bh->b_data,
2252 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002254 udf_update_tag(oepos.bh->b_data,
2255 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002256 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002258 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002259 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002260 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002261 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002263 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002264 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002265 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002266 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002267 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002268 udf_update_tag(oepos.bh->b_data,
2269 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002271 udf_update_tag(oepos.bh->b_data,
2272 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002273 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002276
Jan Kara3bf25cb2007-05-08 00:35:16 -07002277 brelse(epos.bh);
2278 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return (elen >> 30);
2281}
2282
Marcin Slusarz4b111112008-02-08 04:20:36 -08002283int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002284 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002285 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002287 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002288 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002289 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002291 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002293 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002294 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2295 pos->offset = 0;
2296 pos->block = iinfo->i_location;
2297 pos->bh = NULL;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002300 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002301 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2302 if (etype == -1) {
2303 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002304 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return -1;
2306 }
2307 lbcount += *elen;
2308 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002309 /* update extent cache */
2310 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002311 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 return etype;
2314}
2315
Jan Kara60448b12007-05-08 00:35:13 -07002316long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002318 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002319 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002320 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002321 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 int ret;
2323
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002324 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Marcin Slusarz4b111112008-02-08 04:20:36 -08002326 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2327 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002328 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 else
2330 ret = 0;
2331
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002332 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002333 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2336 return udf_fixed_to_variable(ret);
2337 else
2338 return ret;
2339}