blob: 7a12e48ad8196d51273fcc2ca66e89d077eb66ee [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>
36#include <linux/buffer_head.h>
37#include <linux/writeback.h>
38#include <linux/slab.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020039#include <linux/crc-itu-t.h>
Namjae Jeonbc112322011-10-03 14:02:59 +090040#include <linux/mpage.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);
53static void udf_fill_inode(struct inode *, struct buffer_head *);
Jan Kara49521de2010-10-20 17:42:44 +020054static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070055static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara7b0b0932011-12-10 01:43:33 +010056static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070057static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020058 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020060 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020062 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020064 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020066 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070067 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
69
Namjae Jeon99600052013-01-19 11:17:14 +090070static void __udf_clear_extent_cache(struct inode *inode)
71{
72 struct udf_inode_info *iinfo = UDF_I(inode);
73
74 if (iinfo->cached_extent.lstart != -1) {
75 brelse(iinfo->cached_extent.epos.bh);
76 iinfo->cached_extent.lstart = -1;
77 }
78}
79
80/* Invalidate extent cache */
81static void udf_clear_extent_cache(struct inode *inode)
82{
83 struct udf_inode_info *iinfo = UDF_I(inode);
84
85 spin_lock(&iinfo->i_extent_cache_lock);
86 __udf_clear_extent_cache(inode);
87 spin_unlock(&iinfo->i_extent_cache_lock);
88}
89
90/* Return contents of extent cache */
91static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
92 loff_t *lbcount, struct extent_position *pos)
93{
94 struct udf_inode_info *iinfo = UDF_I(inode);
95 int ret = 0;
96
97 spin_lock(&iinfo->i_extent_cache_lock);
98 if ((iinfo->cached_extent.lstart <= bcount) &&
99 (iinfo->cached_extent.lstart != -1)) {
100 /* Cache hit */
101 *lbcount = iinfo->cached_extent.lstart;
102 memcpy(pos, &iinfo->cached_extent.epos,
103 sizeof(struct extent_position));
104 if (pos->bh)
105 get_bh(pos->bh);
106 ret = 1;
107 }
108 spin_unlock(&iinfo->i_extent_cache_lock);
109 return ret;
110}
111
112/* Add extent to extent cache */
113static void udf_update_extent_cache(struct inode *inode, loff_t estart,
114 struct extent_position *pos, int next_epos)
115{
116 struct udf_inode_info *iinfo = UDF_I(inode);
117
118 spin_lock(&iinfo->i_extent_cache_lock);
119 /* Invalidate previously cached extent */
120 __udf_clear_extent_cache(inode);
121 if (pos->bh)
122 get_bh(pos->bh);
123 memcpy(&iinfo->cached_extent.epos, pos,
124 sizeof(struct extent_position));
125 iinfo->cached_extent.lstart = estart;
126 if (next_epos)
127 switch (iinfo->i_alloc_type) {
128 case ICBTAG_FLAG_AD_SHORT:
129 iinfo->cached_extent.epos.offset -=
130 sizeof(struct short_ad);
131 break;
132 case ICBTAG_FLAG_AD_LONG:
133 iinfo->cached_extent.epos.offset -=
134 sizeof(struct long_ad);
135 }
136 spin_unlock(&iinfo->i_extent_cache_lock);
137}
Christoph Hellwigb1e32122008-02-22 12:38:48 +0100138
Al Viro3aac2b62010-06-07 00:43:39 -0400139void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Jan Kara2c948b32009-12-03 13:39:28 +0100141 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400142 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +0100143
Al Viro3aac2b62010-06-07 00:43:39 -0400144 if (!inode->i_nlink && !is_bad_inode(inode)) {
145 want_delete = 1;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200146 udf_setsize(inode, 0);
Al Viro3aac2b62010-06-07 00:43:39 -0400147 udf_update_inode(inode, IS_SYNC(inode));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200148 } else
149 truncate_inode_pages(&inode->i_data, 0);
Al Viro3aac2b62010-06-07 00:43:39 -0400150 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200151 clear_inode(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100152 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
153 inode->i_size != iinfo->i_lenExtents) {
Joe Perches78ace702011-10-10 01:08:05 -0700154 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",
155 inode->i_ino, inode->i_mode,
156 (unsigned long long)inode->i_size,
157 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800159 kfree(iinfo->i_ext.i_data);
160 iinfo->i_ext.i_data = NULL;
Namjae Jeon99600052013-01-19 11:17:14 +0900161 udf_clear_extent_cache(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400162 if (want_delete) {
Al Viro3aac2b62010-06-07 00:43:39 -0400163 udf_free_inode(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Ian Abbott5eec54f2012-09-05 17:44:31 +0100167static void udf_write_failed(struct address_space *mapping, loff_t to)
168{
169 struct inode *inode = mapping->host;
170 struct udf_inode_info *iinfo = UDF_I(inode);
171 loff_t isize = inode->i_size;
172
173 if (to > isize) {
174 truncate_pagecache(inode, to, isize);
175 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
176 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900177 udf_clear_extent_cache(inode);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100178 udf_truncate_extents(inode);
179 up_write(&iinfo->i_data_sem);
180 }
181 }
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static int udf_writepage(struct page *page, struct writeback_control *wbc)
185{
186 return block_write_full_page(page, udf_get_block, wbc);
187}
188
Namjae Jeon378b8e12012-08-31 12:49:07 -0400189static int udf_writepages(struct address_space *mapping,
190 struct writeback_control *wbc)
191{
192 return mpage_writepages(mapping, wbc, udf_get_block);
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int udf_readpage(struct file *file, struct page *page)
196{
Namjae Jeonbc112322011-10-03 14:02:59 +0900197 return mpage_readpage(page, udf_get_block);
198}
199
200static int udf_readpages(struct file *file, struct address_space *mapping,
201 struct list_head *pages, unsigned nr_pages)
202{
203 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Nick Pigginbe021ee2007-10-16 01:25:20 -0700206static int udf_write_begin(struct file *file, struct address_space *mapping,
207 loff_t pos, unsigned len, unsigned flags,
208 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200210 int ret;
211
212 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100213 if (unlikely(ret))
214 udf_write_failed(mapping, pos + len);
215 return ret;
216}
Jan Kara7e49b6f2010-10-22 00:30:26 +0200217
Ian Abbott5eec54f2012-09-05 17:44:31 +0100218static ssize_t udf_direct_IO(int rw, struct kiocb *iocb,
219 const struct iovec *iov,
220 loff_t offset, unsigned long nr_segs)
221{
222 struct file *file = iocb->ki_filp;
223 struct address_space *mapping = file->f_mapping;
224 struct inode *inode = mapping->host;
225 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200226
Ian Abbott5eec54f2012-09-05 17:44:31 +0100227 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
228 udf_get_block);
229 if (unlikely(ret < 0 && (rw & WRITE)))
230 udf_write_failed(mapping, offset + iov_length(iov, nr_segs));
Christoph Hellwig155130a2010-06-04 11:29:58 +0200231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static sector_t udf_bmap(struct address_space *mapping, sector_t block)
235{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700236 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700239const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700240 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900241 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700242 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400243 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100244 .write_begin = udf_write_begin,
245 .write_end = generic_write_end,
246 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700247 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
Jan Karad2eb8c32011-12-10 02:30:48 +0100250/*
251 * Expand file stored in ICB to a normal one-block-file
252 *
253 * This function requires i_data_sem for writing and releases it.
254 * This function requires i_mutex held
255 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200256int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct page *page;
259 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800260 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200261 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct writeback_control udf_wbc = {
263 .sync_mode = WB_SYNC_NONE,
264 .nr_to_write = 1,
265 };
266
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800267 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800269 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800271 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200272 /* from now on we have normal address_space methods */
273 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100274 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100278 /*
279 * Release i_data_sem so that we can lock a page - page lock ranks
280 * above i_data_sem. i_mutex still protects us against file changes.
281 */
282 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jan Kara7e49b6f2010-10-22 00:30:26 +0200284 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
285 if (!page)
286 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700287
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800290 memset(kaddr + iinfo->i_lenAlloc, 0x00,
291 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
292 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
293 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 flush_dcache_page(page);
295 SetPageUptodate(page);
296 kunmap(page);
297 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100298 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800299 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
300 iinfo->i_lenAlloc);
301 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800303 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800305 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200306 /* from now on we have normal address_space methods */
307 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100308 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200309 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
310 if (err) {
311 /* Restore everything back so that we don't lose data... */
312 lock_page(page);
313 kaddr = kmap(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100314 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200315 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
316 inode->i_size);
317 kunmap(page);
318 unlock_page(page);
319 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
320 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100321 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200325
326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700329struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
330 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700333 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200334 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700336 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800339 loff_t f_pos = udf_ext0_offset(inode);
340 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800342 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
345 alloctype = ICBTAG_FLAG_AD_SHORT;
346 else
347 alloctype = ICBTAG_FLAG_AD_LONG;
348
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700349 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800350 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 mark_inode_dirty(inode);
352 return NULL;
353 }
354
355 /* alloc block, and copy data to it */
356 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800357 iinfo->i_location.partitionReferenceNum,
358 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!(*block))
360 return NULL;
361 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800362 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800363 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!newblock)
365 return NULL;
366 dbh = udf_tgetblk(inode->i_sb, newblock);
367 if (!dbh)
368 return NULL;
369 lock_buffer(dbh);
370 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
371 set_buffer_uptodate(dbh);
372 unlock_buffer(dbh);
373 mark_buffer_dirty_inode(dbh, inode);
374
Marcin Slusarz4b111112008-02-08 04:20:36 -0800375 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800376 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700377 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 dfibh.soffset = dfibh.eoffset = 0;
379 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800380 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800381 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800382 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
383 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700384 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700385 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return NULL;
387 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800388 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 sfi->descTag.tagLocation = cpu_to_le32(*block);
390 dfibh.soffset = dfibh.eoffset;
391 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
392 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
393 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800394 sfi->fileIdent +
395 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800396 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700397 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return NULL;
399 }
400 }
401 mark_buffer_dirty_inode(dbh, inode);
402
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800403 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
404 iinfo->i_lenAlloc);
405 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800407 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800408 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100409 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700410 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800411 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700412 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100413 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* UniqueID stuff */
415
Jan Kara3bf25cb2007-05-08 00:35:16 -0700416 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 mark_inode_dirty(inode);
418 return dbh;
419}
420
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700421static int udf_get_block(struct inode *inode, sector_t block,
422 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800425 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800426 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700428 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 phys = udf_block_map(inode, block);
430 if (phys)
431 map_bh(bh_result, inode->i_sb, phys);
432 return 0;
433 }
434
435 err = -EIO;
436 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800437 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100438
439 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800440 if (block == iinfo->i_next_alloc_block + 1) {
441 iinfo->i_next_alloc_block++;
442 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Namjae Jeon99600052013-01-19 11:17:14 +0900445 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100446 phys = inode_getblk(inode, block, &err, &new);
447 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (new)
451 set_buffer_new(bh_result);
452 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700453
454abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100455 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700459static struct buffer_head *udf_getblk(struct inode *inode, long block,
460 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700462 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct buffer_head dummy;
464
465 dummy.b_state = 0;
466 dummy.b_blocknr = -1000;
467 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700468 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700470 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 lock_buffer(bh);
472 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
473 set_buffer_uptodate(bh);
474 unlock_buffer(bh);
475 mark_buffer_dirty_inode(bh, inode);
476 }
477 return bh;
478 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return NULL;
481}
482
Jan Kara31170b62007-05-08 00:35:21 -0700483/* Extend the file by 'blocks' blocks, return the number of extents added */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200484static int udf_do_extend_file(struct inode *inode,
485 struct extent_position *last_pos,
486 struct kernel_long_ad *last_ext,
487 sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700488{
489 sector_t add;
490 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
491 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200492 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700493 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800494 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200495 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700496
497 /* The previous extent is fake and we should not extend by anything
498 * - there's nothing to do... */
499 if (!blocks && fake)
500 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700501
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800502 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700503 /* Round the last extent up to a multiple of block size */
504 if (last_ext->extLength & (sb->s_blocksize - 1)) {
505 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700506 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
507 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
508 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800509 iinfo->i_lenExtents =
510 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700511 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700512 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700513
Jan Kara31170b62007-05-08 00:35:21 -0700514 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800515 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
516 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700517 /* Save the extent so that we can reattach it to the end */
518 prealloc_loc = last_ext->extLocation;
519 prealloc_len = last_ext->extLength;
520 /* Mark the extent as a hole */
521 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700522 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700523 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800524 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700525 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700526
Jan Kara31170b62007-05-08 00:35:21 -0700527 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800528 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
529 EXT_NOT_RECORDED_NOT_ALLOCATED) {
530 add = ((1 << 30) - sb->s_blocksize -
531 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
532 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700533 if (add > blocks)
534 add = blocks;
535 blocks -= add;
536 last_ext->extLength += add << sb->s_blocksize_bits;
537 }
538
539 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200540 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700541 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700542 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800543 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200544 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800545 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700546
Jan Kara31170b62007-05-08 00:35:21 -0700547 /* Managed to do everything necessary? */
548 if (!blocks)
549 goto out;
550
551 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
552 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800553 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700554 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800555 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
556 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700557
Jan Kara31170b62007-05-08 00:35:21 -0700558 /* Create enough extents to cover the whole hole */
559 while (blocks > add) {
560 blocks -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200561 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
562 last_ext->extLength, 1);
563 if (err)
564 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700565 count++;
566 }
567 if (blocks) {
568 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700569 (blocks << sb->s_blocksize_bits);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200570 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
571 last_ext->extLength, 1);
572 if (err)
573 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700574 count++;
575 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700576
577out:
Jan Kara31170b62007-05-08 00:35:21 -0700578 /* Do we have some preallocated blocks saved? */
579 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200580 err = udf_add_aext(inode, last_pos, &prealloc_loc,
581 prealloc_len, 1);
582 if (err)
583 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700584 last_ext->extLocation = prealloc_loc;
585 last_ext->extLength = prealloc_len;
586 count++;
587 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700588
Jan Kara31170b62007-05-08 00:35:21 -0700589 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800590 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200591 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800592 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200593 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700594 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200595 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700596
Jan Kara31170b62007-05-08 00:35:21 -0700597 return count;
598}
599
Jan Kara7e49b6f2010-10-22 00:30:26 +0200600static int udf_extend_file(struct inode *inode, loff_t newsize)
601{
602
603 struct extent_position epos;
604 struct kernel_lb_addr eloc;
605 uint32_t elen;
606 int8_t etype;
607 struct super_block *sb = inode->i_sb;
608 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
609 int adsize;
610 struct udf_inode_info *iinfo = UDF_I(inode);
611 struct kernel_long_ad extent;
612 int err;
613
614 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
615 adsize = sizeof(struct short_ad);
616 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
617 adsize = sizeof(struct long_ad);
618 else
619 BUG();
620
621 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
622
623 /* File has extent covering the new size (could happen when extending
624 * inside a block)? */
625 if (etype != -1)
626 return 0;
627 if (newsize & (sb->s_blocksize - 1))
628 offset++;
629 /* Extended file just to the boundary of the last file block? */
630 if (offset == 0)
631 return 0;
632
633 /* Truncate is extending the file by 'offset' blocks */
634 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
635 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
636 /* File has no extents at all or has empty last
637 * indirect extent! Create a fake extent... */
638 extent.extLocation.logicalBlockNum = 0;
639 extent.extLocation.partitionReferenceNum = 0;
640 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
641 } else {
642 epos.offset -= adsize;
643 etype = udf_next_aext(inode, &epos, &extent.extLocation,
644 &extent.extLength, 0);
645 extent.extLength |= etype << 30;
646 }
647 err = udf_do_extend_file(inode, &epos, &extent, offset);
648 if (err < 0)
649 goto out;
650 err = 0;
651 iinfo->i_lenExtents = newsize;
652out:
653 brelse(epos.bh);
654 return err;
655}
656
Jan Kara7b0b0932011-12-10 01:43:33 +0100657static sector_t inode_getblk(struct inode *inode, sector_t block,
658 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200660 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700661 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700663 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200664 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700666 loff_t lbcount = 0, b_off = 0;
667 uint32_t newblocknum, newblock;
668 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800670 struct udf_inode_info *iinfo = UDF_I(inode);
671 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700672 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900673 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jan Kara7b0b0932011-12-10 01:43:33 +0100675 *err = 0;
676 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700677 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800678 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700679 prev_epos.bh = NULL;
680 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700681 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700684 alternate between laarr[0] and laarr[1] for locations of the
685 current extent, and the previous extent */
686 do {
687 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700688 brelse(prev_epos.bh);
689 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700690 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700692 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700693 brelse(cur_epos.bh);
694 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700695 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
698 lbcount += elen;
699
Jan Karaff116fc2007-05-08 00:35:14 -0700700 prev_epos.block = cur_epos.block;
701 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Jan Karaff116fc2007-05-08 00:35:14 -0700703 prev_epos.offset = cur_epos.offset;
704 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Marcin Slusarz4b111112008-02-08 04:20:36 -0800706 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
707 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 break;
709
710 c = !c;
711
712 laarr[c].extLength = (etype << 30) | elen;
713 laarr[c].extLocation = eloc;
714
715 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
716 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700717 ((elen + inode->i_sb->s_blocksize - 1) >>
718 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700720 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 } while (lbcount + elen <= b_off);
722
723 b_off -= lbcount;
724 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700725 /*
726 * Move prev_epos and cur_epos into indirect extent if we are at
727 * the pointer to it
728 */
729 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
730 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700733 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700735 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
736 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700738 ((elen + inode->i_sb->s_blocksize - 1) &
739 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200740 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700742 brelse(prev_epos.bh);
743 brelse(cur_epos.bh);
744 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200745 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Jan Kara7b0b0932011-12-10 01:43:33 +0100746 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Jan Kara31170b62007-05-08 00:35:21 -0700749 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700750 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700751 int ret;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900752 isBeyondEOF = 1;
Jan Kara31170b62007-05-08 00:35:21 -0700753 if (count) {
754 if (c)
755 laarr[0] = laarr[1];
756 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700757 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700758 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800759 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200760 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700761 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200762 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800763 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700764 startnum = (offset > 0);
765 }
766 /* Create extents for the hole between EOF and offset */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200767 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
768 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700769 brelse(prev_epos.bh);
770 brelse(cur_epos.bh);
771 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200772 *err = ret;
Jan Kara7b0b0932011-12-10 01:43:33 +0100773 return 0;
Jan Kara31170b62007-05-08 00:35:21 -0700774 }
775 c = 0;
776 offset = 0;
777 count += ret;
778 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800779 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
780 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700781 /* Is there any real extent? - otherwise we overwrite
782 * the fake one... */
783 if (count)
784 c = !c;
785 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700786 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800787 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200788 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700789 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700790 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700793 } else {
Namjae Jeonfb719c52012-10-10 00:09:12 +0900794 isBeyondEOF = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 endnum = startnum = ((count > 2) ? 2 : count);
796
Marcin Slusarz4b111112008-02-08 04:20:36 -0800797 /* if the current extent is in position 0,
798 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700799 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700800 laarr[2] = laarr[0];
801 laarr[0] = laarr[1];
802 laarr[1] = laarr[2];
803 c = 1;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Marcin Slusarz4b111112008-02-08 04:20:36 -0800806 /* if the current block is located in an extent,
807 read the next extent */
808 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
809 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700810 laarr[c + 1].extLength = (etype << 30) | elen;
811 laarr[c + 1].extLocation = eloc;
812 count++;
813 startnum++;
814 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800815 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 lastblock = 1;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700820 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800821 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800823 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800824 if (iinfo->i_next_alloc_block == block)
825 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700827 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800828 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800829 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
Marcin Slusarz4b111112008-02-08 04:20:36 -0800832 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800833 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800834 goal, err);
835 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700836 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900837 brelse(cur_epos.bh);
838 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 *err = -ENOSPC;
Jan Kara7b0b0932011-12-10 01:43:33 +0100840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900842 if (isBeyondEOF)
843 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
Marcin Slusarz4b111112008-02-08 04:20:36 -0800846 /* if the extent the requsted block is located in contains multiple
847 * blocks, split the extent into at most three extents. blocks prior
848 * to requested block, requested block, and blocks after requested
849 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
851
852#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200853 /* We preallocate blocks only for regular files. It also makes sense
854 * for directories but there's a problem when to drop the
855 * preallocation. We might use some delayed work for that but I feel
856 * it's overengineering for a filesystem like UDF. */
857 if (S_ISREG(inode->i_mode))
858 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859#endif
860
861 /* merge any continuous blocks in laarr */
862 udf_merge_extents(inode, laarr, &endnum);
863
864 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700865 * of extents is greater than the old number, and deleting extents if
866 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700867 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Jan Kara3bf25cb2007-05-08 00:35:16 -0700869 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900870 brelse(cur_epos.bh);
871 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Marcin Slusarz4b111112008-02-08 04:20:36 -0800873 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800874 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100875 if (!newblock) {
876 *err = -EIO;
877 return 0;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800880 iinfo->i_next_alloc_block = block;
881 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 inode->i_ctime = current_fs_time(inode->i_sb);
883
884 if (IS_SYNC(inode))
885 udf_sync_inode(inode);
886 else
887 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700888
Jan Kara7b0b0932011-12-10 01:43:33 +0100889 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700892static void udf_split_extents(struct inode *inode, int *c, int offset,
893 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200894 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700895 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800897 unsigned long blocksize = inode->i_sb->s_blocksize;
898 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800901 (laarr[*c].extLength >> 30) ==
902 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 int curr = *c;
904 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800905 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int8_t etype = (laarr[curr].extLength >> 30);
907
Marcin Slusarz4b111112008-02-08 04:20:36 -0800908 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700909 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800910 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700911 laarr[curr + 2] = laarr[curr + 1];
912 laarr[curr + 1] = laarr[curr];
913 } else {
914 laarr[curr + 3] = laarr[curr + 1];
915 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700918 if (offset) {
919 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800920 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200921 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800922 0, offset);
923 laarr[curr].extLength =
924 EXT_NOT_RECORDED_NOT_ALLOCATED |
925 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800927 laarr[curr].extLocation.
928 partitionReferenceNum = 0;
929 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800931 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700932 curr++;
933 (*c)++;
934 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 laarr[curr].extLocation.logicalBlockNum = newblocknum;
938 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
939 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800940 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800942 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700943 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700945 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800947 laarr[curr].extLocation.logicalBlockNum +=
948 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700949 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800950 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700951 curr++;
952 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 }
955}
956
957static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200958 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700959 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 int start, length = 0, currlength = 0, i;
962
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700963 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!lastblock)
965 return;
966 else
967 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700968 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800969 if ((laarr[c + 1].extLength >> 30) ==
970 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700971 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800972 length = currlength =
973 (((laarr[c + 1].extLength &
974 UDF_EXTENT_LENGTH_MASK) +
975 inode->i_sb->s_blocksize - 1) >>
976 inode->i_sb->s_blocksize_bits);
977 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 start = c;
979 }
980
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700981 for (i = start + 1; i <= *endnum; i++) {
982 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (lastblock)
984 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800985 } else if ((laarr[i].extLength >> 30) ==
986 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
987 length += (((laarr[i].extLength &
988 UDF_EXTENT_LENGTH_MASK) +
989 inode->i_sb->s_blocksize - 1) >>
990 inode->i_sb->s_blocksize_bits);
991 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
993 }
994
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700995 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700997 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800998 inode->i_sb->s_blocksize - 1) >>
999 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001001 laarr[start].extLocation.partitionReferenceNum,
1002 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1003 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1004 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001005 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001006 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001008 (numalloc <<
1009 inode->i_sb->s_blocksize_bits);
1010 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001011 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001012 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001013 (*endnum)++;
1014 laarr[c + 1].extLocation.logicalBlockNum = next;
1015 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001016 laarr[c].extLocation.
1017 partitionReferenceNum;
1018 laarr[c + 1].extLength =
1019 EXT_NOT_RECORDED_ALLOCATED |
1020 (numalloc <<
1021 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001022 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001025 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001026 int elen = ((laarr[i].extLength &
1027 UDF_EXTENT_LENGTH_MASK) +
1028 inode->i_sb->s_blocksize - 1) >>
1029 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001031 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001033 (numalloc <<
1034 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001036 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001038 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001039 memmove(&laarr[i],
1040 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001041 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001042 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001043 i--;
1044 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001047 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001048 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050 }
1051}
1052
1053static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001054 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001055 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001058 unsigned long blocksize = inode->i_sb->s_blocksize;
1059 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001061 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001062 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1063 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Marcin Slusarz4b111112008-02-08 04:20:36 -08001065 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1066 (((li->extLength >> 30) ==
1067 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1068 ((lip1->extLocation.logicalBlockNum -
1069 li->extLocation.logicalBlockNum) ==
1070 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1071 blocksize - 1) >> blocksize_bits)))) {
1072
1073 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1074 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1075 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1076 lip1->extLength = (lip1->extLength -
1077 (li->extLength &
1078 UDF_EXTENT_LENGTH_MASK) +
1079 UDF_EXTENT_LENGTH_MASK) &
1080 ~(blocksize - 1);
1081 li->extLength = (li->extLength &
1082 UDF_EXTENT_FLAG_MASK) +
1083 (UDF_EXTENT_LENGTH_MASK + 1) -
1084 blocksize;
1085 lip1->extLocation.logicalBlockNum =
1086 li->extLocation.logicalBlockNum +
1087 ((li->extLength &
1088 UDF_EXTENT_LENGTH_MASK) >>
1089 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001090 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001091 li->extLength = lip1->extLength +
1092 (((li->extLength &
1093 UDF_EXTENT_LENGTH_MASK) +
1094 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001095 if (*endnum > (i + 2))
1096 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001097 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001098 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001099 i--;
1100 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001102 } else if (((li->extLength >> 30) ==
1103 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1104 ((lip1->extLength >> 30) ==
1105 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001106 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001107 ((li->extLength &
1108 UDF_EXTENT_LENGTH_MASK) +
1109 blocksize - 1) >> blocksize_bits);
1110 li->extLocation.logicalBlockNum = 0;
1111 li->extLocation.partitionReferenceNum = 0;
1112
1113 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1114 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1115 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1116 lip1->extLength = (lip1->extLength -
1117 (li->extLength &
1118 UDF_EXTENT_LENGTH_MASK) +
1119 UDF_EXTENT_LENGTH_MASK) &
1120 ~(blocksize - 1);
1121 li->extLength = (li->extLength &
1122 UDF_EXTENT_FLAG_MASK) +
1123 (UDF_EXTENT_LENGTH_MASK + 1) -
1124 blocksize;
1125 } else {
1126 li->extLength = lip1->extLength +
1127 (((li->extLength &
1128 UDF_EXTENT_LENGTH_MASK) +
1129 blocksize - 1) & ~(blocksize - 1));
1130 if (*endnum > (i + 2))
1131 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001132 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001133 (*endnum - (i + 2)));
1134 i--;
1135 (*endnum)--;
1136 }
1137 } else if ((li->extLength >> 30) ==
1138 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1139 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001140 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001141 ((li->extLength &
1142 UDF_EXTENT_LENGTH_MASK) +
1143 blocksize - 1) >> blocksize_bits);
1144 li->extLocation.logicalBlockNum = 0;
1145 li->extLocation.partitionReferenceNum = 0;
1146 li->extLength = (li->extLength &
1147 UDF_EXTENT_LENGTH_MASK) |
1148 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150 }
1151}
1152
1153static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001154 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001155 int startnum, int endnum,
1156 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001159 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 uint32_t tmplen;
1161
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001162 if (startnum > endnum) {
1163 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001164 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001165 laarr[i].extLength);
1166 } else if (startnum < endnum) {
1167 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001168 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001169 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001170 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001171 &laarr[i].extLength, 1);
1172 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 }
1175
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001176 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001177 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001178 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001179 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181}
1182
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001183struct buffer_head *udf_bread(struct inode *inode, int block,
1184 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001186 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 bh = udf_getblk(inode, block, create, err);
1189 if (!bh)
1190 return NULL;
1191
1192 if (buffer_uptodate(bh))
1193 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 wait_on_buffer(bh);
1198 if (buffer_uptodate(bh))
1199 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 brelse(bh);
1202 *err = -EIO;
1203 return NULL;
1204}
1205
Jan Kara7e49b6f2010-10-22 00:30:26 +02001206int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001209 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +02001210 int bsize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001213 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001214 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001216 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001218 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001219 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001220 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001221 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1222 if (bsize <
1223 (udf_file_entry_alloc_offset(inode) + newsize)) {
1224 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001225 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001226 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001227 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001228 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001229 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001230 goto set_size;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001233 err = udf_extend_file(inode, newsize);
1234 if (err) {
1235 up_write(&iinfo->i_data_sem);
1236 return err;
1237 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001238set_size:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001239 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001240 up_write(&iinfo->i_data_sem);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001241 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001242 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1243 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001244 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001245 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1246 0x00, bsize - newsize -
1247 udf_file_entry_alloc_offset(inode));
1248 iinfo->i_lenAlloc = newsize;
1249 truncate_setsize(inode, newsize);
1250 up_write(&iinfo->i_data_sem);
1251 goto update_time;
1252 }
1253 err = block_truncate_page(inode->i_mapping, newsize,
1254 udf_get_block);
1255 if (err)
1256 return err;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001257 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001258 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001259 truncate_setsize(inode, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001261 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001262 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001263update_time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1265 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001266 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 else
1268 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001272static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 struct buffer_head *bh = NULL;
1275 struct fileEntry *fe;
1276 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001277 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /*
1280 * Set defaults, but the inode is still incomplete!
1281 * Note: get_new_inode() sets the following on a new inode:
1282 * i_sb = sb
1283 * i_no = ino
1284 * i_flags = sb->s_flags
1285 * i_state = 0
1286 * clean_inode(): zero fills and sets
1287 * i_count = 1
1288 * i_nlink = 1
1289 * i_op = NULL;
1290 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001291 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001292 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001293 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 make_bad_inode(inode);
1295 return;
1296 }
1297
1298 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001299 ident != TAG_IDENT_USE) {
Joe Perches78ace702011-10-10 01:08:05 -07001300 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1301 inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001302 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 make_bad_inode(inode);
1304 return;
1305 }
1306
1307 fe = (struct fileEntry *)bh->b_data;
1308
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001309 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001310 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Pekka Enberg97e961f2008-10-15 12:29:03 +02001312 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001313 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001314 if (ident == TAG_IDENT_IE && ibh) {
1315 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001316 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001317 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001318
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001319 ie = (struct indirectEntry *)ibh->b_data;
1320 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001321
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001322 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001323 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001324 &ident))) {
1325 if (ident == TAG_IDENT_FE ||
1326 ident == TAG_IDENT_EFE) {
1327 memcpy(&iinfo->i_location,
1328 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001329 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001330 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001331 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001332 brelse(nbh);
1333 __udf_read_inode(inode);
1334 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001335 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001336 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001338 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001339 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001340 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Joe Perches78ace702011-10-10 01:08:05 -07001341 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1342 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001343 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 make_bad_inode(inode);
1345 return;
1346 }
1347 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001348
Jan Kara3bf25cb2007-05-08 00:35:16 -07001349 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
1352static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1353{
1354 struct fileEntry *fe;
1355 struct extendedFileEntry *efe;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001356 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001357 struct udf_inode_info *iinfo = UDF_I(inode);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001358 unsigned int link_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 fe = (struct fileEntry *)bh->b_data;
1361 efe = (struct extendedFileEntry *)bh->b_data;
1362
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001363 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001364 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001365 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001366 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001368 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001369 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001370 iinfo->i_unique = 0;
1371 iinfo->i_lenEAttr = 0;
1372 iinfo->i_lenExtents = 0;
1373 iinfo->i_lenAlloc = 0;
1374 iinfo->i_next_alloc_block = 0;
1375 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001376 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001377 iinfo->i_efe = 1;
1378 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001379 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1380 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001381 make_bad_inode(inode);
1382 return;
1383 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001384 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001385 bh->b_data + sizeof(struct extendedFileEntry),
1386 inode->i_sb->s_blocksize -
1387 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001388 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001389 iinfo->i_efe = 0;
1390 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001391 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1392 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001393 make_bad_inode(inode);
1394 return;
1395 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001396 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001397 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001398 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001399 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001400 iinfo->i_efe = 0;
1401 iinfo->i_use = 1;
1402 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001403 ((struct unallocSpaceEntry *)bh->b_data)->
1404 lengthAllocDescs);
1405 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1406 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001407 make_bad_inode(inode);
1408 return;
1409 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001410 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001411 bh->b_data + sizeof(struct unallocSpaceEntry),
1412 inode->i_sb->s_blocksize -
1413 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return;
1415 }
1416
Jan Karac03cad22010-10-20 22:17:28 +02001417 read_lock(&sbi->s_cred_lock);
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001418 i_uid_write(inode, le32_to_cpu(fe->uid));
1419 if (!uid_valid(inode->i_uid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001420 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1421 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001422 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001424 i_gid_write(inode, le32_to_cpu(fe->gid));
1425 if (!gid_valid(inode->i_gid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001426 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1427 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001428 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001430 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001431 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001432 inode->i_mode = sbi->s_fmode;
1433 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001434 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001435 inode->i_mode = sbi->s_dmode;
1436 else
1437 inode->i_mode = udf_convert_permissions(fe);
1438 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001439 read_unlock(&sbi->s_cred_lock);
1440
Miklos Szeredibfe86842011-10-28 14:13:29 +02001441 link_count = le16_to_cpu(fe->fileLinkCount);
1442 if (!link_count)
1443 link_count = 1;
1444 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001445
1446 inode->i_size = le64_to_cpu(fe->informationLength);
1447 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001449 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001451 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Marcin Slusarz56774802008-02-10 11:25:31 +01001453 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001454 inode->i_atime = sbi->s_record_time;
1455
Marcin Slusarz56774802008-02-10 11:25:31 +01001456 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1457 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001458 inode->i_mtime = sbi->s_record_time;
1459
Marcin Slusarz56774802008-02-10 11:25:31 +01001460 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001461 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001463 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1464 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1465 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001466 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001467 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001468 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001469 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Marcin Slusarz56774802008-02-10 11:25:31 +01001471 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001472 inode->i_atime = sbi->s_record_time;
1473
Marcin Slusarz56774802008-02-10 11:25:31 +01001474 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1475 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001476 inode->i_mtime = sbi->s_record_time;
1477
Marcin Slusarz56774802008-02-10 11:25:31 +01001478 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001479 iinfo->i_crtime = sbi->s_record_time;
1480
Marcin Slusarz56774802008-02-10 11:25:31 +01001481 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001482 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001484 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1485 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1486 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001487 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001490 switch (fe->icbTag.fileType) {
1491 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001492 inode->i_op = &udf_dir_inode_operations;
1493 inode->i_fop = &udf_dir_operations;
1494 inode->i_mode |= S_IFDIR;
1495 inc_nlink(inode);
1496 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001497 case ICBTAG_FILE_TYPE_REALTIME:
1498 case ICBTAG_FILE_TYPE_REGULAR:
1499 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001500 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001501 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001502 inode->i_data.a_ops = &udf_adinicb_aops;
1503 else
1504 inode->i_data.a_ops = &udf_aops;
1505 inode->i_op = &udf_file_inode_operations;
1506 inode->i_fop = &udf_file_operations;
1507 inode->i_mode |= S_IFREG;
1508 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001509 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001510 inode->i_mode |= S_IFBLK;
1511 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001512 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001513 inode->i_mode |= S_IFCHR;
1514 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001515 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001516 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1517 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001518 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001519 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1520 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001521 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001522 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001523 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001524 inode->i_mode = S_IFLNK | S_IRWXUGO;
1525 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001526 case ICBTAG_FILE_TYPE_MAIN:
1527 udf_debug("METADATA FILE-----\n");
1528 break;
1529 case ICBTAG_FILE_TYPE_MIRROR:
1530 udf_debug("METADATA MIRROR FILE-----\n");
1531 break;
1532 case ICBTAG_FILE_TYPE_BITMAP:
1533 udf_debug("METADATA BITMAP FILE-----\n");
1534 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001535 default:
Joe Perches78ace702011-10-10 01:08:05 -07001536 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1537 inode->i_ino, fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001538 make_bad_inode(inode);
1539 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001541 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001542 struct deviceSpec *dsea =
1543 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001544 if (dsea) {
1545 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001546 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1547 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001549 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552}
1553
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001554static int udf_alloc_i_data(struct inode *inode, size_t size)
1555{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001556 struct udf_inode_info *iinfo = UDF_I(inode);
1557 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001558
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001559 if (!iinfo->i_ext.i_data) {
Joe Perches78ace702011-10-10 01:08:05 -07001560 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1561 inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001562 return -ENOMEM;
1563 }
1564
1565 return 0;
1566}
1567
Al Virofaa17292011-07-26 03:18:29 -04001568static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Al Virofaa17292011-07-26 03:18:29 -04001570 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 uint32_t permissions;
1572 uint32_t flags;
1573
1574 permissions = le32_to_cpu(fe->permissions);
1575 flags = le16_to_cpu(fe->icbTag.flags);
1576
Marcin Slusarz4b111112008-02-08 04:20:36 -08001577 mode = ((permissions) & S_IRWXO) |
1578 ((permissions >> 2) & S_IRWXG) |
1579 ((permissions >> 4) & S_IRWXU) |
1580 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1581 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1582 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 return mode;
1585}
1586
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001587int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Jan Kara49521de2010-10-20 17:42:44 +02001589 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Jan Kara49521de2010-10-20 17:42:44 +02001592static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 return udf_update_inode(inode, 1);
1595}
1596
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001597static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 struct buffer_head *bh = NULL;
1600 struct fileEntry *fe;
1601 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001602 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 uint32_t udfperms;
1604 uint16_t icbflags;
1605 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001607 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001608 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001609 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Jan Kara5833ded2010-01-08 16:52:59 +01001611 bh = udf_tgetblk(inode->i_sb,
1612 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001613 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001614 udf_debug("getblk failure\n");
1615 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
Jan Karaaae917c2010-01-08 16:46:29 +01001618 lock_buffer(bh);
1619 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 fe = (struct fileEntry *)bh->b_data;
1621 efe = (struct extendedFileEntry *)bh->b_data;
1622
Jan Karaaae917c2010-01-08 16:46:29 +01001623 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001625 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001627 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001628 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001629 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001630 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001631 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1632 use->descTag.tagLocation =
1633 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001634 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001635 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001637 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001638 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001639 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001640 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jan Karaaae917c2010-01-08 16:46:29 +01001642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644
Phillip Susi4d6660e2006-03-07 21:55:24 -08001645 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1646 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001647 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001648 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Phillip Susi4d6660e2006-03-07 21:55:24 -08001650 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1651 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001652 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001653 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Marcin Slusarz4b111112008-02-08 04:20:36 -08001655 udfperms = ((inode->i_mode & S_IRWXO)) |
1656 ((inode->i_mode & S_IRWXG) << 2) |
1657 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Marcin Slusarz4b111112008-02-08 04:20:36 -08001659 udfperms |= (le32_to_cpu(fe->permissions) &
1660 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1661 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1662 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 fe->permissions = cpu_to_le32(udfperms);
1664
1665 if (S_ISDIR(inode->i_mode))
1666 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1667 else
1668 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1669
1670 fe->informationLength = cpu_to_le64(inode->i_size);
1671
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001672 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001673 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001674 struct deviceSpec *dsea =
1675 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001676 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001678 udf_add_extendedattr(inode,
1679 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001680 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 dsea->attrType = cpu_to_le32(12);
1682 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001683 dsea->attrLength = cpu_to_le32(
1684 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001685 sizeof(struct regid));
1686 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001688 eid = (struct regid *)dsea->impUse;
1689 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 strcpy(eid->ident, UDF_ID_DEVELOPER);
1691 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1692 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1693 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1694 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1695 }
1696
Steve Nickelb2527bf2012-02-16 12:53:53 -05001697 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1698 lb_recorded = 0; /* No extents => no blocks! */
1699 else
1700 lb_recorded =
1701 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1702 (blocksize_bits - 9);
1703
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001704 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001705 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001706 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001707 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001708 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Marcin Slusarz56774802008-02-10 11:25:31 +01001710 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1711 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1712 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001713 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1715 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1716 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001717 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1718 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1719 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001720 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1722 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001723 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001724 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001725 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001726 inode->i_sb->s_blocksize -
1727 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001729 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001731 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1732 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1733 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1734 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001735
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001736 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1737 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1738 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1739 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001740
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001741 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1742 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1743 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1744 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Marcin Slusarz56774802008-02-10 11:25:31 +01001746 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1747 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1748 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1749 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001751 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1753 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1754 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001755 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1756 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1757 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001758 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1760 crclen = sizeof(struct extendedFileEntry);
1761 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001762 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 fe->icbTag.strategyType = cpu_to_le16(4096);
1764 fe->icbTag.strategyParameter = cpu_to_le16(1);
1765 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001766 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 fe->icbTag.strategyType = cpu_to_le16(4);
1768 fe->icbTag.numEntries = cpu_to_le16(1);
1769 }
1770
1771 if (S_ISDIR(inode->i_mode))
1772 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1773 else if (S_ISREG(inode->i_mode))
1774 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1775 else if (S_ISLNK(inode->i_mode))
1776 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1777 else if (S_ISBLK(inode->i_mode))
1778 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1779 else if (S_ISCHR(inode->i_mode))
1780 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1781 else if (S_ISFIFO(inode->i_mode))
1782 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1783 else if (S_ISSOCK(inode->i_mode))
1784 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1785
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001786 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001787 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1788 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1789 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1790 (le16_to_cpu(fe->icbTag.flags) &
1791 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1792 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001795 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 fe->descTag.descVersion = cpu_to_le16(3);
1797 else
1798 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001799 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001800 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001801 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001802 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001804 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001805 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001806 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Jan Karaaae917c2010-01-08 16:46:29 +01001808out:
Jan Kara5833ded2010-01-08 16:52:59 +01001809 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001810 unlock_buffer(bh);
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* write the data blocks */
1813 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001814 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001816 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001817 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1818 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 err = -EIO;
1820 }
1821 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001822 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return err;
1825}
1826
Pekka Enberg97e961f2008-10-15 12:29:03 +02001827struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1830 struct inode *inode = iget_locked(sb, block);
1831
1832 if (!inode)
1833 return NULL;
1834
1835 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001836 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 __udf_read_inode(inode);
1838 unlock_new_inode(inode);
1839 }
1840
1841 if (is_bad_inode(inode))
1842 goto out_iput;
1843
Pekka Enberg97e961f2008-10-15 12:29:03 +02001844 if (ino->logicalBlockNum >= UDF_SB(sb)->
1845 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001847 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 make_bad_inode(inode);
1849 goto out_iput;
1850 }
1851
1852 return inode;
1853
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001854 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 iput(inode);
1856 return NULL;
1857}
1858
Jan Kara7e49b6f2010-10-22 00:30:26 +02001859int udf_add_aext(struct inode *inode, struct extent_position *epos,
1860 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001863 struct short_ad *sad = NULL;
1864 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 struct allocExtDesc *aed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001867 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jan Karaff116fc2007-05-08 00:35:14 -07001869 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001870 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001871 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001872 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 else
Jan Karaff116fc2007-05-08 00:35:14 -07001874 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001876 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001877 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001878 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001879 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001881 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001883 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001884 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 struct buffer_head *nbh;
1886 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001887 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Marcin Slusarz4b111112008-02-08 04:20:36 -08001889 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1890 obloc.partitionReferenceNum,
1891 obloc.logicalBlockNum, &err);
1892 if (!epos->block.logicalBlockNum)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001893 return -ENOSPC;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001894 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001895 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001896 0));
1897 if (!nbh)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001898 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 lock_buffer(nbh);
1900 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1901 set_buffer_uptodate(nbh);
1902 unlock_buffer(nbh);
1903 mark_buffer_dirty_inode(nbh, inode);
1904
1905 aed = (struct allocExtDesc *)(nbh->b_data);
1906 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001907 aed->previousAllocExtLocation =
1908 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001909 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001910 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 aed->lengthAllocDescs = cpu_to_le32(adsize);
1912 sptr = ptr - adsize;
1913 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1914 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001915 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001916 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001917 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 aed->lengthAllocDescs = cpu_to_le32(0);
1919 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001920 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001922 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001923 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001924 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001925 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001926 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 mark_inode_dirty(inode);
1928 }
1929 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001930 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001932 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 else
1934 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001935 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001936 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001937 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001938 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001939 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1940 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001941 sad->extPosition =
1942 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001943 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001944 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001945 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001946 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1947 inode->i_sb->s_blocksize);
1948 lad->extLocation = cpu_to_lelb(epos->block);
1949 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001952 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001953 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001954 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001955 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001957 udf_update_tag(epos->bh->b_data,
1958 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001959 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001960 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001961 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001963 }
Jan Karaff116fc2007-05-08 00:35:14 -07001964 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
Jan Kara7e49b6f2010-10-22 00:30:26 +02001967 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001969 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001970 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001972 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001973 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001974 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001975 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1976 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1977 udf_update_tag(epos->bh->b_data,
1978 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001980 udf_update_tag(epos->bh->b_data,
1981 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001982 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
Jan Kara7e49b6f2010-10-22 00:30:26 +02001985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Jan Kara7e49b6f2010-10-22 00:30:26 +02001988void udf_write_aext(struct inode *inode, struct extent_position *epos,
1989 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 int adsize;
1992 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001993 struct short_ad *sad;
1994 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001995 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Jan Karaff116fc2007-05-08 00:35:14 -07001997 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001998 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001999 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002000 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 else
Jan Karaff116fc2007-05-08 00:35:14 -07002002 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002004 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002005 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002006 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002007 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002008 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002009 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002010 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002011 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002012 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002013 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002014 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002015 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002016 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002017 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002018 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002019 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002022 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002023 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002024 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002025 struct allocExtDesc *aed =
2026 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002027 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002028 le32_to_cpu(aed->lengthAllocDescs) +
2029 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
Jan Karaff116fc2007-05-08 00:35:14 -07002031 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002032 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002037 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Marcin Slusarz4b111112008-02-08 04:20:36 -08002040int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002041 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 int8_t etype;
2044
Jan Karaff116fc2007-05-08 00:35:14 -07002045 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002046 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002047 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07002048 epos->block = *eloc;
2049 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002050 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002051 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002052 epos->bh = udf_tread(inode->i_sb, block);
2053 if (!epos->bh) {
2054 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return -1;
2056 }
2057 }
2058
2059 return etype;
2060}
2061
Marcin Slusarz4b111112008-02-08 04:20:36 -08002062int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002063 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 int alen;
2066 int8_t etype;
2067 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002068 struct short_ad *sad;
2069 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002070 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002071
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002072 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002073 if (!epos->offset)
2074 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002075 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002076 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002077 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002078 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002079 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002080 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002081 if (!epos->offset)
2082 epos->offset = sizeof(struct allocExtDesc);
2083 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002084 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002085 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2086 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002089 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002090 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002091 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2092 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002094 etype = le32_to_cpu(sad->extLength) >> 30;
2095 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002096 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002097 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002098 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2099 break;
2100 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002101 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2102 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002103 return -1;
2104 etype = le32_to_cpu(lad->extLength) >> 30;
2105 *eloc = lelb_to_cpu(lad->extLocation);
2106 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2107 break;
2108 default:
Joe Perchesa983f362011-10-10 01:08:07 -07002109 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002110 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
2113 return etype;
2114}
2115
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002116static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002117 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002119 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 uint32_t oelen;
2121 int8_t etype;
2122
Jan Karaff116fc2007-05-08 00:35:14 -07002123 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002124 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002126 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002127 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 neloc = oeloc;
2129 nelen = (etype << 30) | oelen;
2130 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002131 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002132 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return (nelen >> 30);
2135}
2136
Marcin Slusarz4b111112008-02-08 04:20:36 -08002137int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002138 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Jan Karaff116fc2007-05-08 00:35:14 -07002140 struct extent_position oepos;
2141 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 int8_t etype;
2143 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002144 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002146 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002147 get_bh(epos.bh);
2148 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002151 iinfo = UDF_I(inode);
2152 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002153 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002154 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002155 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 else
2157 adsize = 0;
2158
Jan Karaff116fc2007-05-08 00:35:14 -07002159 oepos = epos;
2160 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return -1;
2162
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002163 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002164 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002165 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002166 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002167 brelse(oepos.bh);
2168 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002169 oepos.bh = epos.bh;
2170 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002173 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 elen = 0;
2175
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002176 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002177 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2178 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2179 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002180 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002181 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002183 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002184 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002185 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002186 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002187 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002188 udf_update_tag(oepos.bh->b_data,
2189 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002191 udf_update_tag(oepos.bh->b_data,
2192 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002193 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002195 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002196 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002197 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002198 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002200 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002201 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002202 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002203 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002204 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002205 udf_update_tag(oepos.bh->b_data,
2206 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002208 udf_update_tag(oepos.bh->b_data,
2209 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002210 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002213
Jan Kara3bf25cb2007-05-08 00:35:16 -07002214 brelse(epos.bh);
2215 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return (elen >> 30);
2218}
2219
Marcin Slusarz4b111112008-02-08 04:20:36 -08002220int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002221 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002222 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002224 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002225 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002226 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002228 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002230 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002231 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2232 pos->offset = 0;
2233 pos->block = iinfo->i_location;
2234 pos->bh = NULL;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002237 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002238 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2239 if (etype == -1) {
2240 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002241 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return -1;
2243 }
2244 lbcount += *elen;
2245 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002246 /* update extent cache */
2247 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002248 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 return etype;
2251}
2252
Jan Kara60448b12007-05-08 00:35:13 -07002253long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002255 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002256 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002257 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002258 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int ret;
2260
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002261 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Marcin Slusarz4b111112008-02-08 04:20:36 -08002263 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2264 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002265 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 else
2267 ret = 0;
2268
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002269 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002270 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2273 return udf_fixed_to_variable(ret);
2274 else
2275 return ret;
2276}