blob: 6afac3d561ac81f6f5861f86a0dc4eb2a7d1fca2 [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
Omar Sandoval22c61862015-03-16 04:33:53 -0700217static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
Al Virod8d3d942014-03-04 21:27:34 -0500218 loff_t offset)
Ian Abbott5eec54f2012-09-05 17:44:31 +0100219{
220 struct file *file = iocb->ki_filp;
221 struct address_space *mapping = file->f_mapping;
222 struct inode *inode = mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -0500223 size_t count = iov_iter_count(iter);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100224 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200225
Omar Sandoval17f8c842015-03-16 04:33:50 -0700226 ret = blockdev_direct_IO(iocb, inode, iter, offset, udf_get_block);
Omar Sandoval6f673762015-03-16 04:33:52 -0700227 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
Al Viroa6cbcd42014-03-04 22:38:00 -0500228 udf_write_failed(mapping, offset + count);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232static sector_t udf_bmap(struct address_space *mapping, sector_t block)
233{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700234 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700237const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700238 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900239 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700240 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400241 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100242 .write_begin = udf_write_begin,
243 .write_end = generic_write_end,
244 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700245 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Jan Karad2eb8c32011-12-10 02:30:48 +0100248/*
249 * Expand file stored in ICB to a normal one-block-file
250 *
251 * This function requires i_data_sem for writing and releases it.
252 * This function requires i_mutex held
253 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200254int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct page *page;
257 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800258 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200259 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct writeback_control udf_wbc = {
261 .sync_mode = WB_SYNC_NONE,
262 .nr_to_write = 1,
263 };
264
Jan Kara09ebb172014-02-18 12:00:21 +0100265 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800266 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800268 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800270 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200271 /* from now on we have normal address_space methods */
272 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100273 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100277 /*
278 * Release i_data_sem so that we can lock a page - page lock ranks
279 * above i_data_sem. i_mutex still protects us against file changes.
280 */
281 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Jan Kara7e49b6f2010-10-22 00:30:26 +0200283 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
284 if (!page)
285 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700286
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700287 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800289 memset(kaddr + iinfo->i_lenAlloc, 0x00,
290 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
291 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
292 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 flush_dcache_page(page);
294 SetPageUptodate(page);
295 kunmap(page);
296 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100297 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800298 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
299 iinfo->i_lenAlloc);
300 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800302 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800304 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200305 /* from now on we have normal address_space methods */
306 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100307 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200308 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
309 if (err) {
310 /* Restore everything back so that we don't lose data... */
311 lock_page(page);
312 kaddr = kmap(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100313 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200314 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
315 inode->i_size);
316 kunmap(page);
317 unlock_page(page);
318 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
319 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100320 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200324
325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
329 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700332 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200333 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700335 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800338 loff_t f_pos = udf_ext0_offset(inode);
339 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800341 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
344 alloctype = ICBTAG_FLAG_AD_SHORT;
345 else
346 alloctype = ICBTAG_FLAG_AD_LONG;
347
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700348 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800349 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 mark_inode_dirty(inode);
351 return NULL;
352 }
353
354 /* alloc block, and copy data to it */
355 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800356 iinfo->i_location.partitionReferenceNum,
357 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!(*block))
359 return NULL;
360 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800361 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800362 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!newblock)
364 return NULL;
365 dbh = udf_tgetblk(inode->i_sb, newblock);
366 if (!dbh)
367 return NULL;
368 lock_buffer(dbh);
369 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
370 set_buffer_uptodate(dbh);
371 unlock_buffer(dbh);
372 mark_buffer_dirty_inode(dbh, inode);
373
Marcin Slusarz4b111112008-02-08 04:20:36 -0800374 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800375 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700376 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 dfibh.soffset = dfibh.eoffset = 0;
378 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800379 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800380 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800381 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
382 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700383 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700384 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return NULL;
386 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800387 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 sfi->descTag.tagLocation = cpu_to_le32(*block);
389 dfibh.soffset = dfibh.eoffset;
390 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
391 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
392 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800393 sfi->fileIdent +
394 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800395 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700396 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return NULL;
398 }
399 }
400 mark_buffer_dirty_inode(dbh, inode);
401
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800402 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
403 iinfo->i_lenAlloc);
404 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800406 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800407 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100408 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700409 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800410 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700411 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100412 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* UniqueID stuff */
414
Jan Kara3bf25cb2007-05-08 00:35:16 -0700415 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 mark_inode_dirty(inode);
417 return dbh;
418}
419
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700420static int udf_get_block(struct inode *inode, sector_t block,
421 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800424 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800425 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 phys = udf_block_map(inode, block);
429 if (phys)
430 map_bh(bh_result, inode->i_sb, phys);
431 return 0;
432 }
433
434 err = -EIO;
435 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800436 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100437
438 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800439 if (block == iinfo->i_next_alloc_block + 1) {
440 iinfo->i_next_alloc_block++;
441 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
Namjae Jeon99600052013-01-19 11:17:14 +0900444 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100445 phys = inode_getblk(inode, block, &err, &new);
446 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (new)
450 set_buffer_new(bh_result);
451 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700452
453abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100454 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700458static struct buffer_head *udf_getblk(struct inode *inode, long block,
459 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700461 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct buffer_head dummy;
463
464 dummy.b_state = 0;
465 dummy.b_blocknr = -1000;
466 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700467 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700469 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 lock_buffer(bh);
471 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
472 set_buffer_uptodate(bh);
473 unlock_buffer(bh);
474 mark_buffer_dirty_inode(bh, inode);
475 }
476 return bh;
477 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return NULL;
480}
481
Jan Kara31170b62007-05-08 00:35:21 -0700482/* Extend the file by 'blocks' blocks, return the number of extents added */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200483static int udf_do_extend_file(struct inode *inode,
484 struct extent_position *last_pos,
485 struct kernel_long_ad *last_ext,
486 sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700487{
488 sector_t add;
489 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
490 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200491 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700492 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800493 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200494 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700495
496 /* The previous extent is fake and we should not extend by anything
497 * - there's nothing to do... */
498 if (!blocks && fake)
499 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700500
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800501 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700502 /* Round the last extent up to a multiple of block size */
503 if (last_ext->extLength & (sb->s_blocksize - 1)) {
504 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700505 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
506 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
507 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800508 iinfo->i_lenExtents =
509 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700510 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700511 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700512
Jan Kara31170b62007-05-08 00:35:21 -0700513 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800514 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
515 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700516 /* Save the extent so that we can reattach it to the end */
517 prealloc_loc = last_ext->extLocation;
518 prealloc_len = last_ext->extLength;
519 /* Mark the extent as a hole */
520 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700521 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700522 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800523 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700524 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700525
Jan Kara31170b62007-05-08 00:35:21 -0700526 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800527 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
528 EXT_NOT_RECORDED_NOT_ALLOCATED) {
529 add = ((1 << 30) - sb->s_blocksize -
530 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
531 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700532 if (add > blocks)
533 add = blocks;
534 blocks -= add;
535 last_ext->extLength += add << sb->s_blocksize_bits;
536 }
537
538 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200539 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700540 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700541 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800542 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200543 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800544 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700545
Jan Kara31170b62007-05-08 00:35:21 -0700546 /* Managed to do everything necessary? */
547 if (!blocks)
548 goto out;
549
550 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
551 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800552 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700553 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800554 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
555 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700556
Jan Kara31170b62007-05-08 00:35:21 -0700557 /* Create enough extents to cover the whole hole */
558 while (blocks > add) {
559 blocks -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200560 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
561 last_ext->extLength, 1);
562 if (err)
563 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700564 count++;
565 }
566 if (blocks) {
567 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700568 (blocks << sb->s_blocksize_bits);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200569 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
570 last_ext->extLength, 1);
571 if (err)
572 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700573 count++;
574 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700575
576out:
Jan Kara31170b62007-05-08 00:35:21 -0700577 /* Do we have some preallocated blocks saved? */
578 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200579 err = udf_add_aext(inode, last_pos, &prealloc_loc,
580 prealloc_len, 1);
581 if (err)
582 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700583 last_ext->extLocation = prealloc_loc;
584 last_ext->extLength = prealloc_len;
585 count++;
586 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700587
Jan Kara31170b62007-05-08 00:35:21 -0700588 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800589 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200590 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800591 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200592 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700593 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200594 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700595
Jan Kara31170b62007-05-08 00:35:21 -0700596 return count;
597}
598
Jan Kara7e49b6f2010-10-22 00:30:26 +0200599static int udf_extend_file(struct inode *inode, loff_t newsize)
600{
601
602 struct extent_position epos;
603 struct kernel_lb_addr eloc;
604 uint32_t elen;
605 int8_t etype;
606 struct super_block *sb = inode->i_sb;
607 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
608 int adsize;
609 struct udf_inode_info *iinfo = UDF_I(inode);
610 struct kernel_long_ad extent;
611 int err;
612
613 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
614 adsize = sizeof(struct short_ad);
615 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
616 adsize = sizeof(struct long_ad);
617 else
618 BUG();
619
620 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
621
622 /* File has extent covering the new size (could happen when extending
623 * inside a block)? */
624 if (etype != -1)
625 return 0;
626 if (newsize & (sb->s_blocksize - 1))
627 offset++;
628 /* Extended file just to the boundary of the last file block? */
629 if (offset == 0)
630 return 0;
631
632 /* Truncate is extending the file by 'offset' blocks */
633 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
634 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
635 /* File has no extents at all or has empty last
636 * indirect extent! Create a fake extent... */
637 extent.extLocation.logicalBlockNum = 0;
638 extent.extLocation.partitionReferenceNum = 0;
639 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
640 } else {
641 epos.offset -= adsize;
642 etype = udf_next_aext(inode, &epos, &extent.extLocation,
643 &extent.extLength, 0);
644 extent.extLength |= etype << 30;
645 }
646 err = udf_do_extend_file(inode, &epos, &extent, offset);
647 if (err < 0)
648 goto out;
649 err = 0;
650 iinfo->i_lenExtents = newsize;
651out:
652 brelse(epos.bh);
653 return err;
654}
655
Jan Kara7b0b0932011-12-10 01:43:33 +0100656static sector_t inode_getblk(struct inode *inode, sector_t block,
657 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200659 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700660 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700662 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200663 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700665 loff_t lbcount = 0, b_off = 0;
666 uint32_t newblocknum, newblock;
667 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800669 struct udf_inode_info *iinfo = UDF_I(inode);
670 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700671 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900672 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Jan Kara7b0b0932011-12-10 01:43:33 +0100674 *err = 0;
675 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700676 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800677 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700678 prev_epos.bh = NULL;
679 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700680 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700683 alternate between laarr[0] and laarr[1] for locations of the
684 current extent, and the previous extent */
685 do {
686 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700687 brelse(prev_epos.bh);
688 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700689 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700691 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700692 brelse(cur_epos.bh);
693 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700694 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 lbcount += elen;
698
Jan Karaff116fc2007-05-08 00:35:14 -0700699 prev_epos.block = cur_epos.block;
700 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Jan Karaff116fc2007-05-08 00:35:14 -0700702 prev_epos.offset = cur_epos.offset;
703 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Marcin Slusarz4b111112008-02-08 04:20:36 -0800705 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
706 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708
709 c = !c;
710
711 laarr[c].extLength = (etype << 30) | elen;
712 laarr[c].extLocation = eloc;
713
714 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
715 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700716 ((elen + inode->i_sb->s_blocksize - 1) >>
717 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700719 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 } while (lbcount + elen <= b_off);
721
722 b_off -= lbcount;
723 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700724 /*
725 * Move prev_epos and cur_epos into indirect extent if we are at
726 * the pointer to it
727 */
728 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
729 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700732 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700734 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
735 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700737 ((elen + inode->i_sb->s_blocksize - 1) &
738 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200739 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700741 brelse(prev_epos.bh);
742 brelse(cur_epos.bh);
743 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200744 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Jan Kara7b0b0932011-12-10 01:43:33 +0100745 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Jan Kara31170b62007-05-08 00:35:21 -0700748 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700749 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700750 int ret;
Fabian Frederick69814982015-02-04 18:26:27 +0100751 isBeyondEOF = true;
Jan Kara31170b62007-05-08 00:35:21 -0700752 if (count) {
753 if (c)
754 laarr[0] = laarr[1];
755 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700756 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700757 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800758 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200759 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700760 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200761 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800762 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700763 startnum = (offset > 0);
764 }
765 /* Create extents for the hole between EOF and offset */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200766 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
767 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700768 brelse(prev_epos.bh);
769 brelse(cur_epos.bh);
770 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200771 *err = ret;
Jan Kara7b0b0932011-12-10 01:43:33 +0100772 return 0;
Jan Kara31170b62007-05-08 00:35:21 -0700773 }
774 c = 0;
775 offset = 0;
776 count += ret;
777 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800778 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
779 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700780 /* Is there any real extent? - otherwise we overwrite
781 * the fake one... */
782 if (count)
783 c = !c;
784 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700785 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800786 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200787 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700788 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700789 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700790 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792 } else {
Fabian Frederick69814982015-02-04 18:26:27 +0100793 isBeyondEOF = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 endnum = startnum = ((count > 2) ? 2 : count);
795
Marcin Slusarz4b111112008-02-08 04:20:36 -0800796 /* if the current extent is in position 0,
797 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700798 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700799 laarr[2] = laarr[0];
800 laarr[0] = laarr[1];
801 laarr[1] = laarr[2];
802 c = 1;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Marcin Slusarz4b111112008-02-08 04:20:36 -0800805 /* if the current block is located in an extent,
806 read the next extent */
807 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
808 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700809 laarr[c + 1].extLength = (etype << 30) | elen;
810 laarr[c + 1].extLocation = eloc;
811 count++;
812 startnum++;
813 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800814 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 lastblock = 1;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700819 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800820 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800822 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800823 if (iinfo->i_next_alloc_block == block)
824 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700826 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800827 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800828 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Marcin Slusarz4b111112008-02-08 04:20:36 -0800831 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800832 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800833 goal, err);
834 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700835 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900836 brelse(cur_epos.bh);
837 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 *err = -ENOSPC;
Jan Kara7b0b0932011-12-10 01:43:33 +0100839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900841 if (isBeyondEOF)
842 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Marcin Slusarz4b111112008-02-08 04:20:36 -0800845 /* if the extent the requsted block is located in contains multiple
846 * blocks, split the extent into at most three extents. blocks prior
847 * to requested block, requested block, and blocks after requested
848 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
850
851#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200852 /* We preallocate blocks only for regular files. It also makes sense
853 * for directories but there's a problem when to drop the
854 * preallocation. We might use some delayed work for that but I feel
855 * it's overengineering for a filesystem like UDF. */
856 if (S_ISREG(inode->i_mode))
857 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858#endif
859
860 /* merge any continuous blocks in laarr */
861 udf_merge_extents(inode, laarr, &endnum);
862
863 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700864 * of extents is greater than the old number, and deleting extents if
865 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700866 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Jan Kara3bf25cb2007-05-08 00:35:16 -0700868 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900869 brelse(cur_epos.bh);
870 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Marcin Slusarz4b111112008-02-08 04:20:36 -0800872 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800873 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100874 if (!newblock) {
875 *err = -EIO;
876 return 0;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800879 iinfo->i_next_alloc_block = block;
880 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 inode->i_ctime = current_fs_time(inode->i_sb);
882
883 if (IS_SYNC(inode))
884 udf_sync_inode(inode);
885 else
886 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700887
Jan Kara7b0b0932011-12-10 01:43:33 +0100888 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700891static void udf_split_extents(struct inode *inode, int *c, int offset,
892 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200893 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700894 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800896 unsigned long blocksize = inode->i_sb->s_blocksize;
897 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800900 (laarr[*c].extLength >> 30) ==
901 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int curr = *c;
903 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800904 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 int8_t etype = (laarr[curr].extLength >> 30);
906
Marcin Slusarz4b111112008-02-08 04:20:36 -0800907 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700908 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800909 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700910 laarr[curr + 2] = laarr[curr + 1];
911 laarr[curr + 1] = laarr[curr];
912 } else {
913 laarr[curr + 3] = laarr[curr + 1];
914 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700917 if (offset) {
918 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800919 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200920 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800921 0, offset);
922 laarr[curr].extLength =
923 EXT_NOT_RECORDED_NOT_ALLOCATED |
924 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800926 laarr[curr].extLocation.
927 partitionReferenceNum = 0;
928 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800930 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700931 curr++;
932 (*c)++;
933 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 laarr[curr].extLocation.logicalBlockNum = newblocknum;
937 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
938 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800939 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800941 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700942 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700944 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800946 laarr[curr].extLocation.logicalBlockNum +=
947 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700948 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800949 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700950 curr++;
951 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953 }
954}
955
956static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200957 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700958 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 int start, length = 0, currlength = 0, i;
961
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700962 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!lastblock)
964 return;
965 else
966 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700967 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800968 if ((laarr[c + 1].extLength >> 30) ==
969 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700970 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800971 length = currlength =
972 (((laarr[c + 1].extLength &
973 UDF_EXTENT_LENGTH_MASK) +
974 inode->i_sb->s_blocksize - 1) >>
975 inode->i_sb->s_blocksize_bits);
976 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 start = c;
978 }
979
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700980 for (i = start + 1; i <= *endnum; i++) {
981 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (lastblock)
983 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800984 } else if ((laarr[i].extLength >> 30) ==
985 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
986 length += (((laarr[i].extLength &
987 UDF_EXTENT_LENGTH_MASK) +
988 inode->i_sb->s_blocksize - 1) >>
989 inode->i_sb->s_blocksize_bits);
990 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992 }
993
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700994 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700996 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800997 inode->i_sb->s_blocksize - 1) >>
998 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001000 laarr[start].extLocation.partitionReferenceNum,
1001 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1002 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1003 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001004 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001005 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001007 (numalloc <<
1008 inode->i_sb->s_blocksize_bits);
1009 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001010 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001011 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001012 (*endnum)++;
1013 laarr[c + 1].extLocation.logicalBlockNum = next;
1014 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001015 laarr[c].extLocation.
1016 partitionReferenceNum;
1017 laarr[c + 1].extLength =
1018 EXT_NOT_RECORDED_ALLOCATED |
1019 (numalloc <<
1020 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001021 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001024 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001025 int elen = ((laarr[i].extLength &
1026 UDF_EXTENT_LENGTH_MASK) +
1027 inode->i_sb->s_blocksize - 1) >>
1028 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001030 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001032 (numalloc <<
1033 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001035 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001037 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001038 memmove(&laarr[i],
1039 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001040 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001041 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001042 i--;
1043 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001046 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001047 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 }
1050}
1051
1052static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001053 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001054 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001057 unsigned long blocksize = inode->i_sb->s_blocksize;
1058 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001060 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001061 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1062 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Marcin Slusarz4b111112008-02-08 04:20:36 -08001064 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1065 (((li->extLength >> 30) ==
1066 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1067 ((lip1->extLocation.logicalBlockNum -
1068 li->extLocation.logicalBlockNum) ==
1069 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1070 blocksize - 1) >> blocksize_bits)))) {
1071
1072 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1073 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1074 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1075 lip1->extLength = (lip1->extLength -
1076 (li->extLength &
1077 UDF_EXTENT_LENGTH_MASK) +
1078 UDF_EXTENT_LENGTH_MASK) &
1079 ~(blocksize - 1);
1080 li->extLength = (li->extLength &
1081 UDF_EXTENT_FLAG_MASK) +
1082 (UDF_EXTENT_LENGTH_MASK + 1) -
1083 blocksize;
1084 lip1->extLocation.logicalBlockNum =
1085 li->extLocation.logicalBlockNum +
1086 ((li->extLength &
1087 UDF_EXTENT_LENGTH_MASK) >>
1088 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001089 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001090 li->extLength = lip1->extLength +
1091 (((li->extLength &
1092 UDF_EXTENT_LENGTH_MASK) +
1093 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001094 if (*endnum > (i + 2))
1095 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001096 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001097 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001098 i--;
1099 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001101 } else if (((li->extLength >> 30) ==
1102 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1103 ((lip1->extLength >> 30) ==
1104 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001105 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001106 ((li->extLength &
1107 UDF_EXTENT_LENGTH_MASK) +
1108 blocksize - 1) >> blocksize_bits);
1109 li->extLocation.logicalBlockNum = 0;
1110 li->extLocation.partitionReferenceNum = 0;
1111
1112 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1113 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1114 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1115 lip1->extLength = (lip1->extLength -
1116 (li->extLength &
1117 UDF_EXTENT_LENGTH_MASK) +
1118 UDF_EXTENT_LENGTH_MASK) &
1119 ~(blocksize - 1);
1120 li->extLength = (li->extLength &
1121 UDF_EXTENT_FLAG_MASK) +
1122 (UDF_EXTENT_LENGTH_MASK + 1) -
1123 blocksize;
1124 } else {
1125 li->extLength = lip1->extLength +
1126 (((li->extLength &
1127 UDF_EXTENT_LENGTH_MASK) +
1128 blocksize - 1) & ~(blocksize - 1));
1129 if (*endnum > (i + 2))
1130 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001131 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001132 (*endnum - (i + 2)));
1133 i--;
1134 (*endnum)--;
1135 }
1136 } else if ((li->extLength >> 30) ==
1137 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1138 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001139 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001140 ((li->extLength &
1141 UDF_EXTENT_LENGTH_MASK) +
1142 blocksize - 1) >> blocksize_bits);
1143 li->extLocation.logicalBlockNum = 0;
1144 li->extLocation.partitionReferenceNum = 0;
1145 li->extLength = (li->extLength &
1146 UDF_EXTENT_LENGTH_MASK) |
1147 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 }
1150}
1151
1152static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001153 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001154 int startnum, int endnum,
1155 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001158 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 uint32_t tmplen;
1160
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001161 if (startnum > endnum) {
1162 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001163 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001164 laarr[i].extLength);
1165 } else if (startnum < endnum) {
1166 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001167 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001168 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001169 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001170 &laarr[i].extLength, 1);
1171 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 }
1174
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001175 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001176 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001177 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001178 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180}
1181
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001182struct buffer_head *udf_bread(struct inode *inode, int block,
1183 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001185 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 bh = udf_getblk(inode, block, create, err);
1188 if (!bh)
1189 return NULL;
1190
1191 if (buffer_uptodate(bh))
1192 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 wait_on_buffer(bh);
1197 if (buffer_uptodate(bh))
1198 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 brelse(bh);
1201 *err = -EIO;
1202 return NULL;
1203}
1204
Jan Kara7e49b6f2010-10-22 00:30:26 +02001205int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001208 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +02001209 int bsize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001212 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001215 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001217 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001218 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001219 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001220 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1221 if (bsize <
1222 (udf_file_entry_alloc_offset(inode) + newsize)) {
1223 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001224 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001225 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001226 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001227 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001228 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001229 goto set_size;
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001232 err = udf_extend_file(inode, newsize);
1233 if (err) {
1234 up_write(&iinfo->i_data_sem);
1235 return err;
1236 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001237set_size:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001238 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001239 up_write(&iinfo->i_data_sem);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001240 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001241 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1242 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001243 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001244 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1245 0x00, bsize - newsize -
1246 udf_file_entry_alloc_offset(inode));
1247 iinfo->i_lenAlloc = newsize;
1248 truncate_setsize(inode, newsize);
1249 up_write(&iinfo->i_data_sem);
1250 goto update_time;
1251 }
1252 err = block_truncate_page(inode->i_mapping, newsize,
1253 udf_get_block);
1254 if (err)
1255 return err;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001256 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001257 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001258 truncate_setsize(inode, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001260 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001261 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001262update_time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1264 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001265 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 else
1267 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Jan Karac03aa9f2014-09-04 14:06:55 +02001271/*
1272 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1273 * arbitrary - just that we hopefully don't limit any real use of rewritten
1274 * inode on write-once media but avoid looping for too long on corrupted media.
1275 */
1276#define UDF_MAX_ICB_NESTING 1024
1277
Jan Kara6174c2e2014-10-09 12:52:16 +02001278static int udf_read_inode(struct inode *inode, bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct buffer_head *bh = NULL;
1281 struct fileEntry *fe;
Jan Karabb7720a02014-09-04 13:32:50 +02001282 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001284 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Karabb7720a02014-09-04 13:32:50 +02001285 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001286 struct kernel_lb_addr *iloc = &iinfo->i_location;
Jan Karabb7720a02014-09-04 13:32:50 +02001287 unsigned int link_count;
Jan Karac03aa9f2014-09-04 14:06:55 +02001288 unsigned int indirections = 0;
Jan Kara79144952015-01-07 13:46:16 +01001289 int bs = inode->i_sb->s_blocksize;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001290 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Jan Karac03aa9f2014-09-04 14:06:55 +02001292reread:
Jan Kara6d3d5e82014-09-04 16:15:51 +02001293 if (iloc->logicalBlockNum >=
1294 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1295 udf_debug("block=%d, partition=%d out of range\n",
1296 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1297 return -EIO;
1298 }
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /*
1301 * Set defaults, but the inode is still incomplete!
1302 * Note: get_new_inode() sets the following on a new inode:
1303 * i_sb = sb
1304 * i_no = ino
1305 * i_flags = sb->s_flags
1306 * i_state = 0
1307 * clean_inode(): zero fills and sets
1308 * i_count = 1
1309 * i_nlink = 1
1310 * i_op = NULL;
1311 */
Jan Kara6d3d5e82014-09-04 16:15:51 +02001312 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001313 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001314 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001315 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
1318 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001319 ident != TAG_IDENT_USE) {
Joe Perches78ace702011-10-10 01:08:05 -07001320 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1321 inode->i_ino, ident);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
1325 fe = (struct fileEntry *)bh->b_data;
Jan Karabb7720a02014-09-04 13:32:50 +02001326 efe = (struct extendedFileEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001328 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001329 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Jan Kara6d3d5e82014-09-04 16:15:51 +02001331 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001332 if (ident == TAG_IDENT_IE && ibh) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001333 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001334 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001335
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001336 ie = (struct indirectEntry *)ibh->b_data;
1337 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001338
Jan Karac03aa9f2014-09-04 14:06:55 +02001339 if (ie->indirectICB.extLength) {
Jan Karac03aa9f2014-09-04 14:06:55 +02001340 brelse(ibh);
1341 memcpy(&iinfo->i_location, &loc,
1342 sizeof(struct kernel_lb_addr));
1343 if (++indirections > UDF_MAX_ICB_NESTING) {
1344 udf_err(inode->i_sb,
1345 "too many ICBs in ICB hierarchy"
1346 " (max %d supported)\n",
1347 UDF_MAX_ICB_NESTING);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001348 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001349 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001350 brelse(bh);
Jan Karac03aa9f2014-09-04 14:06:55 +02001351 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001353 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001354 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001355 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Joe Perches78ace702011-10-10 01:08:05 -07001356 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1357 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001360 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001361 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001362 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001363 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001365 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001366 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001367 iinfo->i_unique = 0;
1368 iinfo->i_lenEAttr = 0;
1369 iinfo->i_lenExtents = 0;
1370 iinfo->i_lenAlloc = 0;
1371 iinfo->i_next_alloc_block = 0;
1372 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001373 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001374 iinfo->i_efe = 1;
1375 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001376 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001377 sizeof(struct extendedFileEntry));
1378 if (ret)
1379 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001380 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001381 bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001382 bs - sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001383 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001384 iinfo->i_efe = 0;
1385 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001386 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001387 if (ret)
1388 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001389 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001390 bh->b_data + sizeof(struct fileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001391 bs - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001392 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001393 iinfo->i_efe = 0;
1394 iinfo->i_use = 1;
1395 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001396 ((struct unallocSpaceEntry *)bh->b_data)->
1397 lengthAllocDescs);
Jan Kara79144952015-01-07 13:46:16 +01001398 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001399 sizeof(struct unallocSpaceEntry));
1400 if (ret)
1401 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001402 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001403 bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara79144952015-01-07 13:46:16 +01001404 bs - sizeof(struct unallocSpaceEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Jan Kara6d3d5e82014-09-04 16:15:51 +02001408 ret = -EIO;
Jan Karac03cad22010-10-20 22:17:28 +02001409 read_lock(&sbi->s_cred_lock);
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001410 i_uid_write(inode, le32_to_cpu(fe->uid));
1411 if (!uid_valid(inode->i_uid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001412 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1413 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001414 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001416 i_gid_write(inode, le32_to_cpu(fe->gid));
1417 if (!gid_valid(inode->i_gid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001418 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1419 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001420 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001422 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001423 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001424 inode->i_mode = sbi->s_fmode;
1425 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001426 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001427 inode->i_mode = sbi->s_dmode;
1428 else
1429 inode->i_mode = udf_convert_permissions(fe);
1430 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001431 read_unlock(&sbi->s_cred_lock);
1432
Miklos Szeredibfe86842011-10-28 14:13:29 +02001433 link_count = le16_to_cpu(fe->fileLinkCount);
Jan Kara4071b912014-09-04 16:19:47 +02001434 if (!link_count) {
Jan Kara6174c2e2014-10-09 12:52:16 +02001435 if (!hidden_inode) {
1436 ret = -ESTALE;
1437 goto out;
1438 }
1439 link_count = 1;
Jan Kara4071b912014-09-04 16:19:47 +02001440 }
Miklos Szeredibfe86842011-10-28 14:13:29 +02001441 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001442
1443 inode->i_size = le64_to_cpu(fe->informationLength);
1444 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001446 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001448 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Marcin Slusarz56774802008-02-10 11:25:31 +01001450 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001451 inode->i_atime = sbi->s_record_time;
1452
Marcin Slusarz56774802008-02-10 11:25:31 +01001453 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1454 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001455 inode->i_mtime = sbi->s_record_time;
1456
Marcin Slusarz56774802008-02-10 11:25:31 +01001457 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001458 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001460 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1461 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1462 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001463 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001464 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001465 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001466 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Marcin Slusarz56774802008-02-10 11:25:31 +01001468 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001469 inode->i_atime = sbi->s_record_time;
1470
Marcin Slusarz56774802008-02-10 11:25:31 +01001471 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1472 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001473 inode->i_mtime = sbi->s_record_time;
1474
Marcin Slusarz56774802008-02-10 11:25:31 +01001475 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001476 iinfo->i_crtime = sbi->s_record_time;
1477
Marcin Slusarz56774802008-02-10 11:25:31 +01001478 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001479 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001481 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1482 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1483 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001484 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Jan Kara470cca52014-09-04 16:26:19 +02001486 inode->i_generation = iinfo->i_unique;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Jan Kara23b133bd2015-01-07 13:49:08 +01001488 /*
1489 * Sanity check length of allocation descriptors and extended attrs to
1490 * avoid integer overflows
1491 */
1492 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1493 goto out;
1494 /* Now do exact checks */
1495 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1496 goto out;
Jan Karae1593322014-12-19 12:03:53 +01001497 /* Sanity checks for files in ICB so that we don't get confused later */
1498 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1499 /*
1500 * For file in ICB data is stored in allocation descriptor
1501 * so sizes should match
1502 */
1503 if (iinfo->i_lenAlloc != inode->i_size)
1504 goto out;
1505 /* File in ICB has to fit in there... */
Jan Kara79144952015-01-07 13:46:16 +01001506 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
Jan Karae1593322014-12-19 12:03:53 +01001507 goto out;
1508 }
1509
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001510 switch (fe->icbTag.fileType) {
1511 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001512 inode->i_op = &udf_dir_inode_operations;
1513 inode->i_fop = &udf_dir_operations;
1514 inode->i_mode |= S_IFDIR;
1515 inc_nlink(inode);
1516 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001517 case ICBTAG_FILE_TYPE_REALTIME:
1518 case ICBTAG_FILE_TYPE_REGULAR:
1519 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001520 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001521 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001522 inode->i_data.a_ops = &udf_adinicb_aops;
1523 else
1524 inode->i_data.a_ops = &udf_aops;
1525 inode->i_op = &udf_file_inode_operations;
1526 inode->i_fop = &udf_file_operations;
1527 inode->i_mode |= S_IFREG;
1528 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001529 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001530 inode->i_mode |= S_IFBLK;
1531 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001532 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001533 inode->i_mode |= S_IFCHR;
1534 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001535 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001536 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1537 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001538 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001539 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1540 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001541 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001542 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001543 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001544 inode->i_mode = S_IFLNK | S_IRWXUGO;
1545 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001546 case ICBTAG_FILE_TYPE_MAIN:
1547 udf_debug("METADATA FILE-----\n");
1548 break;
1549 case ICBTAG_FILE_TYPE_MIRROR:
1550 udf_debug("METADATA MIRROR FILE-----\n");
1551 break;
1552 case ICBTAG_FILE_TYPE_BITMAP:
1553 udf_debug("METADATA BITMAP FILE-----\n");
1554 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001555 default:
Joe Perches78ace702011-10-10 01:08:05 -07001556 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1557 inode->i_ino, fe->icbTag.fileType);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001560 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001561 struct deviceSpec *dsea =
1562 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001563 if (dsea) {
1564 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001565 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1566 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001568 } else
Jan Kara6d3d5e82014-09-04 16:15:51 +02001569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001571 ret = 0;
1572out:
Jan Karabb7720a02014-09-04 13:32:50 +02001573 brelse(bh);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001574 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001577static int udf_alloc_i_data(struct inode *inode, size_t size)
1578{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001579 struct udf_inode_info *iinfo = UDF_I(inode);
1580 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001581
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001582 if (!iinfo->i_ext.i_data) {
Joe Perches78ace702011-10-10 01:08:05 -07001583 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1584 inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001585 return -ENOMEM;
1586 }
1587
1588 return 0;
1589}
1590
Al Virofaa17292011-07-26 03:18:29 -04001591static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Al Virofaa17292011-07-26 03:18:29 -04001593 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 uint32_t permissions;
1595 uint32_t flags;
1596
1597 permissions = le32_to_cpu(fe->permissions);
1598 flags = le16_to_cpu(fe->icbTag.flags);
1599
Marcin Slusarz4b111112008-02-08 04:20:36 -08001600 mode = ((permissions) & S_IRWXO) |
1601 ((permissions >> 2) & S_IRWXG) |
1602 ((permissions >> 4) & S_IRWXU) |
1603 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1604 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1605 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 return mode;
1608}
1609
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001610int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Jan Kara49521de2010-10-20 17:42:44 +02001612 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Jan Kara49521de2010-10-20 17:42:44 +02001615static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 return udf_update_inode(inode, 1);
1618}
1619
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001620static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 struct buffer_head *bh = NULL;
1623 struct fileEntry *fe;
1624 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001625 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 uint32_t udfperms;
1627 uint16_t icbflags;
1628 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001630 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001631 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001632 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Jan Kara5833ded2010-01-08 16:52:59 +01001634 bh = udf_tgetblk(inode->i_sb,
1635 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001636 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001637 udf_debug("getblk failure\n");
Changwoo Min0fd2ba32015-03-22 19:17:49 -04001638 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640
Jan Karaaae917c2010-01-08 16:46:29 +01001641 lock_buffer(bh);
1642 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 fe = (struct fileEntry *)bh->b_data;
1644 efe = (struct extendedFileEntry *)bh->b_data;
1645
Jan Karaaae917c2010-01-08 16:46:29 +01001646 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001648 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001650 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001651 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001652 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001653 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001654 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1655 use->descTag.tagLocation =
1656 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001657 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001658 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001660 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001661 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001662 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001663 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Jan Karaaae917c2010-01-08 16:46:29 +01001665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
Phillip Susi4d6660e2006-03-07 21:55:24 -08001668 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1669 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001670 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001671 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Phillip Susi4d6660e2006-03-07 21:55:24 -08001673 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1674 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001675 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001676 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Marcin Slusarz4b111112008-02-08 04:20:36 -08001678 udfperms = ((inode->i_mode & S_IRWXO)) |
1679 ((inode->i_mode & S_IRWXG) << 2) |
1680 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Marcin Slusarz4b111112008-02-08 04:20:36 -08001682 udfperms |= (le32_to_cpu(fe->permissions) &
1683 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1684 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1685 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 fe->permissions = cpu_to_le32(udfperms);
1687
Jan Kara8a70ee32014-09-04 11:47:51 +02001688 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1690 else
1691 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1692
1693 fe->informationLength = cpu_to_le64(inode->i_size);
1694
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001695 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001696 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001697 struct deviceSpec *dsea =
1698 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001699 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001701 udf_add_extendedattr(inode,
1702 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001703 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 dsea->attrType = cpu_to_le32(12);
1705 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001706 dsea->attrLength = cpu_to_le32(
1707 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001708 sizeof(struct regid));
1709 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001711 eid = (struct regid *)dsea->impUse;
1712 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 strcpy(eid->ident, UDF_ID_DEVELOPER);
1714 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1715 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1716 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1717 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1718 }
1719
Steve Nickelb2527bf2012-02-16 12:53:53 -05001720 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1721 lb_recorded = 0; /* No extents => no blocks! */
1722 else
1723 lb_recorded =
1724 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1725 (blocksize_bits - 9);
1726
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001727 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001728 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001729 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001730 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001731 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Marcin Slusarz56774802008-02-10 11:25:31 +01001733 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1734 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1735 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001736 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1738 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1739 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001740 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1741 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1742 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001743 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1745 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001746 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001747 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001748 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001749 inode->i_sb->s_blocksize -
1750 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001752 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001754 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1755 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1756 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1757 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001758
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001759 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1760 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1761 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1762 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001763
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001764 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1765 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1766 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1767 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Marcin Slusarz56774802008-02-10 11:25:31 +01001769 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1770 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1771 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1772 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001774 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1776 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1777 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001778 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1779 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1780 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001781 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1783 crclen = sizeof(struct extendedFileEntry);
1784 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001785 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 fe->icbTag.strategyType = cpu_to_le16(4096);
1787 fe->icbTag.strategyParameter = cpu_to_le16(1);
1788 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001789 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 fe->icbTag.strategyType = cpu_to_le16(4);
1791 fe->icbTag.numEntries = cpu_to_le16(1);
1792 }
1793
1794 if (S_ISDIR(inode->i_mode))
1795 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1796 else if (S_ISREG(inode->i_mode))
1797 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1798 else if (S_ISLNK(inode->i_mode))
1799 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1800 else if (S_ISBLK(inode->i_mode))
1801 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1802 else if (S_ISCHR(inode->i_mode))
1803 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1804 else if (S_ISFIFO(inode->i_mode))
1805 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1806 else if (S_ISSOCK(inode->i_mode))
1807 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1808
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001809 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001810 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1811 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1812 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1813 (le16_to_cpu(fe->icbTag.flags) &
1814 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1815 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001818 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 fe->descTag.descVersion = cpu_to_le16(3);
1820 else
1821 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001822 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001823 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001824 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001825 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001827 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001828 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001829 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Jan Karaaae917c2010-01-08 16:46:29 +01001831out:
Jan Kara5833ded2010-01-08 16:52:59 +01001832 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001833 unlock_buffer(bh);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /* write the data blocks */
1836 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001837 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001839 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001840 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1841 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 err = -EIO;
1843 }
1844 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001845 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return err;
1848}
1849
Jan Kara6174c2e2014-10-09 12:52:16 +02001850struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1851 bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1854 struct inode *inode = iget_locked(sb, block);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001855 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 if (!inode)
Jan Kara6d3d5e82014-09-04 16:15:51 +02001858 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Jan Kara6d3d5e82014-09-04 16:15:51 +02001860 if (!(inode->i_state & I_NEW))
1861 return inode;
1862
1863 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Jan Kara6174c2e2014-10-09 12:52:16 +02001864 err = udf_read_inode(inode, hidden_inode);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001865 if (err < 0) {
1866 iget_failed(inode);
1867 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001869 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Jan Kara7e49b6f2010-10-22 00:30:26 +02001874int udf_add_aext(struct inode *inode, struct extent_position *epos,
1875 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001878 struct short_ad *sad = NULL;
1879 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 struct allocExtDesc *aed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001882 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Jan Karaff116fc2007-05-08 00:35:14 -07001884 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001885 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001886 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001887 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 else
Jan Karaff116fc2007-05-08 00:35:14 -07001889 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001891 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001892 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001893 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001894 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001896 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001898 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001899 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 struct buffer_head *nbh;
1901 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001902 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Marcin Slusarz4b111112008-02-08 04:20:36 -08001904 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1905 obloc.partitionReferenceNum,
1906 obloc.logicalBlockNum, &err);
1907 if (!epos->block.logicalBlockNum)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001908 return -ENOSPC;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001909 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001910 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001911 0));
1912 if (!nbh)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001913 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 lock_buffer(nbh);
1915 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1916 set_buffer_uptodate(nbh);
1917 unlock_buffer(nbh);
1918 mark_buffer_dirty_inode(nbh, inode);
1919
1920 aed = (struct allocExtDesc *)(nbh->b_data);
1921 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001922 aed->previousAllocExtLocation =
1923 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001924 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001925 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 aed->lengthAllocDescs = cpu_to_le32(adsize);
1927 sptr = ptr - adsize;
1928 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1929 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001930 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001931 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001932 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 aed->lengthAllocDescs = cpu_to_le32(0);
1934 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001935 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001937 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001938 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001939 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001940 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001941 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 mark_inode_dirty(inode);
1943 }
1944 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001945 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001947 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 else
1949 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001950 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001951 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001952 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001953 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001954 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1955 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001956 sad->extPosition =
1957 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001958 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001959 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001960 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001961 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1962 inode->i_sb->s_blocksize);
1963 lad->extLocation = cpu_to_lelb(epos->block);
1964 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001967 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001968 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001969 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001970 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001972 udf_update_tag(epos->bh->b_data,
1973 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001974 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001975 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001976 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001978 }
Jan Karaff116fc2007-05-08 00:35:14 -07001979 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981
Jan Kara7e49b6f2010-10-22 00:30:26 +02001982 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001984 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001985 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001987 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001988 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001989 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001990 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1991 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1992 udf_update_tag(epos->bh->b_data,
1993 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001995 udf_update_tag(epos->bh->b_data,
1996 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001997 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999
Jan Kara7e49b6f2010-10-22 00:30:26 +02002000 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
Jan Kara7e49b6f2010-10-22 00:30:26 +02002003void udf_write_aext(struct inode *inode, struct extent_position *epos,
2004 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 int adsize;
2007 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002008 struct short_ad *sad;
2009 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002010 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Jan Karaff116fc2007-05-08 00:35:14 -07002012 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002013 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002014 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002015 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 else
Jan Karaff116fc2007-05-08 00:35:14 -07002017 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002019 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002020 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002021 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002022 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002023 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002024 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002025 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002026 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002027 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002028 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002029 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002030 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002031 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002032 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002033 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002034 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002037 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002038 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002039 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002040 struct allocExtDesc *aed =
2041 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002042 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002043 le32_to_cpu(aed->lengthAllocDescs) +
2044 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Jan Karaff116fc2007-05-08 00:35:14 -07002046 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002047 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002052 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Marcin Slusarz4b111112008-02-08 04:20:36 -08002055int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002056 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 int8_t etype;
2059
Jan Karaff116fc2007-05-08 00:35:14 -07002060 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002061 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002062 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07002063 epos->block = *eloc;
2064 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002065 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002066 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002067 epos->bh = udf_tread(inode->i_sb, block);
2068 if (!epos->bh) {
2069 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return -1;
2071 }
2072 }
2073
2074 return etype;
2075}
2076
Marcin Slusarz4b111112008-02-08 04:20:36 -08002077int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002078 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 int alen;
2081 int8_t etype;
2082 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002083 struct short_ad *sad;
2084 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002085 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002086
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002087 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002088 if (!epos->offset)
2089 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002090 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002091 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002092 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002093 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002094 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002095 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002096 if (!epos->offset)
2097 epos->offset = sizeof(struct allocExtDesc);
2098 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002099 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002100 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2101 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002104 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002105 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002106 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2107 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002109 etype = le32_to_cpu(sad->extLength) >> 30;
2110 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002111 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002112 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002113 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2114 break;
2115 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002116 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2117 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002118 return -1;
2119 etype = le32_to_cpu(lad->extLength) >> 30;
2120 *eloc = lelb_to_cpu(lad->extLocation);
2121 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2122 break;
2123 default:
Joe Perchesa983f362011-10-10 01:08:07 -07002124 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002125 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
2127
2128 return etype;
2129}
2130
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002131static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002132 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002134 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 uint32_t oelen;
2136 int8_t etype;
2137
Jan Karaff116fc2007-05-08 00:35:14 -07002138 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002139 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002141 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002142 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 neloc = oeloc;
2144 nelen = (etype << 30) | oelen;
2145 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002146 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002147 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return (nelen >> 30);
2150}
2151
Marcin Slusarz4b111112008-02-08 04:20:36 -08002152int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002153 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Jan Karaff116fc2007-05-08 00:35:14 -07002155 struct extent_position oepos;
2156 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 int8_t etype;
2158 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002159 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002161 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002162 get_bh(epos.bh);
2163 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002166 iinfo = UDF_I(inode);
2167 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002168 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002169 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002170 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 else
2172 adsize = 0;
2173
Jan Karaff116fc2007-05-08 00:35:14 -07002174 oepos = epos;
2175 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return -1;
2177
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002178 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002179 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002180 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002181 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002182 brelse(oepos.bh);
2183 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002184 oepos.bh = epos.bh;
2185 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002188 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 elen = 0;
2190
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002191 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002192 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2193 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2194 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002195 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002196 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002198 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002199 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002200 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002201 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002202 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002203 udf_update_tag(oepos.bh->b_data,
2204 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002206 udf_update_tag(oepos.bh->b_data,
2207 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002208 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002210 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002211 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002212 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002213 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002215 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002216 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002217 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002218 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002219 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002220 udf_update_tag(oepos.bh->b_data,
2221 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002223 udf_update_tag(oepos.bh->b_data,
2224 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002225 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002228
Jan Kara3bf25cb2007-05-08 00:35:16 -07002229 brelse(epos.bh);
2230 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return (elen >> 30);
2233}
2234
Marcin Slusarz4b111112008-02-08 04:20:36 -08002235int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002236 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002237 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002239 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002240 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002241 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002243 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002245 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002246 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2247 pos->offset = 0;
2248 pos->block = iinfo->i_location;
2249 pos->bh = NULL;
2250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002252 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002253 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2254 if (etype == -1) {
2255 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002256 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return -1;
2258 }
2259 lbcount += *elen;
2260 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002261 /* update extent cache */
2262 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002263 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 return etype;
2266}
2267
Jan Kara60448b12007-05-08 00:35:13 -07002268long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002270 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002271 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002272 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002273 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 int ret;
2275
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002276 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Marcin Slusarz4b111112008-02-08 04:20:36 -08002278 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2279 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002280 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 else
2282 ret = 0;
2283
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002284 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002285 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2288 return udf_fixed_to_variable(ret);
2289 else
2290 return ret;
2291}