blob: 28984baf61949d15adf355035da0df2d21e526d9 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070041#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "udf_i.h"
44#include "udf_sb.h"
45
46MODULE_AUTHOR("Ben Fennema");
47MODULE_DESCRIPTION("Universal Disk Format Filesystem");
48MODULE_LICENSE("GPL");
49
50#define EXTENT_MERGE_SIZE 5
51
Al Virofaa17292011-07-26 03:18:29 -040052static umode_t udf_convert_permissions(struct fileEntry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int udf_update_inode(struct inode *, int);
54static void udf_fill_inode(struct inode *, struct buffer_head *);
Jan Kara49521de2010-10-20 17:42:44 +020055static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070056static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara7b0b0932011-12-10 01:43:33 +010057static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070058static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020059 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020061 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020063 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020065 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020067 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070068 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
70
Namjae Jeon99600052013-01-19 11:17:14 +090071static void __udf_clear_extent_cache(struct inode *inode)
72{
73 struct udf_inode_info *iinfo = UDF_I(inode);
74
75 if (iinfo->cached_extent.lstart != -1) {
76 brelse(iinfo->cached_extent.epos.bh);
77 iinfo->cached_extent.lstart = -1;
78 }
79}
80
81/* Invalidate extent cache */
82static void udf_clear_extent_cache(struct inode *inode)
83{
84 struct udf_inode_info *iinfo = UDF_I(inode);
85
86 spin_lock(&iinfo->i_extent_cache_lock);
87 __udf_clear_extent_cache(inode);
88 spin_unlock(&iinfo->i_extent_cache_lock);
89}
90
91/* Return contents of extent cache */
92static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
93 loff_t *lbcount, struct extent_position *pos)
94{
95 struct udf_inode_info *iinfo = UDF_I(inode);
96 int ret = 0;
97
98 spin_lock(&iinfo->i_extent_cache_lock);
99 if ((iinfo->cached_extent.lstart <= bcount) &&
100 (iinfo->cached_extent.lstart != -1)) {
101 /* Cache hit */
102 *lbcount = iinfo->cached_extent.lstart;
103 memcpy(pos, &iinfo->cached_extent.epos,
104 sizeof(struct extent_position));
105 if (pos->bh)
106 get_bh(pos->bh);
107 ret = 1;
108 }
109 spin_unlock(&iinfo->i_extent_cache_lock);
110 return ret;
111}
112
113/* Add extent to extent cache */
114static void udf_update_extent_cache(struct inode *inode, loff_t estart,
115 struct extent_position *pos, int next_epos)
116{
117 struct udf_inode_info *iinfo = UDF_I(inode);
118
119 spin_lock(&iinfo->i_extent_cache_lock);
120 /* Invalidate previously cached extent */
121 __udf_clear_extent_cache(inode);
122 if (pos->bh)
123 get_bh(pos->bh);
124 memcpy(&iinfo->cached_extent.epos, pos,
125 sizeof(struct extent_position));
126 iinfo->cached_extent.lstart = estart;
127 if (next_epos)
128 switch (iinfo->i_alloc_type) {
129 case ICBTAG_FLAG_AD_SHORT:
130 iinfo->cached_extent.epos.offset -=
131 sizeof(struct short_ad);
132 break;
133 case ICBTAG_FLAG_AD_LONG:
134 iinfo->cached_extent.epos.offset -=
135 sizeof(struct long_ad);
136 }
137 spin_unlock(&iinfo->i_extent_cache_lock);
138}
Christoph Hellwigb1e32122008-02-22 12:38:48 +0100139
Al Viro3aac2b62010-06-07 00:43:39 -0400140void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Jan Kara2c948b32009-12-03 13:39:28 +0100142 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400143 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +0100144
Al Viro3aac2b62010-06-07 00:43:39 -0400145 if (!inode->i_nlink && !is_bad_inode(inode)) {
146 want_delete = 1;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200147 udf_setsize(inode, 0);
Al Viro3aac2b62010-06-07 00:43:39 -0400148 udf_update_inode(inode, IS_SYNC(inode));
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700149 }
150 truncate_inode_pages_final(&inode->i_data);
Al Viro3aac2b62010-06-07 00:43:39 -0400151 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200152 clear_inode(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100153 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
154 inode->i_size != iinfo->i_lenExtents) {
Joe Perches78ace702011-10-10 01:08:05 -0700155 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",
156 inode->i_ino, inode->i_mode,
157 (unsigned long long)inode->i_size,
158 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800160 kfree(iinfo->i_ext.i_data);
161 iinfo->i_ext.i_data = NULL;
Namjae Jeon99600052013-01-19 11:17:14 +0900162 udf_clear_extent_cache(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400163 if (want_delete) {
Al Viro3aac2b62010-06-07 00:43:39 -0400164 udf_free_inode(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Ian Abbott5eec54f2012-09-05 17:44:31 +0100168static void udf_write_failed(struct address_space *mapping, loff_t to)
169{
170 struct inode *inode = mapping->host;
171 struct udf_inode_info *iinfo = UDF_I(inode);
172 loff_t isize = inode->i_size;
173
174 if (to > isize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700175 truncate_pagecache(inode, isize);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100176 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
177 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900178 udf_clear_extent_cache(inode);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100179 udf_truncate_extents(inode);
180 up_write(&iinfo->i_data_sem);
181 }
182 }
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static int udf_writepage(struct page *page, struct writeback_control *wbc)
186{
187 return block_write_full_page(page, udf_get_block, wbc);
188}
189
Namjae Jeon378b8e12012-08-31 12:49:07 -0400190static int udf_writepages(struct address_space *mapping,
191 struct writeback_control *wbc)
192{
193 return mpage_writepages(mapping, wbc, udf_get_block);
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static int udf_readpage(struct file *file, struct page *page)
197{
Namjae Jeonbc112322011-10-03 14:02:59 +0900198 return mpage_readpage(page, udf_get_block);
199}
200
201static int udf_readpages(struct file *file, struct address_space *mapping,
202 struct list_head *pages, unsigned nr_pages)
203{
204 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Nick Pigginbe021ee2007-10-16 01:25:20 -0700207static int udf_write_begin(struct file *file, struct address_space *mapping,
208 loff_t pos, unsigned len, unsigned flags,
209 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200211 int ret;
212
213 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100214 if (unlikely(ret))
215 udf_write_failed(mapping, pos + len);
216 return ret;
217}
Jan Kara7e49b6f2010-10-22 00:30:26 +0200218
Ian Abbott5eec54f2012-09-05 17:44:31 +0100219static ssize_t udf_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -0500220 struct iov_iter *iter,
221 loff_t offset)
Ian Abbott5eec54f2012-09-05 17:44:31 +0100222{
223 struct file *file = iocb->ki_filp;
224 struct address_space *mapping = file->f_mapping;
225 struct inode *inode = mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -0500226 size_t count = iov_iter_count(iter);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100227 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200228
Al Virod8d3d942014-03-04 21:27:34 -0500229 ret = blockdev_direct_IO(rw, iocb, inode, iter->iov, offset, iter->nr_segs,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100230 udf_get_block);
231 if (unlikely(ret < 0 && (rw & WRITE)))
Al Viroa6cbcd42014-03-04 22:38:00 -0500232 udf_write_failed(mapping, offset + count);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200233 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static sector_t udf_bmap(struct address_space *mapping, sector_t block)
237{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700238 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700241const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700242 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900243 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700244 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400245 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100246 .write_begin = udf_write_begin,
247 .write_end = generic_write_end,
248 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700249 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Jan Karad2eb8c32011-12-10 02:30:48 +0100252/*
253 * Expand file stored in ICB to a normal one-block-file
254 *
255 * This function requires i_data_sem for writing and releases it.
256 * This function requires i_mutex held
257 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200258int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct page *page;
261 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800262 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200263 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct writeback_control udf_wbc = {
265 .sync_mode = WB_SYNC_NONE,
266 .nr_to_write = 1,
267 };
268
Jan Kara09ebb172014-02-18 12:00:21 +0100269 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800270 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800272 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800274 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200275 /* from now on we have normal address_space methods */
276 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100277 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100281 /*
282 * Release i_data_sem so that we can lock a page - page lock ranks
283 * above i_data_sem. i_mutex still protects us against file changes.
284 */
285 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jan Kara7e49b6f2010-10-22 00:30:26 +0200287 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
288 if (!page)
289 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700290
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700291 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800293 memset(kaddr + iinfo->i_lenAlloc, 0x00,
294 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
295 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
296 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 flush_dcache_page(page);
298 SetPageUptodate(page);
299 kunmap(page);
300 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100301 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800302 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
303 iinfo->i_lenAlloc);
304 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800306 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800308 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200309 /* from now on we have normal address_space methods */
310 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100311 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200312 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
313 if (err) {
314 /* Restore everything back so that we don't lose data... */
315 lock_page(page);
316 kaddr = kmap(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100317 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200318 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
319 inode->i_size);
320 kunmap(page);
321 unlock_page(page);
322 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
323 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100324 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200328
329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700332struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
333 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700336 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200337 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700339 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800342 loff_t f_pos = udf_ext0_offset(inode);
343 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800345 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
348 alloctype = ICBTAG_FLAG_AD_SHORT;
349 else
350 alloctype = ICBTAG_FLAG_AD_LONG;
351
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800353 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 mark_inode_dirty(inode);
355 return NULL;
356 }
357
358 /* alloc block, and copy data to it */
359 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800360 iinfo->i_location.partitionReferenceNum,
361 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!(*block))
363 return NULL;
364 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800365 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800366 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (!newblock)
368 return NULL;
369 dbh = udf_tgetblk(inode->i_sb, newblock);
370 if (!dbh)
371 return NULL;
372 lock_buffer(dbh);
373 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
374 set_buffer_uptodate(dbh);
375 unlock_buffer(dbh);
376 mark_buffer_dirty_inode(dbh, inode);
377
Marcin Slusarz4b111112008-02-08 04:20:36 -0800378 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800379 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700380 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 dfibh.soffset = dfibh.eoffset = 0;
382 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800383 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800384 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800385 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
386 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700387 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700388 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return NULL;
390 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800391 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sfi->descTag.tagLocation = cpu_to_le32(*block);
393 dfibh.soffset = dfibh.eoffset;
394 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
395 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
396 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800397 sfi->fileIdent +
398 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800399 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700400 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return NULL;
402 }
403 }
404 mark_buffer_dirty_inode(dbh, inode);
405
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800406 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
407 iinfo->i_lenAlloc);
408 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800410 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800411 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100412 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700413 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800414 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700415 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100416 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* UniqueID stuff */
418
Jan Kara3bf25cb2007-05-08 00:35:16 -0700419 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mark_inode_dirty(inode);
421 return dbh;
422}
423
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700424static int udf_get_block(struct inode *inode, sector_t block,
425 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800428 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800429 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700431 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 phys = udf_block_map(inode, block);
433 if (phys)
434 map_bh(bh_result, inode->i_sb, phys);
435 return 0;
436 }
437
438 err = -EIO;
439 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800440 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100441
442 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800443 if (block == iinfo->i_next_alloc_block + 1) {
444 iinfo->i_next_alloc_block++;
445 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
Namjae Jeon99600052013-01-19 11:17:14 +0900448 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100449 phys = inode_getblk(inode, block, &err, &new);
450 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (new)
454 set_buffer_new(bh_result);
455 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700456
457abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100458 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700462static struct buffer_head *udf_getblk(struct inode *inode, long block,
463 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700465 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct buffer_head dummy;
467
468 dummy.b_state = 0;
469 dummy.b_blocknr = -1000;
470 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700471 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700473 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 lock_buffer(bh);
475 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
476 set_buffer_uptodate(bh);
477 unlock_buffer(bh);
478 mark_buffer_dirty_inode(bh, inode);
479 }
480 return bh;
481 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return NULL;
484}
485
Jan Kara31170b62007-05-08 00:35:21 -0700486/* Extend the file by 'blocks' blocks, return the number of extents added */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200487static int udf_do_extend_file(struct inode *inode,
488 struct extent_position *last_pos,
489 struct kernel_long_ad *last_ext,
490 sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700491{
492 sector_t add;
493 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
494 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200495 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700496 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800497 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200498 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700499
500 /* The previous extent is fake and we should not extend by anything
501 * - there's nothing to do... */
502 if (!blocks && fake)
503 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700504
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800505 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700506 /* Round the last extent up to a multiple of block size */
507 if (last_ext->extLength & (sb->s_blocksize - 1)) {
508 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700509 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
510 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
511 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800512 iinfo->i_lenExtents =
513 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700514 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700515 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700516
Jan Kara31170b62007-05-08 00:35:21 -0700517 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800518 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
519 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700520 /* Save the extent so that we can reattach it to the end */
521 prealloc_loc = last_ext->extLocation;
522 prealloc_len = last_ext->extLength;
523 /* Mark the extent as a hole */
524 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700525 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700526 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800527 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700528 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700529
Jan Kara31170b62007-05-08 00:35:21 -0700530 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800531 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
532 EXT_NOT_RECORDED_NOT_ALLOCATED) {
533 add = ((1 << 30) - sb->s_blocksize -
534 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
535 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700536 if (add > blocks)
537 add = blocks;
538 blocks -= add;
539 last_ext->extLength += add << sb->s_blocksize_bits;
540 }
541
542 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200543 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700544 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700545 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800546 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200547 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800548 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549
Jan Kara31170b62007-05-08 00:35:21 -0700550 /* Managed to do everything necessary? */
551 if (!blocks)
552 goto out;
553
554 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
555 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800556 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700557 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800558 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
559 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700560
Jan Kara31170b62007-05-08 00:35:21 -0700561 /* Create enough extents to cover the whole hole */
562 while (blocks > add) {
563 blocks -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200564 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
565 last_ext->extLength, 1);
566 if (err)
567 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700568 count++;
569 }
570 if (blocks) {
571 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700572 (blocks << sb->s_blocksize_bits);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200573 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
574 last_ext->extLength, 1);
575 if (err)
576 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700577 count++;
578 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700579
580out:
Jan Kara31170b62007-05-08 00:35:21 -0700581 /* Do we have some preallocated blocks saved? */
582 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200583 err = udf_add_aext(inode, last_pos, &prealloc_loc,
584 prealloc_len, 1);
585 if (err)
586 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700587 last_ext->extLocation = prealloc_loc;
588 last_ext->extLength = prealloc_len;
589 count++;
590 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700591
Jan Kara31170b62007-05-08 00:35:21 -0700592 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800593 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200594 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800595 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200596 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700597 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200598 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700599
Jan Kara31170b62007-05-08 00:35:21 -0700600 return count;
601}
602
Jan Kara7e49b6f2010-10-22 00:30:26 +0200603static int udf_extend_file(struct inode *inode, loff_t newsize)
604{
605
606 struct extent_position epos;
607 struct kernel_lb_addr eloc;
608 uint32_t elen;
609 int8_t etype;
610 struct super_block *sb = inode->i_sb;
611 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
612 int adsize;
613 struct udf_inode_info *iinfo = UDF_I(inode);
614 struct kernel_long_ad extent;
615 int err;
616
617 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
618 adsize = sizeof(struct short_ad);
619 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
620 adsize = sizeof(struct long_ad);
621 else
622 BUG();
623
624 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
625
626 /* File has extent covering the new size (could happen when extending
627 * inside a block)? */
628 if (etype != -1)
629 return 0;
630 if (newsize & (sb->s_blocksize - 1))
631 offset++;
632 /* Extended file just to the boundary of the last file block? */
633 if (offset == 0)
634 return 0;
635
636 /* Truncate is extending the file by 'offset' blocks */
637 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
638 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
639 /* File has no extents at all or has empty last
640 * indirect extent! Create a fake extent... */
641 extent.extLocation.logicalBlockNum = 0;
642 extent.extLocation.partitionReferenceNum = 0;
643 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
644 } else {
645 epos.offset -= adsize;
646 etype = udf_next_aext(inode, &epos, &extent.extLocation,
647 &extent.extLength, 0);
648 extent.extLength |= etype << 30;
649 }
650 err = udf_do_extend_file(inode, &epos, &extent, offset);
651 if (err < 0)
652 goto out;
653 err = 0;
654 iinfo->i_lenExtents = newsize;
655out:
656 brelse(epos.bh);
657 return err;
658}
659
Jan Kara7b0b0932011-12-10 01:43:33 +0100660static sector_t inode_getblk(struct inode *inode, sector_t block,
661 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200663 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700664 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700666 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200667 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700669 loff_t lbcount = 0, b_off = 0;
670 uint32_t newblocknum, newblock;
671 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800673 struct udf_inode_info *iinfo = UDF_I(inode);
674 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700675 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900676 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Jan Kara7b0b0932011-12-10 01:43:33 +0100678 *err = 0;
679 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700680 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800681 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700682 prev_epos.bh = NULL;
683 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700684 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700687 alternate between laarr[0] and laarr[1] for locations of the
688 current extent, and the previous extent */
689 do {
690 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700691 brelse(prev_epos.bh);
692 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700693 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700695 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700696 brelse(cur_epos.bh);
697 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700698 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 lbcount += elen;
702
Jan Karaff116fc2007-05-08 00:35:14 -0700703 prev_epos.block = cur_epos.block;
704 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jan Karaff116fc2007-05-08 00:35:14 -0700706 prev_epos.offset = cur_epos.offset;
707 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Marcin Slusarz4b111112008-02-08 04:20:36 -0800709 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
710 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712
713 c = !c;
714
715 laarr[c].extLength = (etype << 30) | elen;
716 laarr[c].extLocation = eloc;
717
718 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
719 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700720 ((elen + inode->i_sb->s_blocksize - 1) >>
721 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700723 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 } while (lbcount + elen <= b_off);
725
726 b_off -= lbcount;
727 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700728 /*
729 * Move prev_epos and cur_epos into indirect extent if we are at
730 * the pointer to it
731 */
732 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
733 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700736 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700738 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
739 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700741 ((elen + inode->i_sb->s_blocksize - 1) &
742 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200743 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700745 brelse(prev_epos.bh);
746 brelse(cur_epos.bh);
747 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200748 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Jan Kara7b0b0932011-12-10 01:43:33 +0100749 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Jan Kara31170b62007-05-08 00:35:21 -0700752 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700753 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700754 int ret;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900755 isBeyondEOF = 1;
Jan Kara31170b62007-05-08 00:35:21 -0700756 if (count) {
757 if (c)
758 laarr[0] = laarr[1];
759 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700760 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700761 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800762 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200763 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700764 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200765 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800766 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700767 startnum = (offset > 0);
768 }
769 /* Create extents for the hole between EOF and offset */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200770 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
771 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700772 brelse(prev_epos.bh);
773 brelse(cur_epos.bh);
774 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200775 *err = ret;
Jan Kara7b0b0932011-12-10 01:43:33 +0100776 return 0;
Jan Kara31170b62007-05-08 00:35:21 -0700777 }
778 c = 0;
779 offset = 0;
780 count += ret;
781 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800782 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
783 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700784 /* Is there any real extent? - otherwise we overwrite
785 * the fake one... */
786 if (count)
787 c = !c;
788 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700789 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800790 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200791 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700793 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700794 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700796 } else {
Namjae Jeonfb719c52012-10-10 00:09:12 +0900797 isBeyondEOF = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 endnum = startnum = ((count > 2) ? 2 : count);
799
Marcin Slusarz4b111112008-02-08 04:20:36 -0800800 /* if the current extent is in position 0,
801 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700802 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700803 laarr[2] = laarr[0];
804 laarr[0] = laarr[1];
805 laarr[1] = laarr[2];
806 c = 1;
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Marcin Slusarz4b111112008-02-08 04:20:36 -0800809 /* if the current block is located in an extent,
810 read the next extent */
811 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
812 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700813 laarr[c + 1].extLength = (etype << 30) | elen;
814 laarr[c + 1].extLocation = eloc;
815 count++;
816 startnum++;
817 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 lastblock = 1;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700823 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800824 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800826 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800827 if (iinfo->i_next_alloc_block == block)
828 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700830 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800831 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800832 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Marcin Slusarz4b111112008-02-08 04:20:36 -0800835 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800836 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800837 goal, err);
838 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700839 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900840 brelse(cur_epos.bh);
841 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 *err = -ENOSPC;
Jan Kara7b0b0932011-12-10 01:43:33 +0100843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900845 if (isBeyondEOF)
846 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
Marcin Slusarz4b111112008-02-08 04:20:36 -0800849 /* if the extent the requsted block is located in contains multiple
850 * blocks, split the extent into at most three extents. blocks prior
851 * to requested block, requested block, and blocks after requested
852 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
854
855#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200856 /* We preallocate blocks only for regular files. It also makes sense
857 * for directories but there's a problem when to drop the
858 * preallocation. We might use some delayed work for that but I feel
859 * it's overengineering for a filesystem like UDF. */
860 if (S_ISREG(inode->i_mode))
861 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862#endif
863
864 /* merge any continuous blocks in laarr */
865 udf_merge_extents(inode, laarr, &endnum);
866
867 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700868 * of extents is greater than the old number, and deleting extents if
869 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700870 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Jan Kara3bf25cb2007-05-08 00:35:16 -0700872 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900873 brelse(cur_epos.bh);
874 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Marcin Slusarz4b111112008-02-08 04:20:36 -0800876 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800877 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100878 if (!newblock) {
879 *err = -EIO;
880 return 0;
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800883 iinfo->i_next_alloc_block = block;
884 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 inode->i_ctime = current_fs_time(inode->i_sb);
886
887 if (IS_SYNC(inode))
888 udf_sync_inode(inode);
889 else
890 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700891
Jan Kara7b0b0932011-12-10 01:43:33 +0100892 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700895static void udf_split_extents(struct inode *inode, int *c, int offset,
896 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200897 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700898 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800900 unsigned long blocksize = inode->i_sb->s_blocksize;
901 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800904 (laarr[*c].extLength >> 30) ==
905 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int curr = *c;
907 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800908 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int8_t etype = (laarr[curr].extLength >> 30);
910
Marcin Slusarz4b111112008-02-08 04:20:36 -0800911 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700912 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800913 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700914 laarr[curr + 2] = laarr[curr + 1];
915 laarr[curr + 1] = laarr[curr];
916 } else {
917 laarr[curr + 3] = laarr[curr + 1];
918 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700921 if (offset) {
922 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800923 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200924 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800925 0, offset);
926 laarr[curr].extLength =
927 EXT_NOT_RECORDED_NOT_ALLOCATED |
928 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800930 laarr[curr].extLocation.
931 partitionReferenceNum = 0;
932 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800934 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700935 curr++;
936 (*c)++;
937 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 laarr[curr].extLocation.logicalBlockNum = newblocknum;
941 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
942 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800943 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800945 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700946 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700948 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800950 laarr[curr].extLocation.logicalBlockNum +=
951 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700952 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800953 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700954 curr++;
955 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 }
958}
959
960static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200961 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700962 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 int start, length = 0, currlength = 0, i;
965
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700966 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!lastblock)
968 return;
969 else
970 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700971 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800972 if ((laarr[c + 1].extLength >> 30) ==
973 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700974 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800975 length = currlength =
976 (((laarr[c + 1].extLength &
977 UDF_EXTENT_LENGTH_MASK) +
978 inode->i_sb->s_blocksize - 1) >>
979 inode->i_sb->s_blocksize_bits);
980 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 start = c;
982 }
983
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700984 for (i = start + 1; i <= *endnum; i++) {
985 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (lastblock)
987 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800988 } else if ((laarr[i].extLength >> 30) ==
989 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
990 length += (((laarr[i].extLength &
991 UDF_EXTENT_LENGTH_MASK) +
992 inode->i_sb->s_blocksize - 1) >>
993 inode->i_sb->s_blocksize_bits);
994 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
996 }
997
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700998 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001000 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001001 inode->i_sb->s_blocksize - 1) >>
1002 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001004 laarr[start].extLocation.partitionReferenceNum,
1005 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1006 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1007 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001008 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001009 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001011 (numalloc <<
1012 inode->i_sb->s_blocksize_bits);
1013 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001014 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001015 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001016 (*endnum)++;
1017 laarr[c + 1].extLocation.logicalBlockNum = next;
1018 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001019 laarr[c].extLocation.
1020 partitionReferenceNum;
1021 laarr[c + 1].extLength =
1022 EXT_NOT_RECORDED_ALLOCATED |
1023 (numalloc <<
1024 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001025 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001028 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001029 int elen = ((laarr[i].extLength &
1030 UDF_EXTENT_LENGTH_MASK) +
1031 inode->i_sb->s_blocksize - 1) >>
1032 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001034 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001036 (numalloc <<
1037 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001039 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001041 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001042 memmove(&laarr[i],
1043 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001044 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001045 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001046 i--;
1047 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001050 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001051 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 }
1054}
1055
1056static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001057 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001058 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001061 unsigned long blocksize = inode->i_sb->s_blocksize;
1062 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001064 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001065 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1066 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Marcin Slusarz4b111112008-02-08 04:20:36 -08001068 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1069 (((li->extLength >> 30) ==
1070 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1071 ((lip1->extLocation.logicalBlockNum -
1072 li->extLocation.logicalBlockNum) ==
1073 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1074 blocksize - 1) >> blocksize_bits)))) {
1075
1076 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1077 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1078 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1079 lip1->extLength = (lip1->extLength -
1080 (li->extLength &
1081 UDF_EXTENT_LENGTH_MASK) +
1082 UDF_EXTENT_LENGTH_MASK) &
1083 ~(blocksize - 1);
1084 li->extLength = (li->extLength &
1085 UDF_EXTENT_FLAG_MASK) +
1086 (UDF_EXTENT_LENGTH_MASK + 1) -
1087 blocksize;
1088 lip1->extLocation.logicalBlockNum =
1089 li->extLocation.logicalBlockNum +
1090 ((li->extLength &
1091 UDF_EXTENT_LENGTH_MASK) >>
1092 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001093 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001094 li->extLength = lip1->extLength +
1095 (((li->extLength &
1096 UDF_EXTENT_LENGTH_MASK) +
1097 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001098 if (*endnum > (i + 2))
1099 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001100 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001101 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001102 i--;
1103 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001105 } else if (((li->extLength >> 30) ==
1106 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1107 ((lip1->extLength >> 30) ==
1108 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001109 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001110 ((li->extLength &
1111 UDF_EXTENT_LENGTH_MASK) +
1112 blocksize - 1) >> blocksize_bits);
1113 li->extLocation.logicalBlockNum = 0;
1114 li->extLocation.partitionReferenceNum = 0;
1115
1116 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1117 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1118 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1119 lip1->extLength = (lip1->extLength -
1120 (li->extLength &
1121 UDF_EXTENT_LENGTH_MASK) +
1122 UDF_EXTENT_LENGTH_MASK) &
1123 ~(blocksize - 1);
1124 li->extLength = (li->extLength &
1125 UDF_EXTENT_FLAG_MASK) +
1126 (UDF_EXTENT_LENGTH_MASK + 1) -
1127 blocksize;
1128 } else {
1129 li->extLength = lip1->extLength +
1130 (((li->extLength &
1131 UDF_EXTENT_LENGTH_MASK) +
1132 blocksize - 1) & ~(blocksize - 1));
1133 if (*endnum > (i + 2))
1134 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001135 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001136 (*endnum - (i + 2)));
1137 i--;
1138 (*endnum)--;
1139 }
1140 } else if ((li->extLength >> 30) ==
1141 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1142 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001143 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001144 ((li->extLength &
1145 UDF_EXTENT_LENGTH_MASK) +
1146 blocksize - 1) >> blocksize_bits);
1147 li->extLocation.logicalBlockNum = 0;
1148 li->extLocation.partitionReferenceNum = 0;
1149 li->extLength = (li->extLength &
1150 UDF_EXTENT_LENGTH_MASK) |
1151 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153 }
1154}
1155
1156static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001157 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001158 int startnum, int endnum,
1159 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001162 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 uint32_t tmplen;
1164
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001165 if (startnum > endnum) {
1166 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001167 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001168 laarr[i].extLength);
1169 } else if (startnum < endnum) {
1170 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001171 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001172 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001173 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001174 &laarr[i].extLength, 1);
1175 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 }
1178
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001179 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001180 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001181 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001182 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184}
1185
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001186struct buffer_head *udf_bread(struct inode *inode, int block,
1187 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001189 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 bh = udf_getblk(inode, block, create, err);
1192 if (!bh)
1193 return NULL;
1194
1195 if (buffer_uptodate(bh))
1196 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 wait_on_buffer(bh);
1201 if (buffer_uptodate(bh))
1202 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 brelse(bh);
1205 *err = -EIO;
1206 return NULL;
1207}
1208
Jan Kara7e49b6f2010-10-22 00:30:26 +02001209int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001212 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +02001213 int bsize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001216 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001219 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001221 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001222 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001223 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001224 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1225 if (bsize <
1226 (udf_file_entry_alloc_offset(inode) + newsize)) {
1227 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001228 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001229 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001230 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001231 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001232 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001233 goto set_size;
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001236 err = udf_extend_file(inode, newsize);
1237 if (err) {
1238 up_write(&iinfo->i_data_sem);
1239 return err;
1240 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001241set_size:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001242 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001243 up_write(&iinfo->i_data_sem);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001244 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001245 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1246 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001247 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001248 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1249 0x00, bsize - newsize -
1250 udf_file_entry_alloc_offset(inode));
1251 iinfo->i_lenAlloc = newsize;
1252 truncate_setsize(inode, newsize);
1253 up_write(&iinfo->i_data_sem);
1254 goto update_time;
1255 }
1256 err = block_truncate_page(inode->i_mapping, newsize,
1257 udf_get_block);
1258 if (err)
1259 return err;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001260 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001261 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001262 truncate_setsize(inode, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001264 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001265 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001266update_time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1268 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001269 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 else
1271 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001275static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct buffer_head *bh = NULL;
1278 struct fileEntry *fe;
1279 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001280 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /*
1283 * Set defaults, but the inode is still incomplete!
1284 * Note: get_new_inode() sets the following on a new inode:
1285 * i_sb = sb
1286 * i_no = ino
1287 * i_flags = sb->s_flags
1288 * i_state = 0
1289 * clean_inode(): zero fills and sets
1290 * i_count = 1
1291 * i_nlink = 1
1292 * i_op = NULL;
1293 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001294 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001295 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001296 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 make_bad_inode(inode);
1298 return;
1299 }
1300
1301 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001302 ident != TAG_IDENT_USE) {
Joe Perches78ace702011-10-10 01:08:05 -07001303 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1304 inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001305 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 make_bad_inode(inode);
1307 return;
1308 }
1309
1310 fe = (struct fileEntry *)bh->b_data;
1311
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001312 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001313 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Pekka Enberg97e961f2008-10-15 12:29:03 +02001315 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001316 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001317 if (ident == TAG_IDENT_IE && ibh) {
1318 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001319 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001320 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001321
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001322 ie = (struct indirectEntry *)ibh->b_data;
1323 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001324
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001325 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001326 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001327 &ident))) {
1328 if (ident == TAG_IDENT_FE ||
1329 ident == TAG_IDENT_EFE) {
1330 memcpy(&iinfo->i_location,
1331 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001332 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001333 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001334 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001335 brelse(nbh);
1336 __udf_read_inode(inode);
1337 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001338 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001339 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001341 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001342 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001343 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Joe Perches78ace702011-10-10 01:08:05 -07001344 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1345 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001346 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 make_bad_inode(inode);
1348 return;
1349 }
1350 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001351
Jan Kara3bf25cb2007-05-08 00:35:16 -07001352 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1356{
1357 struct fileEntry *fe;
1358 struct extendedFileEntry *efe;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001359 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001360 struct udf_inode_info *iinfo = UDF_I(inode);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001361 unsigned int link_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 fe = (struct fileEntry *)bh->b_data;
1364 efe = (struct extendedFileEntry *)bh->b_data;
1365
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001366 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001367 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001368 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001369 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001371 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001372 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001373 iinfo->i_unique = 0;
1374 iinfo->i_lenEAttr = 0;
1375 iinfo->i_lenExtents = 0;
1376 iinfo->i_lenAlloc = 0;
1377 iinfo->i_next_alloc_block = 0;
1378 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001379 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001380 iinfo->i_efe = 1;
1381 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001382 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1383 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001384 make_bad_inode(inode);
1385 return;
1386 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001387 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001388 bh->b_data + sizeof(struct extendedFileEntry),
1389 inode->i_sb->s_blocksize -
1390 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001391 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001392 iinfo->i_efe = 0;
1393 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001394 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1395 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001396 make_bad_inode(inode);
1397 return;
1398 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001399 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001400 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001401 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001402 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001403 iinfo->i_efe = 0;
1404 iinfo->i_use = 1;
1405 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001406 ((struct unallocSpaceEntry *)bh->b_data)->
1407 lengthAllocDescs);
1408 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1409 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001410 make_bad_inode(inode);
1411 return;
1412 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001413 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001414 bh->b_data + sizeof(struct unallocSpaceEntry),
1415 inode->i_sb->s_blocksize -
1416 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return;
1418 }
1419
Jan Karac03cad22010-10-20 22:17:28 +02001420 read_lock(&sbi->s_cred_lock);
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001421 i_uid_write(inode, le32_to_cpu(fe->uid));
1422 if (!uid_valid(inode->i_uid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001423 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1424 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001425 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001427 i_gid_write(inode, le32_to_cpu(fe->gid));
1428 if (!gid_valid(inode->i_gid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001429 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1430 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001431 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001433 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001434 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001435 inode->i_mode = sbi->s_fmode;
1436 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001437 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001438 inode->i_mode = sbi->s_dmode;
1439 else
1440 inode->i_mode = udf_convert_permissions(fe);
1441 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001442 read_unlock(&sbi->s_cred_lock);
1443
Miklos Szeredibfe86842011-10-28 14:13:29 +02001444 link_count = le16_to_cpu(fe->fileLinkCount);
1445 if (!link_count)
1446 link_count = 1;
1447 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001448
1449 inode->i_size = le64_to_cpu(fe->informationLength);
1450 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001452 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001454 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Marcin Slusarz56774802008-02-10 11:25:31 +01001456 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001457 inode->i_atime = sbi->s_record_time;
1458
Marcin Slusarz56774802008-02-10 11:25:31 +01001459 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1460 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001461 inode->i_mtime = sbi->s_record_time;
1462
Marcin Slusarz56774802008-02-10 11:25:31 +01001463 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001464 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001466 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1467 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1468 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001469 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001470 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001471 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001472 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Marcin Slusarz56774802008-02-10 11:25:31 +01001474 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001475 inode->i_atime = sbi->s_record_time;
1476
Marcin Slusarz56774802008-02-10 11:25:31 +01001477 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1478 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001479 inode->i_mtime = sbi->s_record_time;
1480
Marcin Slusarz56774802008-02-10 11:25:31 +01001481 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001482 iinfo->i_crtime = sbi->s_record_time;
1483
Marcin Slusarz56774802008-02-10 11:25:31 +01001484 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001485 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001487 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1488 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1489 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001490 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001493 switch (fe->icbTag.fileType) {
1494 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001495 inode->i_op = &udf_dir_inode_operations;
1496 inode->i_fop = &udf_dir_operations;
1497 inode->i_mode |= S_IFDIR;
1498 inc_nlink(inode);
1499 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001500 case ICBTAG_FILE_TYPE_REALTIME:
1501 case ICBTAG_FILE_TYPE_REGULAR:
1502 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001503 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001504 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001505 inode->i_data.a_ops = &udf_adinicb_aops;
1506 else
1507 inode->i_data.a_ops = &udf_aops;
1508 inode->i_op = &udf_file_inode_operations;
1509 inode->i_fop = &udf_file_operations;
1510 inode->i_mode |= S_IFREG;
1511 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001512 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001513 inode->i_mode |= S_IFBLK;
1514 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001515 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001516 inode->i_mode |= S_IFCHR;
1517 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001518 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001519 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1520 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001521 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001522 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1523 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001524 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001525 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001526 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001527 inode->i_mode = S_IFLNK | S_IRWXUGO;
1528 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001529 case ICBTAG_FILE_TYPE_MAIN:
1530 udf_debug("METADATA FILE-----\n");
1531 break;
1532 case ICBTAG_FILE_TYPE_MIRROR:
1533 udf_debug("METADATA MIRROR FILE-----\n");
1534 break;
1535 case ICBTAG_FILE_TYPE_BITMAP:
1536 udf_debug("METADATA BITMAP FILE-----\n");
1537 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001538 default:
Joe Perches78ace702011-10-10 01:08:05 -07001539 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1540 inode->i_ino, fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001541 make_bad_inode(inode);
1542 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001544 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001545 struct deviceSpec *dsea =
1546 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001547 if (dsea) {
1548 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001549 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1550 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001552 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555}
1556
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001557static int udf_alloc_i_data(struct inode *inode, size_t size)
1558{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001559 struct udf_inode_info *iinfo = UDF_I(inode);
1560 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001561
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001562 if (!iinfo->i_ext.i_data) {
Joe Perches78ace702011-10-10 01:08:05 -07001563 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1564 inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001565 return -ENOMEM;
1566 }
1567
1568 return 0;
1569}
1570
Al Virofaa17292011-07-26 03:18:29 -04001571static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Al Virofaa17292011-07-26 03:18:29 -04001573 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 uint32_t permissions;
1575 uint32_t flags;
1576
1577 permissions = le32_to_cpu(fe->permissions);
1578 flags = le16_to_cpu(fe->icbTag.flags);
1579
Marcin Slusarz4b111112008-02-08 04:20:36 -08001580 mode = ((permissions) & S_IRWXO) |
1581 ((permissions >> 2) & S_IRWXG) |
1582 ((permissions >> 4) & S_IRWXU) |
1583 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1584 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1585 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 return mode;
1588}
1589
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001590int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Jan Kara49521de2010-10-20 17:42:44 +02001592 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Jan Kara49521de2010-10-20 17:42:44 +02001595static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 return udf_update_inode(inode, 1);
1598}
1599
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001600static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct buffer_head *bh = NULL;
1603 struct fileEntry *fe;
1604 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001605 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 uint32_t udfperms;
1607 uint16_t icbflags;
1608 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001610 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001611 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001612 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Jan Kara5833ded2010-01-08 16:52:59 +01001614 bh = udf_tgetblk(inode->i_sb,
1615 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001616 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001617 udf_debug("getblk failure\n");
1618 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
1620
Jan Karaaae917c2010-01-08 16:46:29 +01001621 lock_buffer(bh);
1622 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 fe = (struct fileEntry *)bh->b_data;
1624 efe = (struct extendedFileEntry *)bh->b_data;
1625
Jan Karaaae917c2010-01-08 16:46:29 +01001626 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001628 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001630 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001631 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001632 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001633 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001634 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1635 use->descTag.tagLocation =
1636 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001637 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001638 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001640 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001641 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001642 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001643 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Jan Karaaae917c2010-01-08 16:46:29 +01001645 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647
Phillip Susi4d6660e2006-03-07 21:55:24 -08001648 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1649 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001650 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001651 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Phillip Susi4d6660e2006-03-07 21:55:24 -08001653 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1654 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001655 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001656 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Marcin Slusarz4b111112008-02-08 04:20:36 -08001658 udfperms = ((inode->i_mode & S_IRWXO)) |
1659 ((inode->i_mode & S_IRWXG) << 2) |
1660 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Marcin Slusarz4b111112008-02-08 04:20:36 -08001662 udfperms |= (le32_to_cpu(fe->permissions) &
1663 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1664 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1665 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 fe->permissions = cpu_to_le32(udfperms);
1667
1668 if (S_ISDIR(inode->i_mode))
1669 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1670 else
1671 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1672
1673 fe->informationLength = cpu_to_le64(inode->i_size);
1674
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001675 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001676 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001677 struct deviceSpec *dsea =
1678 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001679 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001681 udf_add_extendedattr(inode,
1682 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001683 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 dsea->attrType = cpu_to_le32(12);
1685 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001686 dsea->attrLength = cpu_to_le32(
1687 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001688 sizeof(struct regid));
1689 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001691 eid = (struct regid *)dsea->impUse;
1692 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 strcpy(eid->ident, UDF_ID_DEVELOPER);
1694 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1695 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1696 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1697 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1698 }
1699
Steve Nickelb2527bf2012-02-16 12:53:53 -05001700 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1701 lb_recorded = 0; /* No extents => no blocks! */
1702 else
1703 lb_recorded =
1704 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1705 (blocksize_bits - 9);
1706
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001707 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001708 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001709 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001710 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001711 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Marcin Slusarz56774802008-02-10 11:25:31 +01001713 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1714 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1715 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001716 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1718 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1719 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001720 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1721 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1722 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001723 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1725 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001726 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001727 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001728 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001729 inode->i_sb->s_blocksize -
1730 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001732 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001734 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1735 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1736 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1737 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001738
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001739 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1740 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1741 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1742 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001743
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001744 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1745 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1746 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1747 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Marcin Slusarz56774802008-02-10 11:25:31 +01001749 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1750 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1751 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1752 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001754 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1756 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1757 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001758 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1759 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1760 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001761 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1763 crclen = sizeof(struct extendedFileEntry);
1764 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001765 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 fe->icbTag.strategyType = cpu_to_le16(4096);
1767 fe->icbTag.strategyParameter = cpu_to_le16(1);
1768 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001769 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 fe->icbTag.strategyType = cpu_to_le16(4);
1771 fe->icbTag.numEntries = cpu_to_le16(1);
1772 }
1773
1774 if (S_ISDIR(inode->i_mode))
1775 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1776 else if (S_ISREG(inode->i_mode))
1777 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1778 else if (S_ISLNK(inode->i_mode))
1779 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1780 else if (S_ISBLK(inode->i_mode))
1781 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1782 else if (S_ISCHR(inode->i_mode))
1783 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1784 else if (S_ISFIFO(inode->i_mode))
1785 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1786 else if (S_ISSOCK(inode->i_mode))
1787 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1788
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001789 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001790 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1791 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1792 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1793 (le16_to_cpu(fe->icbTag.flags) &
1794 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1795 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001798 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 fe->descTag.descVersion = cpu_to_le16(3);
1800 else
1801 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001802 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001803 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001804 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001805 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001807 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001808 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001809 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jan Karaaae917c2010-01-08 16:46:29 +01001811out:
Jan Kara5833ded2010-01-08 16:52:59 +01001812 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001813 unlock_buffer(bh);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /* write the data blocks */
1816 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001817 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001819 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001820 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1821 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 err = -EIO;
1823 }
1824 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001825 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return err;
1828}
1829
Pekka Enberg97e961f2008-10-15 12:29:03 +02001830struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1833 struct inode *inode = iget_locked(sb, block);
1834
1835 if (!inode)
1836 return NULL;
1837
1838 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001839 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 __udf_read_inode(inode);
1841 unlock_new_inode(inode);
1842 }
1843
1844 if (is_bad_inode(inode))
1845 goto out_iput;
1846
Pekka Enberg97e961f2008-10-15 12:29:03 +02001847 if (ino->logicalBlockNum >= UDF_SB(sb)->
1848 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001850 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 make_bad_inode(inode);
1852 goto out_iput;
1853 }
1854
1855 return inode;
1856
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001857 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 iput(inode);
1859 return NULL;
1860}
1861
Jan Kara7e49b6f2010-10-22 00:30:26 +02001862int udf_add_aext(struct inode *inode, struct extent_position *epos,
1863 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001866 struct short_ad *sad = NULL;
1867 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 struct allocExtDesc *aed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001870 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Jan Karaff116fc2007-05-08 00:35:14 -07001872 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001873 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001874 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001875 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 else
Jan Karaff116fc2007-05-08 00:35:14 -07001877 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001879 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001880 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001881 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001882 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001884 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001886 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001887 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 struct buffer_head *nbh;
1889 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001890 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Marcin Slusarz4b111112008-02-08 04:20:36 -08001892 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1893 obloc.partitionReferenceNum,
1894 obloc.logicalBlockNum, &err);
1895 if (!epos->block.logicalBlockNum)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001896 return -ENOSPC;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001897 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001898 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001899 0));
1900 if (!nbh)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001901 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 lock_buffer(nbh);
1903 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1904 set_buffer_uptodate(nbh);
1905 unlock_buffer(nbh);
1906 mark_buffer_dirty_inode(nbh, inode);
1907
1908 aed = (struct allocExtDesc *)(nbh->b_data);
1909 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001910 aed->previousAllocExtLocation =
1911 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001912 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001913 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 aed->lengthAllocDescs = cpu_to_le32(adsize);
1915 sptr = ptr - adsize;
1916 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1917 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001918 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001919 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001920 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 aed->lengthAllocDescs = cpu_to_le32(0);
1922 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001923 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001925 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001926 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001927 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001928 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001929 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 mark_inode_dirty(inode);
1931 }
1932 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001933 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001935 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 else
1937 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001938 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001939 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001940 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001941 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001942 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1943 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001944 sad->extPosition =
1945 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001946 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001947 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001948 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001949 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1950 inode->i_sb->s_blocksize);
1951 lad->extLocation = cpu_to_lelb(epos->block);
1952 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1953 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001955 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001956 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001957 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001958 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001960 udf_update_tag(epos->bh->b_data,
1961 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001962 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001963 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001964 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001966 }
Jan Karaff116fc2007-05-08 00:35:14 -07001967 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969
Jan Kara7e49b6f2010-10-22 00:30:26 +02001970 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001972 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001973 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001975 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001976 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001977 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001978 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1979 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1980 udf_update_tag(epos->bh->b_data,
1981 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001983 udf_update_tag(epos->bh->b_data,
1984 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001985 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987
Jan Kara7e49b6f2010-10-22 00:30:26 +02001988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Jan Kara7e49b6f2010-10-22 00:30:26 +02001991void udf_write_aext(struct inode *inode, struct extent_position *epos,
1992 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 int adsize;
1995 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001996 struct short_ad *sad;
1997 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001998 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Jan Karaff116fc2007-05-08 00:35:14 -07002000 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002001 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002002 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002003 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 else
Jan Karaff116fc2007-05-08 00:35:14 -07002005 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002007 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002008 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002009 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002010 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002011 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002012 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002013 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002014 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002015 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002016 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002017 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002018 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002019 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002020 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002021 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002025 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002026 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002027 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002028 struct allocExtDesc *aed =
2029 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002030 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002031 le32_to_cpu(aed->lengthAllocDescs) +
2032 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Jan Karaff116fc2007-05-08 00:35:14 -07002034 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002035 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002040 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Marcin Slusarz4b111112008-02-08 04:20:36 -08002043int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002044 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 int8_t etype;
2047
Jan Karaff116fc2007-05-08 00:35:14 -07002048 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002049 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002050 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07002051 epos->block = *eloc;
2052 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002053 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002054 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002055 epos->bh = udf_tread(inode->i_sb, block);
2056 if (!epos->bh) {
2057 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return -1;
2059 }
2060 }
2061
2062 return etype;
2063}
2064
Marcin Slusarz4b111112008-02-08 04:20:36 -08002065int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002066 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 int alen;
2069 int8_t etype;
2070 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002071 struct short_ad *sad;
2072 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002073 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002074
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002075 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002076 if (!epos->offset)
2077 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002078 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002079 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002080 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002081 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002082 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002083 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002084 if (!epos->offset)
2085 epos->offset = sizeof(struct allocExtDesc);
2086 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002087 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002088 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2089 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002092 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002093 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002094 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2095 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002097 etype = le32_to_cpu(sad->extLength) >> 30;
2098 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002099 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002100 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002101 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2102 break;
2103 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002104 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2105 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002106 return -1;
2107 etype = le32_to_cpu(lad->extLength) >> 30;
2108 *eloc = lelb_to_cpu(lad->extLocation);
2109 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2110 break;
2111 default:
Joe Perchesa983f362011-10-10 01:08:07 -07002112 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002113 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115
2116 return etype;
2117}
2118
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002119static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002120 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002122 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 uint32_t oelen;
2124 int8_t etype;
2125
Jan Karaff116fc2007-05-08 00:35:14 -07002126 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002127 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002129 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002130 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 neloc = oeloc;
2132 nelen = (etype << 30) | oelen;
2133 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002134 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002135 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return (nelen >> 30);
2138}
2139
Marcin Slusarz4b111112008-02-08 04:20:36 -08002140int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002141 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Jan Karaff116fc2007-05-08 00:35:14 -07002143 struct extent_position oepos;
2144 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 int8_t etype;
2146 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002147 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002149 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002150 get_bh(epos.bh);
2151 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002154 iinfo = UDF_I(inode);
2155 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002156 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002157 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002158 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 else
2160 adsize = 0;
2161
Jan Karaff116fc2007-05-08 00:35:14 -07002162 oepos = epos;
2163 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return -1;
2165
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002166 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002167 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002168 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002169 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002170 brelse(oepos.bh);
2171 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002172 oepos.bh = epos.bh;
2173 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002176 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 elen = 0;
2178
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002179 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002180 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2181 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2182 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002183 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002184 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002186 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002187 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002188 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002189 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002190 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002191 udf_update_tag(oepos.bh->b_data,
2192 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002194 udf_update_tag(oepos.bh->b_data,
2195 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002196 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002198 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002199 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002200 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002201 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002203 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002204 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002205 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002206 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002207 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002208 udf_update_tag(oepos.bh->b_data,
2209 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002211 udf_update_tag(oepos.bh->b_data,
2212 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002213 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002216
Jan Kara3bf25cb2007-05-08 00:35:16 -07002217 brelse(epos.bh);
2218 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return (elen >> 30);
2221}
2222
Marcin Slusarz4b111112008-02-08 04:20:36 -08002223int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002224 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002225 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002227 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002228 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002229 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002231 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002233 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002234 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2235 pos->offset = 0;
2236 pos->block = iinfo->i_location;
2237 pos->bh = NULL;
2238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002240 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002241 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2242 if (etype == -1) {
2243 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002244 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return -1;
2246 }
2247 lbcount += *elen;
2248 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002249 /* update extent cache */
2250 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002251 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 return etype;
2254}
2255
Jan Kara60448b12007-05-08 00:35:13 -07002256long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002258 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002259 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002260 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002261 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 int ret;
2263
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002264 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Marcin Slusarz4b111112008-02-08 04:20:36 -08002266 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2267 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002268 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 else
2270 ret = 0;
2271
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002272 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002273 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2276 return udf_fixed_to_variable(ret);
2277 else
2278 return ret;
2279}