blob: 8d8eda8379caa05720daf418ea6e3c9f2356c500 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
16 *
17 * HISTORY
18 *
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
Marcin Slusarz4b111112008-02-08 04:20:36 -080022 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
30 */
31
32#include "udfdecl.h"
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/writeback.h>
37#include <linux/slab.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020038#include <linux/crc-itu-t.h>
Namjae Jeonbc112322011-10-03 14:02:59 +090039#include <linux/mpage.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080040#include <linux/uio.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060041#include <linux/bio.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);
Jan Kara49521de2010-10-20 17:42:44 +020054static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070055static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara7b0b0932011-12-10 01:43:33 +010056static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070057static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020058 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void udf_split_extents(struct inode *, int *, int, int,
Fabian Frederick3cc6f842017-01-06 21:53:50 +010060 struct kernel_long_ad *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void udf_prealloc_extents(struct inode *, int, int,
Fabian Frederick3cc6f842017-01-06 21:53:50 +010062 struct kernel_long_ad *, int *);
63static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
64static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
65 int, struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
67
Namjae Jeon99600052013-01-19 11:17:14 +090068static void __udf_clear_extent_cache(struct inode *inode)
69{
70 struct udf_inode_info *iinfo = UDF_I(inode);
71
72 if (iinfo->cached_extent.lstart != -1) {
73 brelse(iinfo->cached_extent.epos.bh);
74 iinfo->cached_extent.lstart = -1;
75 }
76}
77
78/* Invalidate extent cache */
79static void udf_clear_extent_cache(struct inode *inode)
80{
81 struct udf_inode_info *iinfo = UDF_I(inode);
82
83 spin_lock(&iinfo->i_extent_cache_lock);
84 __udf_clear_extent_cache(inode);
85 spin_unlock(&iinfo->i_extent_cache_lock);
86}
87
88/* Return contents of extent cache */
89static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
90 loff_t *lbcount, struct extent_position *pos)
91{
92 struct udf_inode_info *iinfo = UDF_I(inode);
93 int ret = 0;
94
95 spin_lock(&iinfo->i_extent_cache_lock);
96 if ((iinfo->cached_extent.lstart <= bcount) &&
97 (iinfo->cached_extent.lstart != -1)) {
98 /* Cache hit */
99 *lbcount = iinfo->cached_extent.lstart;
100 memcpy(pos, &iinfo->cached_extent.epos,
101 sizeof(struct extent_position));
102 if (pos->bh)
103 get_bh(pos->bh);
104 ret = 1;
105 }
106 spin_unlock(&iinfo->i_extent_cache_lock);
107 return ret;
108}
109
110/* Add extent to extent cache */
111static void udf_update_extent_cache(struct inode *inode, loff_t estart,
112 struct extent_position *pos, int next_epos)
113{
114 struct udf_inode_info *iinfo = UDF_I(inode);
115
116 spin_lock(&iinfo->i_extent_cache_lock);
117 /* Invalidate previously cached extent */
118 __udf_clear_extent_cache(inode);
119 if (pos->bh)
120 get_bh(pos->bh);
121 memcpy(&iinfo->cached_extent.epos, pos,
122 sizeof(struct extent_position));
123 iinfo->cached_extent.lstart = estart;
124 if (next_epos)
125 switch (iinfo->i_alloc_type) {
126 case ICBTAG_FLAG_AD_SHORT:
127 iinfo->cached_extent.epos.offset -=
128 sizeof(struct short_ad);
129 break;
130 case ICBTAG_FLAG_AD_LONG:
131 iinfo->cached_extent.epos.offset -=
132 sizeof(struct long_ad);
133 }
134 spin_unlock(&iinfo->i_extent_cache_lock);
135}
Christoph Hellwigb1e32122008-02-22 12:38:48 +0100136
Al Viro3aac2b622010-06-07 00:43:39 -0400137void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Jan Kara2c948b32009-12-03 13:39:28 +0100139 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b622010-06-07 00:43:39 -0400140 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +0100141
Al Viro3aac2b622010-06-07 00:43:39 -0400142 if (!inode->i_nlink && !is_bad_inode(inode)) {
143 want_delete = 1;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200144 udf_setsize(inode, 0);
Al Viro3aac2b622010-06-07 00:43:39 -0400145 udf_update_inode(inode, IS_SYNC(inode));
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700146 }
147 truncate_inode_pages_final(&inode->i_data);
Al Viro3aac2b622010-06-07 00:43:39 -0400148 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200149 clear_inode(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100150 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
151 inode->i_size != iinfo->i_lenExtents) {
Joe Perches78ace702011-10-10 01:08:05 -0700152 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",
153 inode->i_ino, inode->i_mode,
154 (unsigned long long)inode->i_size,
155 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800157 kfree(iinfo->i_ext.i_data);
158 iinfo->i_ext.i_data = NULL;
Namjae Jeon99600052013-01-19 11:17:14 +0900159 udf_clear_extent_cache(inode);
Al Viro3aac2b622010-06-07 00:43:39 -0400160 if (want_delete) {
Al Viro3aac2b622010-06-07 00:43:39 -0400161 udf_free_inode(inode);
Al Viro3aac2b622010-06-07 00:43:39 -0400162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Ian Abbott5eec54f2012-09-05 17:44:31 +0100165static void udf_write_failed(struct address_space *mapping, loff_t to)
166{
167 struct inode *inode = mapping->host;
168 struct udf_inode_info *iinfo = UDF_I(inode);
169 loff_t isize = inode->i_size;
170
171 if (to > isize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700172 truncate_pagecache(inode, isize);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100173 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
174 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900175 udf_clear_extent_cache(inode);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100176 udf_truncate_extents(inode);
177 up_write(&iinfo->i_data_sem);
178 }
179 }
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static int udf_writepage(struct page *page, struct writeback_control *wbc)
183{
184 return block_write_full_page(page, udf_get_block, wbc);
185}
186
Namjae Jeon378b8e12012-08-31 12:49:07 -0400187static int udf_writepages(struct address_space *mapping,
188 struct writeback_control *wbc)
189{
190 return mpage_writepages(mapping, wbc, udf_get_block);
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int udf_readpage(struct file *file, struct page *page)
194{
Namjae Jeonbc112322011-10-03 14:02:59 +0900195 return mpage_readpage(page, udf_get_block);
196}
197
198static int udf_readpages(struct file *file, struct address_space *mapping,
199 struct list_head *pages, unsigned nr_pages)
200{
201 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Nick Pigginbe021ee2007-10-16 01:25:20 -0700204static int udf_write_begin(struct file *file, struct address_space *mapping,
205 loff_t pos, unsigned len, unsigned flags,
206 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200208 int ret;
209
210 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100211 if (unlikely(ret))
212 udf_write_failed(mapping, pos + len);
213 return ret;
214}
Jan Kara7e49b6f2010-10-22 00:30:26 +0200215
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700216static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Ian Abbott5eec54f2012-09-05 17:44:31 +0100217{
218 struct file *file = iocb->ki_filp;
219 struct address_space *mapping = file->f_mapping;
220 struct inode *inode = mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -0500221 size_t count = iov_iter_count(iter);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100222 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200223
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700224 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
Omar Sandoval6f673762015-03-16 04:33:52 -0700225 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700226 udf_write_failed(mapping, iocb->ki_pos + count);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200227 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static sector_t udf_bmap(struct address_space *mapping, sector_t block)
231{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700235const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900237 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700238 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400239 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100240 .write_begin = udf_write_begin,
241 .write_end = generic_write_end,
242 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700243 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
Jan Karad2eb8c32011-12-10 02:30:48 +0100246/*
247 * Expand file stored in ICB to a normal one-block-file
248 *
249 * This function requires i_data_sem for writing and releases it.
250 * This function requires i_mutex held
251 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200252int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct page *page;
255 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800256 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200257 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct writeback_control udf_wbc = {
259 .sync_mode = WB_SYNC_NONE,
260 .nr_to_write = 1,
261 };
262
Al Viro59551022016-01-22 15:40:57 -0500263 WARN_ON_ONCE(!inode_is_locked(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800264 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800266 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800268 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200269 /* from now on we have normal address_space methods */
270 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100271 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100275 /*
276 * Release i_data_sem so that we can lock a page - page lock ranks
277 * above i_data_sem. i_mutex still protects us against file changes.
278 */
279 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Jan Kara7e49b6f2010-10-22 00:30:26 +0200281 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
282 if (!page)
283 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700284
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700285 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800287 memset(kaddr + iinfo->i_lenAlloc, 0x00,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300288 PAGE_SIZE - iinfo->i_lenAlloc);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800289 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
290 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 flush_dcache_page(page);
292 SetPageUptodate(page);
293 kunmap(page);
294 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100295 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800296 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
297 iinfo->i_lenAlloc);
298 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800300 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800302 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200303 /* from now on we have normal address_space methods */
304 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100305 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200306 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
307 if (err) {
308 /* Restore everything back so that we don't lose data... */
309 lock_page(page);
310 kaddr = kmap(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100311 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200312 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
313 inode->i_size);
314 kunmap(page);
315 unlock_page(page);
316 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
317 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100318 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200319 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300320 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200322
323 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700326struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
327 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700330 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200331 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700333 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800336 loff_t f_pos = udf_ext0_offset(inode);
337 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800339 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
342 alloctype = ICBTAG_FLAG_AD_SHORT;
343 else
344 alloctype = ICBTAG_FLAG_AD_LONG;
345
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700346 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800347 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 mark_inode_dirty(inode);
349 return NULL;
350 }
351
352 /* alloc block, and copy data to it */
353 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800354 iinfo->i_location.partitionReferenceNum,
355 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!(*block))
357 return NULL;
358 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800359 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800360 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!newblock)
362 return NULL;
363 dbh = udf_tgetblk(inode->i_sb, newblock);
364 if (!dbh)
365 return NULL;
366 lock_buffer(dbh);
367 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
368 set_buffer_uptodate(dbh);
369 unlock_buffer(dbh);
370 mark_buffer_dirty_inode(dbh, inode);
371
Marcin Slusarz4b111112008-02-08 04:20:36 -0800372 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800373 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700374 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dfibh.soffset = dfibh.eoffset = 0;
376 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800377 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800378 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800379 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
380 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700381 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700382 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return NULL;
384 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800385 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 sfi->descTag.tagLocation = cpu_to_le32(*block);
387 dfibh.soffset = dfibh.eoffset;
388 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
389 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
390 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800391 sfi->fileIdent +
392 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800393 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700394 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return NULL;
396 }
397 }
398 mark_buffer_dirty_inode(dbh, inode);
399
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800400 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
401 iinfo->i_lenAlloc);
402 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800404 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800405 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100406 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700407 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800408 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700409 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100410 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* UniqueID stuff */
412
Jan Kara3bf25cb2007-05-08 00:35:16 -0700413 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 mark_inode_dirty(inode);
415 return dbh;
416}
417
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700418static int udf_get_block(struct inode *inode, sector_t block,
419 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800422 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800423 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700425 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 phys = udf_block_map(inode, block);
427 if (phys)
428 map_bh(bh_result, inode->i_sb, phys);
429 return 0;
430 }
431
432 err = -EIO;
433 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800434 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100435
436 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800437 if (block == iinfo->i_next_alloc_block + 1) {
438 iinfo->i_next_alloc_block++;
439 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Namjae Jeon99600052013-01-19 11:17:14 +0900442 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100443 phys = inode_getblk(inode, block, &err, &new);
444 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (new)
448 set_buffer_new(bh_result);
449 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700450
451abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100452 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700456static struct buffer_head *udf_getblk(struct inode *inode, long block,
457 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700459 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct buffer_head dummy;
461
462 dummy.b_state = 0;
463 dummy.b_blocknr = -1000;
464 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700465 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700467 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 lock_buffer(bh);
469 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
470 set_buffer_uptodate(bh);
471 unlock_buffer(bh);
472 mark_buffer_dirty_inode(bh, inode);
473 }
474 return bh;
475 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return NULL;
478}
479
Jan Kara31170b62007-05-08 00:35:21 -0700480/* Extend the file by 'blocks' blocks, return the number of extents added */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200481static int udf_do_extend_file(struct inode *inode,
482 struct extent_position *last_pos,
483 struct kernel_long_ad *last_ext,
484 sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700485{
486 sector_t add;
487 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
488 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200489 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700490 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800491 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200492 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700493
494 /* The previous extent is fake and we should not extend by anything
495 * - there's nothing to do... */
496 if (!blocks && fake)
497 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700498
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800499 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700500 /* Round the last extent up to a multiple of block size */
501 if (last_ext->extLength & (sb->s_blocksize - 1)) {
502 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700503 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
504 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
505 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800506 iinfo->i_lenExtents =
507 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700508 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700509 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700510
Jan Kara31170b62007-05-08 00:35:21 -0700511 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800512 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
513 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700514 /* Save the extent so that we can reattach it to the end */
515 prealloc_loc = last_ext->extLocation;
516 prealloc_len = last_ext->extLength;
517 /* Mark the extent as a hole */
518 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700519 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700520 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800521 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700522 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700523
Jan Kara31170b62007-05-08 00:35:21 -0700524 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800525 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
526 EXT_NOT_RECORDED_NOT_ALLOCATED) {
527 add = ((1 << 30) - sb->s_blocksize -
528 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
529 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700530 if (add > blocks)
531 add = blocks;
532 blocks -= add;
533 last_ext->extLength += add << sb->s_blocksize_bits;
534 }
535
536 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200537 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700538 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700539 count++;
Jan Kara6c371572015-12-23 18:05:03 +0100540 } else {
541 struct kernel_lb_addr tmploc;
542 uint32_t tmplen;
543
Pekka Enberg97e961f2008-10-15 12:29:03 +0200544 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800545 last_ext->extLength, 1);
Jan Kara6c371572015-12-23 18:05:03 +0100546 /*
547 * We've rewritten the last extent but there may be empty
548 * indirect extent after it - enter it.
549 */
550 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
551 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700552
Jan Kara31170b62007-05-08 00:35:21 -0700553 /* Managed to do everything necessary? */
554 if (!blocks)
555 goto out;
556
557 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
558 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800559 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700560 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800561 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
562 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700563
Jan Kara31170b62007-05-08 00:35:21 -0700564 /* Create enough extents to cover the whole hole */
565 while (blocks > add) {
566 blocks -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200567 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
568 last_ext->extLength, 1);
569 if (err)
570 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700571 count++;
572 }
573 if (blocks) {
574 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700575 (blocks << sb->s_blocksize_bits);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200576 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
577 last_ext->extLength, 1);
578 if (err)
579 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700580 count++;
581 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700582
583out:
Jan Kara31170b62007-05-08 00:35:21 -0700584 /* Do we have some preallocated blocks saved? */
585 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200586 err = udf_add_aext(inode, last_pos, &prealloc_loc,
587 prealloc_len, 1);
588 if (err)
589 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700590 last_ext->extLocation = prealloc_loc;
591 last_ext->extLength = prealloc_len;
592 count++;
593 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700594
Jan Kara31170b62007-05-08 00:35:21 -0700595 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800596 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200597 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800598 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200599 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700600 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200601 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700602
Jan Kara31170b62007-05-08 00:35:21 -0700603 return count;
604}
605
Jan Kara7e49b6f2010-10-22 00:30:26 +0200606static int udf_extend_file(struct inode *inode, loff_t newsize)
607{
608
609 struct extent_position epos;
610 struct kernel_lb_addr eloc;
611 uint32_t elen;
612 int8_t etype;
613 struct super_block *sb = inode->i_sb;
614 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
615 int adsize;
616 struct udf_inode_info *iinfo = UDF_I(inode);
617 struct kernel_long_ad extent;
618 int err;
619
620 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
621 adsize = sizeof(struct short_ad);
622 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
623 adsize = sizeof(struct long_ad);
624 else
625 BUG();
626
627 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
628
629 /* File has extent covering the new size (could happen when extending
630 * inside a block)? */
631 if (etype != -1)
632 return 0;
633 if (newsize & (sb->s_blocksize - 1))
634 offset++;
635 /* Extended file just to the boundary of the last file block? */
636 if (offset == 0)
637 return 0;
638
639 /* Truncate is extending the file by 'offset' blocks */
640 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
641 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
642 /* File has no extents at all or has empty last
643 * indirect extent! Create a fake extent... */
644 extent.extLocation.logicalBlockNum = 0;
645 extent.extLocation.partitionReferenceNum = 0;
646 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
647 } else {
648 epos.offset -= adsize;
649 etype = udf_next_aext(inode, &epos, &extent.extLocation,
650 &extent.extLength, 0);
651 extent.extLength |= etype << 30;
652 }
653 err = udf_do_extend_file(inode, &epos, &extent, offset);
654 if (err < 0)
655 goto out;
656 err = 0;
657 iinfo->i_lenExtents = newsize;
658out:
659 brelse(epos.bh);
660 return err;
661}
662
Jan Kara7b0b0932011-12-10 01:43:33 +0100663static sector_t inode_getblk(struct inode *inode, sector_t block,
664 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200666 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700667 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700669 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200670 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700672 loff_t lbcount = 0, b_off = 0;
673 uint32_t newblocknum, newblock;
674 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800676 struct udf_inode_info *iinfo = UDF_I(inode);
677 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700678 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900679 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Jan Kara7b0b0932011-12-10 01:43:33 +0100681 *err = 0;
682 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700683 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800684 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700685 prev_epos.bh = NULL;
686 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700687 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700690 alternate between laarr[0] and laarr[1] for locations of the
691 current extent, and the previous extent */
692 do {
693 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700694 brelse(prev_epos.bh);
695 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700696 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700698 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700699 brelse(cur_epos.bh);
700 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700701 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
704 lbcount += elen;
705
Jan Karaff116fc2007-05-08 00:35:14 -0700706 prev_epos.block = cur_epos.block;
707 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jan Karaff116fc2007-05-08 00:35:14 -0700709 prev_epos.offset = cur_epos.offset;
710 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Marcin Slusarz4b111112008-02-08 04:20:36 -0800712 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
713 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
715
716 c = !c;
717
718 laarr[c].extLength = (etype << 30) | elen;
719 laarr[c].extLocation = eloc;
720
721 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
722 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700723 ((elen + inode->i_sb->s_blocksize - 1) >>
724 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700726 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 } while (lbcount + elen <= b_off);
728
729 b_off -= lbcount;
730 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700731 /*
732 * Move prev_epos and cur_epos into indirect extent if we are at
733 * the pointer to it
734 */
735 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
736 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700741 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
742 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700744 ((elen + inode->i_sb->s_blocksize - 1) &
745 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200746 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700748 brelse(prev_epos.bh);
749 brelse(cur_epos.bh);
750 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200751 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Jan Kara7b0b0932011-12-10 01:43:33 +0100752 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
Jan Kara31170b62007-05-08 00:35:21 -0700755 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700756 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700757 int ret;
Fabian Frederick69814982015-02-04 18:26:27 +0100758 isBeyondEOF = true;
Jan Kara31170b62007-05-08 00:35:21 -0700759 if (count) {
760 if (c)
761 laarr[0] = laarr[1];
762 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700763 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700764 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800765 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200766 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700767 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200768 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800769 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700770 startnum = (offset > 0);
771 }
772 /* Create extents for the hole between EOF and offset */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200773 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
774 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700775 brelse(prev_epos.bh);
776 brelse(cur_epos.bh);
777 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200778 *err = ret;
Jan Kara7b0b0932011-12-10 01:43:33 +0100779 return 0;
Jan Kara31170b62007-05-08 00:35:21 -0700780 }
781 c = 0;
782 offset = 0;
783 count += ret;
784 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800785 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
786 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700787 /* Is there any real extent? - otherwise we overwrite
788 * the fake one... */
789 if (count)
790 c = !c;
791 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700792 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800793 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200794 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700795 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700796 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700797 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700799 } else {
Fabian Frederick69814982015-02-04 18:26:27 +0100800 isBeyondEOF = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 endnum = startnum = ((count > 2) ? 2 : count);
802
Marcin Slusarz4b111112008-02-08 04:20:36 -0800803 /* if the current extent is in position 0,
804 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700805 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700806 laarr[2] = laarr[0];
807 laarr[0] = laarr[1];
808 laarr[1] = laarr[2];
809 c = 1;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Marcin Slusarz4b111112008-02-08 04:20:36 -0800812 /* if the current block is located in an extent,
813 read the next extent */
814 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
815 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700816 laarr[c + 1].extLength = (etype << 30) | elen;
817 laarr[c + 1].extLocation = eloc;
818 count++;
819 startnum++;
820 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800821 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 lastblock = 1;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700826 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800827 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800829 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800830 if (iinfo->i_next_alloc_block == block)
831 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700833 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800834 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800835 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
Marcin Slusarz4b111112008-02-08 04:20:36 -0800838 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800839 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800840 goal, err);
841 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700842 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900843 brelse(cur_epos.bh);
844 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 *err = -ENOSPC;
Jan Kara7b0b0932011-12-10 01:43:33 +0100846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900848 if (isBeyondEOF)
849 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
Marcin Slusarz4b111112008-02-08 04:20:36 -0800852 /* if the extent the requsted block is located in contains multiple
853 * blocks, split the extent into at most three extents. blocks prior
854 * to requested block, requested block, and blocks after requested
855 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
857
Jan Kara81056dd2009-07-16 18:02:25 +0200858 /* We preallocate blocks only for regular files. It also makes sense
859 * for directories but there's a problem when to drop the
860 * preallocation. We might use some delayed work for that but I feel
861 * it's overengineering for a filesystem like UDF. */
862 if (S_ISREG(inode->i_mode))
863 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* merge any continuous blocks in laarr */
866 udf_merge_extents(inode, laarr, &endnum);
867
868 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700869 * of extents is greater than the old number, and deleting extents if
870 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700871 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Jan Kara3bf25cb2007-05-08 00:35:16 -0700873 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900874 brelse(cur_epos.bh);
875 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Marcin Slusarz4b111112008-02-08 04:20:36 -0800877 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800878 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100879 if (!newblock) {
880 *err = -EIO;
881 return 0;
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800884 iinfo->i_next_alloc_block = block;
885 iinfo->i_next_alloc_goal = newblocknum;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700886 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (IS_SYNC(inode))
889 udf_sync_inode(inode);
890 else
891 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700892
Jan Kara7b0b0932011-12-10 01:43:33 +0100893 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700896static void udf_split_extents(struct inode *inode, int *c, int offset,
Fabian Frederick3cc6f842017-01-06 21:53:50 +0100897 int newblocknum, struct kernel_long_ad *laarr,
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,
Fabian Frederick3cc6f842017-01-06 21:53:50 +0100961 struct kernel_long_ad *laarr,
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
Fabian Frederick3cc6f842017-01-06 21:53:50 +01001056static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001057 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001060 unsigned long blocksize = inode->i_sb->s_blocksize;
1061 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001063 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001064 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1065 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Marcin Slusarz4b111112008-02-08 04:20:36 -08001067 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1068 (((li->extLength >> 30) ==
1069 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1070 ((lip1->extLocation.logicalBlockNum -
1071 li->extLocation.logicalBlockNum) ==
1072 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1073 blocksize - 1) >> blocksize_bits)))) {
1074
1075 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1076 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1077 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1078 lip1->extLength = (lip1->extLength -
1079 (li->extLength &
1080 UDF_EXTENT_LENGTH_MASK) +
1081 UDF_EXTENT_LENGTH_MASK) &
1082 ~(blocksize - 1);
1083 li->extLength = (li->extLength &
1084 UDF_EXTENT_FLAG_MASK) +
1085 (UDF_EXTENT_LENGTH_MASK + 1) -
1086 blocksize;
1087 lip1->extLocation.logicalBlockNum =
1088 li->extLocation.logicalBlockNum +
1089 ((li->extLength &
1090 UDF_EXTENT_LENGTH_MASK) >>
1091 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001092 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001093 li->extLength = lip1->extLength +
1094 (((li->extLength &
1095 UDF_EXTENT_LENGTH_MASK) +
1096 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001097 if (*endnum > (i + 2))
1098 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001099 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001100 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001101 i--;
1102 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001104 } else if (((li->extLength >> 30) ==
1105 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1106 ((lip1->extLength >> 30) ==
1107 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001108 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001109 ((li->extLength &
1110 UDF_EXTENT_LENGTH_MASK) +
1111 blocksize - 1) >> blocksize_bits);
1112 li->extLocation.logicalBlockNum = 0;
1113 li->extLocation.partitionReferenceNum = 0;
1114
1115 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1116 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1117 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1118 lip1->extLength = (lip1->extLength -
1119 (li->extLength &
1120 UDF_EXTENT_LENGTH_MASK) +
1121 UDF_EXTENT_LENGTH_MASK) &
1122 ~(blocksize - 1);
1123 li->extLength = (li->extLength &
1124 UDF_EXTENT_FLAG_MASK) +
1125 (UDF_EXTENT_LENGTH_MASK + 1) -
1126 blocksize;
1127 } else {
1128 li->extLength = lip1->extLength +
1129 (((li->extLength &
1130 UDF_EXTENT_LENGTH_MASK) +
1131 blocksize - 1) & ~(blocksize - 1));
1132 if (*endnum > (i + 2))
1133 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001134 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001135 (*endnum - (i + 2)));
1136 i--;
1137 (*endnum)--;
1138 }
1139 } else if ((li->extLength >> 30) ==
1140 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1141 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001142 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001143 ((li->extLength &
1144 UDF_EXTENT_LENGTH_MASK) +
1145 blocksize - 1) >> blocksize_bits);
1146 li->extLocation.logicalBlockNum = 0;
1147 li->extLocation.partitionReferenceNum = 0;
1148 li->extLength = (li->extLength &
1149 UDF_EXTENT_LENGTH_MASK) |
1150 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 }
1153}
1154
Fabian Frederick3cc6f842017-01-06 21:53:50 +01001155static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001156 int startnum, int endnum,
1157 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001160 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 uint32_t tmplen;
1162
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001163 if (startnum > endnum) {
1164 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001165 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001166 laarr[i].extLength);
1167 } else if (startnum < endnum) {
1168 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001169 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001170 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001171 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001172 &laarr[i].extLength, 1);
1173 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 }
1176
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001177 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001178 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001179 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001180 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182}
1183
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001184struct buffer_head *udf_bread(struct inode *inode, int block,
1185 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001187 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 bh = udf_getblk(inode, block, create, err);
1190 if (!bh)
1191 return NULL;
1192
1193 if (buffer_uptodate(bh))
1194 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001195
Mike Christiedfec8a12016-06-05 14:31:44 -05001196 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 wait_on_buffer(bh);
1199 if (buffer_uptodate(bh))
1200 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 brelse(bh);
1203 *err = -EIO;
1204 return NULL;
1205}
1206
Jan Kara7e49b6f2010-10-22 00:30:26 +02001207int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001210 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +02001211 int bsize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001214 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001217 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001219 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001220 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001221 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001222 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1223 if (bsize <
1224 (udf_file_entry_alloc_offset(inode) + newsize)) {
1225 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001226 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001227 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001228 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001229 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001230 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001231 goto set_size;
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001234 err = udf_extend_file(inode, newsize);
1235 if (err) {
1236 up_write(&iinfo->i_data_sem);
1237 return err;
1238 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001239set_size:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001240 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001241 up_write(&iinfo->i_data_sem);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001242 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001243 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1244 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001245 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001246 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1247 0x00, bsize - newsize -
1248 udf_file_entry_alloc_offset(inode));
1249 iinfo->i_lenAlloc = newsize;
1250 truncate_setsize(inode, newsize);
1251 up_write(&iinfo->i_data_sem);
1252 goto update_time;
1253 }
1254 err = block_truncate_page(inode->i_mapping, newsize,
1255 udf_get_block);
1256 if (err)
1257 return err;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001258 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001259 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001260 truncate_setsize(inode, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001262 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001263 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001264update_time:
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001265 inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001267 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 else
1269 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Jan Karac03aa9f2014-09-04 14:06:55 +02001273/*
1274 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1275 * arbitrary - just that we hopefully don't limit any real use of rewritten
1276 * inode on write-once media but avoid looping for too long on corrupted media.
1277 */
1278#define UDF_MAX_ICB_NESTING 1024
1279
Jan Kara6174c2e2014-10-09 12:52:16 +02001280static int udf_read_inode(struct inode *inode, bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct buffer_head *bh = NULL;
1283 struct fileEntry *fe;
Jan Karabb7720a02014-09-04 13:32:50 +02001284 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001286 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Karabb7720a02014-09-04 13:32:50 +02001287 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001288 struct kernel_lb_addr *iloc = &iinfo->i_location;
Jan Karabb7720a02014-09-04 13:32:50 +02001289 unsigned int link_count;
Jan Karac03aa9f2014-09-04 14:06:55 +02001290 unsigned int indirections = 0;
Jan Kara79144952015-01-07 13:46:16 +01001291 int bs = inode->i_sb->s_blocksize;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001292 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Jan Karac03aa9f2014-09-04 14:06:55 +02001294reread:
Jan Kara6d3d5e82014-09-04 16:15:51 +02001295 if (iloc->logicalBlockNum >=
1296 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1297 udf_debug("block=%d, partition=%d out of range\n",
1298 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1299 return -EIO;
1300 }
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * Set defaults, but the inode is still incomplete!
1304 * Note: get_new_inode() sets the following on a new inode:
1305 * i_sb = sb
1306 * i_no = ino
1307 * i_flags = sb->s_flags
1308 * i_state = 0
1309 * clean_inode(): zero fills and sets
1310 * i_count = 1
1311 * i_nlink = 1
1312 * i_op = NULL;
1313 */
Jan Kara6d3d5e82014-09-04 16:15:51 +02001314 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001315 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001316 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001317 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
1320 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001321 ident != TAG_IDENT_USE) {
Joe Perches78ace702011-10-10 01:08:05 -07001322 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1323 inode->i_ino, ident);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001324 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
1327 fe = (struct fileEntry *)bh->b_data;
Jan Karabb7720a02014-09-04 13:32:50 +02001328 efe = (struct extendedFileEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001330 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001331 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Jan Kara6d3d5e82014-09-04 16:15:51 +02001333 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001334 if (ident == TAG_IDENT_IE && ibh) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001335 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001336 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001337
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001338 ie = (struct indirectEntry *)ibh->b_data;
1339 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001340
Jan Karac03aa9f2014-09-04 14:06:55 +02001341 if (ie->indirectICB.extLength) {
Jan Karac03aa9f2014-09-04 14:06:55 +02001342 brelse(ibh);
1343 memcpy(&iinfo->i_location, &loc,
1344 sizeof(struct kernel_lb_addr));
1345 if (++indirections > UDF_MAX_ICB_NESTING) {
1346 udf_err(inode->i_sb,
1347 "too many ICBs in ICB hierarchy"
1348 " (max %d supported)\n",
1349 UDF_MAX_ICB_NESTING);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001350 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001351 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001352 brelse(bh);
Jan Karac03aa9f2014-09-04 14:06:55 +02001353 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001355 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001356 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001357 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Joe Perches78ace702011-10-10 01:08:05 -07001358 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1359 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001362 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001363 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001364 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001365 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001367 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001368 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001369 iinfo->i_unique = 0;
1370 iinfo->i_lenEAttr = 0;
1371 iinfo->i_lenExtents = 0;
1372 iinfo->i_lenAlloc = 0;
1373 iinfo->i_next_alloc_block = 0;
1374 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001375 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001376 iinfo->i_efe = 1;
1377 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001378 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001379 sizeof(struct extendedFileEntry));
1380 if (ret)
1381 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001382 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001383 bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001384 bs - sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001385 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001386 iinfo->i_efe = 0;
1387 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001388 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001389 if (ret)
1390 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001391 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001392 bh->b_data + sizeof(struct fileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001393 bs - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001394 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001395 iinfo->i_efe = 0;
1396 iinfo->i_use = 1;
1397 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001398 ((struct unallocSpaceEntry *)bh->b_data)->
1399 lengthAllocDescs);
Jan Kara79144952015-01-07 13:46:16 +01001400 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001401 sizeof(struct unallocSpaceEntry));
1402 if (ret)
1403 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001404 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001405 bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara79144952015-01-07 13:46:16 +01001406 bs - sizeof(struct unallocSpaceEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Jan Kara6d3d5e82014-09-04 16:15:51 +02001410 ret = -EIO;
Jan Karac03cad22010-10-20 22:17:28 +02001411 read_lock(&sbi->s_cred_lock);
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001412 i_uid_write(inode, le32_to_cpu(fe->uid));
1413 if (!uid_valid(inode->i_uid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001414 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1415 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001416 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001418 i_gid_write(inode, le32_to_cpu(fe->gid));
1419 if (!gid_valid(inode->i_gid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001420 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1421 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001422 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001424 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001425 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001426 inode->i_mode = sbi->s_fmode;
1427 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001428 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001429 inode->i_mode = sbi->s_dmode;
1430 else
1431 inode->i_mode = udf_convert_permissions(fe);
1432 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001433 read_unlock(&sbi->s_cred_lock);
1434
Miklos Szeredibfe86842011-10-28 14:13:29 +02001435 link_count = le16_to_cpu(fe->fileLinkCount);
Jan Kara4071b912014-09-04 16:19:47 +02001436 if (!link_count) {
Jan Kara6174c2e2014-10-09 12:52:16 +02001437 if (!hidden_inode) {
1438 ret = -ESTALE;
1439 goto out;
1440 }
1441 link_count = 1;
Jan Kara4071b912014-09-04 16:19:47 +02001442 }
Miklos Szeredibfe86842011-10-28 14:13:29 +02001443 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001444
1445 inode->i_size = le64_to_cpu(fe->informationLength);
1446 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001448 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001450 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Marcin Slusarz56774802008-02-10 11:25:31 +01001452 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001453 inode->i_atime = sbi->s_record_time;
1454
Marcin Slusarz56774802008-02-10 11:25:31 +01001455 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1456 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001457 inode->i_mtime = sbi->s_record_time;
1458
Marcin Slusarz56774802008-02-10 11:25:31 +01001459 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001460 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001462 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1463 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1464 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001465 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001466 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001467 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001468 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Marcin Slusarz56774802008-02-10 11:25:31 +01001470 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001471 inode->i_atime = sbi->s_record_time;
1472
Marcin Slusarz56774802008-02-10 11:25:31 +01001473 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1474 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001475 inode->i_mtime = sbi->s_record_time;
1476
Marcin Slusarz56774802008-02-10 11:25:31 +01001477 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001478 iinfo->i_crtime = sbi->s_record_time;
1479
Marcin Slusarz56774802008-02-10 11:25:31 +01001480 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001481 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001483 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1484 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1485 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001486 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Jan Kara470cca52014-09-04 16:26:19 +02001488 inode->i_generation = iinfo->i_unique;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Jan Kara23b133bd2015-01-07 13:49:08 +01001490 /*
1491 * Sanity check length of allocation descriptors and extended attrs to
1492 * avoid integer overflows
1493 */
1494 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1495 goto out;
1496 /* Now do exact checks */
1497 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1498 goto out;
Jan Karae1593322014-12-19 12:03:53 +01001499 /* Sanity checks for files in ICB so that we don't get confused later */
1500 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1501 /*
1502 * For file in ICB data is stored in allocation descriptor
1503 * so sizes should match
1504 */
1505 if (iinfo->i_lenAlloc != inode->i_size)
1506 goto out;
1507 /* File in ICB has to fit in there... */
Jan Kara79144952015-01-07 13:46:16 +01001508 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
Jan Karae1593322014-12-19 12:03:53 +01001509 goto out;
1510 }
1511
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001512 switch (fe->icbTag.fileType) {
1513 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001514 inode->i_op = &udf_dir_inode_operations;
1515 inode->i_fop = &udf_dir_operations;
1516 inode->i_mode |= S_IFDIR;
1517 inc_nlink(inode);
1518 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001519 case ICBTAG_FILE_TYPE_REALTIME:
1520 case ICBTAG_FILE_TYPE_REGULAR:
1521 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001522 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001523 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001524 inode->i_data.a_ops = &udf_adinicb_aops;
1525 else
1526 inode->i_data.a_ops = &udf_aops;
1527 inode->i_op = &udf_file_inode_operations;
1528 inode->i_fop = &udf_file_operations;
1529 inode->i_mode |= S_IFREG;
1530 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001531 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001532 inode->i_mode |= S_IFBLK;
1533 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001534 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001535 inode->i_mode |= S_IFCHR;
1536 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001537 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001538 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1539 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001540 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001541 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1542 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001543 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001544 inode->i_data.a_ops = &udf_symlink_aops;
Jan Karaad4d0532017-01-02 14:30:31 +01001545 inode->i_op = &udf_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05001546 inode_nohighmem(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001547 inode->i_mode = S_IFLNK | S_IRWXUGO;
1548 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001549 case ICBTAG_FILE_TYPE_MAIN:
1550 udf_debug("METADATA FILE-----\n");
1551 break;
1552 case ICBTAG_FILE_TYPE_MIRROR:
1553 udf_debug("METADATA MIRROR FILE-----\n");
1554 break;
1555 case ICBTAG_FILE_TYPE_BITMAP:
1556 udf_debug("METADATA BITMAP FILE-----\n");
1557 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001558 default:
Joe Perches78ace702011-10-10 01:08:05 -07001559 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1560 inode->i_ino, fe->icbTag.fileType);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001561 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001563 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001564 struct deviceSpec *dsea =
1565 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001566 if (dsea) {
1567 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001568 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1569 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001571 } else
Jan Kara6d3d5e82014-09-04 16:15:51 +02001572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001574 ret = 0;
1575out:
Jan Karabb7720a02014-09-04 13:32:50 +02001576 brelse(bh);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001580static int udf_alloc_i_data(struct inode *inode, size_t size)
1581{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001582 struct udf_inode_info *iinfo = UDF_I(inode);
1583 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001584
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001585 if (!iinfo->i_ext.i_data) {
Joe Perches78ace702011-10-10 01:08:05 -07001586 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1587 inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001588 return -ENOMEM;
1589 }
1590
1591 return 0;
1592}
1593
Al Virofaa17292011-07-26 03:18:29 -04001594static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Al Virofaa17292011-07-26 03:18:29 -04001596 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 uint32_t permissions;
1598 uint32_t flags;
1599
1600 permissions = le32_to_cpu(fe->permissions);
1601 flags = le16_to_cpu(fe->icbTag.flags);
1602
Marcin Slusarz4b111112008-02-08 04:20:36 -08001603 mode = ((permissions) & S_IRWXO) |
1604 ((permissions >> 2) & S_IRWXG) |
1605 ((permissions >> 4) & S_IRWXU) |
1606 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1607 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1608 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 return mode;
1611}
1612
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001613int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Jan Kara49521de2010-10-20 17:42:44 +02001615 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Jan Kara49521de2010-10-20 17:42:44 +02001618static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 return udf_update_inode(inode, 1);
1621}
1622
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001623static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 struct buffer_head *bh = NULL;
1626 struct fileEntry *fe;
1627 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001628 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 uint32_t udfperms;
1630 uint16_t icbflags;
1631 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001633 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001634 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001635 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Jan Kara5833ded2010-01-08 16:52:59 +01001637 bh = udf_tgetblk(inode->i_sb,
1638 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001639 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001640 udf_debug("getblk failure\n");
Changwoo Min0fd2ba32015-03-22 19:17:49 -04001641 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643
Jan Karaaae917c2010-01-08 16:46:29 +01001644 lock_buffer(bh);
1645 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 fe = (struct fileEntry *)bh->b_data;
1647 efe = (struct extendedFileEntry *)bh->b_data;
1648
Jan Karaaae917c2010-01-08 16:46:29 +01001649 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001651 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001653 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001654 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001655 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001656 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001657 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001658 crclen = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001660 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
Phillip Susi4d6660e2006-03-07 21:55:24 -08001663 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1664 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001665 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001666 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Phillip Susi4d6660e2006-03-07 21:55:24 -08001668 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1669 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001670 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001671 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Marcin Slusarz4b111112008-02-08 04:20:36 -08001673 udfperms = ((inode->i_mode & S_IRWXO)) |
1674 ((inode->i_mode & S_IRWXG) << 2) |
1675 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Marcin Slusarz4b111112008-02-08 04:20:36 -08001677 udfperms |= (le32_to_cpu(fe->permissions) &
1678 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1679 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1680 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 fe->permissions = cpu_to_le32(udfperms);
1682
Jan Kara8a70ee32014-09-04 11:47:51 +02001683 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1685 else
1686 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1687
1688 fe->informationLength = cpu_to_le64(inode->i_size);
1689
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001690 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001691 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001692 struct deviceSpec *dsea =
1693 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001694 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001696 udf_add_extendedattr(inode,
1697 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001698 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 dsea->attrType = cpu_to_le32(12);
1700 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001701 dsea->attrLength = cpu_to_le32(
1702 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001703 sizeof(struct regid));
1704 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001706 eid = (struct regid *)dsea->impUse;
1707 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 strcpy(eid->ident, UDF_ID_DEVELOPER);
1709 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1710 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1711 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1712 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1713 }
1714
Steve Nickelb2527bf2012-02-16 12:53:53 -05001715 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1716 lb_recorded = 0; /* No extents => no blocks! */
1717 else
1718 lb_recorded =
1719 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1720 (blocksize_bits - 9);
1721
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001722 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001723 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001724 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001725 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001726 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Marcin Slusarz56774802008-02-10 11:25:31 +01001728 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1729 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1730 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001731 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1733 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1734 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001735 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1736 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1737 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001738 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1740 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001741 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001742 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001743 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001744 inode->i_sb->s_blocksize -
1745 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001747 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001749 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1750 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1751 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1752 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001753
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001754 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1755 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1756 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1757 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001758
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001759 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1760 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1761 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1762 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Marcin Slusarz56774802008-02-10 11:25:31 +01001764 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1765 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1766 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1767 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001769 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1771 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1772 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001773 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1774 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1775 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001776 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1778 crclen = sizeof(struct extendedFileEntry);
1779 }
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001780
1781finish:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001782 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 fe->icbTag.strategyType = cpu_to_le16(4096);
1784 fe->icbTag.strategyParameter = cpu_to_le16(1);
1785 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001786 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 fe->icbTag.strategyType = cpu_to_le16(4);
1788 fe->icbTag.numEntries = cpu_to_le16(1);
1789 }
1790
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001791 if (iinfo->i_use)
1792 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1793 else if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1795 else if (S_ISREG(inode->i_mode))
1796 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1797 else if (S_ISLNK(inode->i_mode))
1798 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1799 else if (S_ISBLK(inode->i_mode))
1800 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1801 else if (S_ISCHR(inode->i_mode))
1802 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1803 else if (S_ISFIFO(inode->i_mode))
1804 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1805 else if (S_ISSOCK(inode->i_mode))
1806 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1807
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001808 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001809 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1810 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1811 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1812 (le16_to_cpu(fe->icbTag.flags) &
1813 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1814 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001817 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 fe->descTag.descVersion = cpu_to_le16(3);
1819 else
1820 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001821 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001822 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001823 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001824 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001826 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001827 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001828 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Jan Kara5833ded2010-01-08 16:52:59 +01001830 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001831 unlock_buffer(bh);
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* write the data blocks */
1834 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001835 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001837 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001838 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1839 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 err = -EIO;
1841 }
1842 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001843 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return err;
1846}
1847
Jan Kara6174c2e2014-10-09 12:52:16 +02001848struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1849 bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1852 struct inode *inode = iget_locked(sb, block);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001853 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 if (!inode)
Jan Kara6d3d5e82014-09-04 16:15:51 +02001856 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Jan Kara6d3d5e82014-09-04 16:15:51 +02001858 if (!(inode->i_state & I_NEW))
1859 return inode;
1860
1861 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Jan Kara6174c2e2014-10-09 12:52:16 +02001862 err = udf_read_inode(inode, hidden_inode);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001863 if (err < 0) {
1864 iget_failed(inode);
1865 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001867 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Jan Karafcea62b2015-12-23 14:21:13 +01001872int udf_setup_indirect_aext(struct inode *inode, int block,
1873 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Jan Karafcea62b2015-12-23 14:21:13 +01001875 struct super_block *sb = inode->i_sb;
1876 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 struct allocExtDesc *aed;
Jan Karafcea62b2015-12-23 14:21:13 +01001878 struct extent_position nepos;
1879 struct kernel_lb_addr neloc;
1880 int ver, adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Jan Karafcea62b2015-12-23 14:21:13 +01001882 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1883 adsize = sizeof(struct short_ad);
1884 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1885 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 else
Arnd Bergmann4f1b1512016-01-01 15:21:54 +01001887 return -EIO;
Jan Karafcea62b2015-12-23 14:21:13 +01001888
1889 neloc.logicalBlockNum = block;
1890 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1891
1892 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1893 if (!bh)
1894 return -EIO;
1895 lock_buffer(bh);
1896 memset(bh->b_data, 0x00, sb->s_blocksize);
1897 set_buffer_uptodate(bh);
1898 unlock_buffer(bh);
1899 mark_buffer_dirty_inode(bh, inode);
1900
1901 aed = (struct allocExtDesc *)(bh->b_data);
1902 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1903 aed->previousAllocExtLocation =
1904 cpu_to_le32(epos->block.logicalBlockNum);
1905 }
1906 aed->lengthAllocDescs = cpu_to_le32(0);
1907 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1908 ver = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 else
Jan Karafcea62b2015-12-23 14:21:13 +01001910 ver = 2;
1911 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1912 sizeof(struct tag));
1913
1914 nepos.block = neloc;
1915 nepos.offset = sizeof(struct allocExtDesc);
1916 nepos.bh = bh;
1917
1918 /*
1919 * Do we have to copy current last extent to make space for indirect
1920 * one?
1921 */
1922 if (epos->offset + adsize > sb->s_blocksize) {
1923 struct kernel_lb_addr cp_loc;
1924 uint32_t cp_len;
1925 int cp_type;
1926
1927 epos->offset -= adsize;
1928 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1929 cp_len |= ((uint32_t)cp_type) << 30;
1930
1931 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1932 udf_write_aext(inode, epos, &nepos.block,
1933 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1934 } else {
1935 __udf_add_aext(inode, epos, &nepos.block,
1936 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1937 }
1938
1939 brelse(epos->bh);
1940 *epos = nepos;
1941
1942 return 0;
1943}
1944
1945/*
1946 * Append extent at the given position - should be the first free one in inode
1947 * / indirect extent. This function assumes there is enough space in the inode
1948 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1949 */
1950int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1951 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1952{
1953 struct udf_inode_info *iinfo = UDF_I(inode);
1954 struct allocExtDesc *aed;
1955 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001957 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001958 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001959 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001960 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001962 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Jan Karafcea62b2015-12-23 14:21:13 +01001964 if (!epos->bh) {
1965 WARN_ON(iinfo->i_lenAlloc !=
1966 epos->offset - udf_file_entry_alloc_offset(inode));
1967 } else {
1968 aed = (struct allocExtDesc *)epos->bh->b_data;
1969 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1970 epos->offset - sizeof(struct allocExtDesc));
1971 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
Jan Kara7e49b6f2010-10-22 00:30:26 +02001974 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001976 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001977 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001979 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001980 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001981 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001982 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1983 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1984 udf_update_tag(epos->bh->b_data,
1985 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001987 udf_update_tag(epos->bh->b_data,
1988 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001989 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991
Jan Kara7e49b6f2010-10-22 00:30:26 +02001992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Jan Karafcea62b2015-12-23 14:21:13 +01001995/*
1996 * Append extent at given position - should be the first free one in inode
1997 * / indirect extent. Takes care of allocating and linking indirect blocks.
1998 */
1999int udf_add_aext(struct inode *inode, struct extent_position *epos,
2000 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2001{
2002 int adsize;
2003 struct super_block *sb = inode->i_sb;
2004
2005 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2006 adsize = sizeof(struct short_ad);
2007 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2008 adsize = sizeof(struct long_ad);
2009 else
2010 return -EIO;
2011
2012 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2013 int err;
2014 int new_block;
2015
2016 new_block = udf_new_block(sb, NULL,
2017 epos->block.partitionReferenceNum,
2018 epos->block.logicalBlockNum, &err);
2019 if (!new_block)
2020 return -ENOSPC;
2021
2022 err = udf_setup_indirect_aext(inode, new_block, epos);
2023 if (err)
2024 return err;
2025 }
2026
2027 return __udf_add_aext(inode, epos, eloc, elen, inc);
2028}
2029
Jan Kara7e49b6f2010-10-22 00:30:26 +02002030void udf_write_aext(struct inode *inode, struct extent_position *epos,
2031 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 int adsize;
2034 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002035 struct short_ad *sad;
2036 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002037 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Jan Karaff116fc2007-05-08 00:35:14 -07002039 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002040 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002041 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002042 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 else
Jan Karaff116fc2007-05-08 00:35:14 -07002044 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002046 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002047 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002048 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002049 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002050 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002051 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002052 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002053 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002054 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002055 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002056 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002057 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002058 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002059 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002060 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002061 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002064 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002065 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002066 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002067 struct allocExtDesc *aed =
2068 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002069 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002070 le32_to_cpu(aed->lengthAllocDescs) +
2071 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
Jan Karaff116fc2007-05-08 00:35:14 -07002073 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002074 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002079 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Vegard Nossumb0918d92015-12-11 15:54:16 +01002082/*
2083 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2084 * someone does some weird stuff.
2085 */
2086#define UDF_MAX_INDIR_EXTS 16
2087
Marcin Slusarz4b111112008-02-08 04:20:36 -08002088int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002089 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 int8_t etype;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002092 unsigned int indirections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Jan Karaff116fc2007-05-08 00:35:14 -07002094 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002095 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002096 int block;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002097
2098 if (++indirections > UDF_MAX_INDIR_EXTS) {
2099 udf_err(inode->i_sb,
2100 "too many indirect extents in inode %lu\n",
2101 inode->i_ino);
2102 return -1;
2103 }
2104
Jan Karaff116fc2007-05-08 00:35:14 -07002105 epos->block = *eloc;
2106 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002107 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002108 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002109 epos->bh = udf_tread(inode->i_sb, block);
2110 if (!epos->bh) {
2111 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return -1;
2113 }
2114 }
2115
2116 return etype;
2117}
2118
Marcin Slusarz4b111112008-02-08 04:20:36 -08002119int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002120 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 int alen;
2123 int8_t etype;
2124 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002125 struct short_ad *sad;
2126 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002127 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002128
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002129 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002130 if (!epos->offset)
2131 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002132 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002133 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002134 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002135 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002136 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002137 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002138 if (!epos->offset)
2139 epos->offset = sizeof(struct allocExtDesc);
2140 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002141 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002142 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2143 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002146 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002147 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002148 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2149 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002151 etype = le32_to_cpu(sad->extLength) >> 30;
2152 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002153 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002154 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002155 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2156 break;
2157 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002158 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2159 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002160 return -1;
2161 etype = le32_to_cpu(lad->extLength) >> 30;
2162 *eloc = lelb_to_cpu(lad->extLocation);
2163 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2164 break;
2165 default:
Joe Perchesa983f362011-10-10 01:08:07 -07002166 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002167 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169
2170 return etype;
2171}
2172
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002173static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002174 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002176 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 uint32_t oelen;
2178 int8_t etype;
2179
Jan Karaff116fc2007-05-08 00:35:14 -07002180 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002181 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002183 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002184 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 neloc = oeloc;
2186 nelen = (etype << 30) | oelen;
2187 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002188 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002189 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return (nelen >> 30);
2192}
2193
Marcin Slusarz4b111112008-02-08 04:20:36 -08002194int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002195 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Jan Karaff116fc2007-05-08 00:35:14 -07002197 struct extent_position oepos;
2198 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 int8_t etype;
2200 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002201 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002203 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002204 get_bh(epos.bh);
2205 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002208 iinfo = UDF_I(inode);
2209 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002210 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002211 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002212 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 else
2214 adsize = 0;
2215
Jan Karaff116fc2007-05-08 00:35:14 -07002216 oepos = epos;
2217 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return -1;
2219
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002220 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002221 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002222 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002223 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002224 brelse(oepos.bh);
2225 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002226 oepos.bh = epos.bh;
2227 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002230 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 elen = 0;
2232
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002233 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002234 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2235 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2236 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002237 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002238 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002240 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002241 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002242 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002243 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002244 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002245 udf_update_tag(oepos.bh->b_data,
2246 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002248 udf_update_tag(oepos.bh->b_data,
2249 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002250 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002252 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002253 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002254 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002255 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002257 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002258 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002259 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002260 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002261 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002262 udf_update_tag(oepos.bh->b_data,
2263 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002265 udf_update_tag(oepos.bh->b_data,
2266 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002267 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002270
Jan Kara3bf25cb2007-05-08 00:35:16 -07002271 brelse(epos.bh);
2272 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return (elen >> 30);
2275}
2276
Marcin Slusarz4b111112008-02-08 04:20:36 -08002277int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002278 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002279 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002281 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002282 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002283 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002285 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002287 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002288 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2289 pos->offset = 0;
2290 pos->block = iinfo->i_location;
2291 pos->bh = NULL;
2292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002294 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002295 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2296 if (etype == -1) {
2297 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002298 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return -1;
2300 }
2301 lbcount += *elen;
2302 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002303 /* update extent cache */
2304 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002305 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 return etype;
2308}
2309
Jan Kara60448b12007-05-08 00:35:13 -07002310long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002312 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002313 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002314 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002315 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 int ret;
2317
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002318 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Marcin Slusarz4b111112008-02-08 04:20:36 -08002320 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2321 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002322 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 else
2324 ret = 0;
2325
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002326 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002327 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2330 return udf_fixed_to_variable(ret);
2331 else
2332 return ret;
2333}