blob: 1a0588e77a2f77054a1dba91574f92a5286442bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
16 *
17 * HISTORY
18 *
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
Marcin Slusarz4b111112008-02-08 04:20:36 -080022 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
30 */
31
32#include "udfdecl.h"
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/pagemap.h>
36#include <linux/buffer_head.h>
37#include <linux/writeback.h>
38#include <linux/slab.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020039#include <linux/crc-itu-t.h>
Namjae Jeonbc112322011-10-03 14:02:59 +090040#include <linux/mpage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "udf_i.h"
43#include "udf_sb.h"
44
45MODULE_AUTHOR("Ben Fennema");
46MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47MODULE_LICENSE("GPL");
48
49#define EXTENT_MERGE_SIZE 5
50
Al Virofaa17292011-07-26 03:18:29 -040051static umode_t udf_convert_permissions(struct fileEntry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int udf_update_inode(struct inode *, int);
53static void udf_fill_inode(struct inode *, struct buffer_head *);
Jan Kara49521de2010-10-20 17:42:44 +020054static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070055static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara7b0b0932011-12-10 01:43:33 +010056static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070057static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020058 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020060 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020062 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020064 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020066 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070067 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
69
Christoph Hellwigb1e32122008-02-22 12:38:48 +010070
Al Viro3aac2b62010-06-07 00:43:39 -040071void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Jan Kara2c948b32009-12-03 13:39:28 +010073 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b62010-06-07 00:43:39 -040074 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +010075
Al Viro3aac2b62010-06-07 00:43:39 -040076 if (!inode->i_nlink && !is_bad_inode(inode)) {
77 want_delete = 1;
Jan Kara7e49b6f2010-10-22 00:30:26 +020078 udf_setsize(inode, 0);
Al Viro3aac2b62010-06-07 00:43:39 -040079 udf_update_inode(inode, IS_SYNC(inode));
Jan Kara7e49b6f2010-10-22 00:30:26 +020080 } else
81 truncate_inode_pages(&inode->i_data, 0);
Al Viro3aac2b62010-06-07 00:43:39 -040082 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +020083 clear_inode(inode);
Jan Kara2c948b32009-12-03 13:39:28 +010084 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
85 inode->i_size != iinfo->i_lenExtents) {
Joe Perches78ace702011-10-10 01:08:05 -070086 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",
87 inode->i_ino, inode->i_mode,
88 (unsigned long long)inode->i_size,
89 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080091 kfree(iinfo->i_ext.i_data);
92 iinfo->i_ext.i_data = NULL;
Al Viro3aac2b62010-06-07 00:43:39 -040093 if (want_delete) {
Al Viro3aac2b62010-06-07 00:43:39 -040094 udf_free_inode(inode);
Al Viro3aac2b62010-06-07 00:43:39 -040095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98static int udf_writepage(struct page *page, struct writeback_control *wbc)
99{
100 return block_write_full_page(page, udf_get_block, wbc);
101}
102
Namjae Jeon378b8e12012-08-31 12:49:07 -0400103static int udf_writepages(struct address_space *mapping,
104 struct writeback_control *wbc)
105{
106 return mpage_writepages(mapping, wbc, udf_get_block);
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int udf_readpage(struct file *file, struct page *page)
110{
Namjae Jeonbc112322011-10-03 14:02:59 +0900111 return mpage_readpage(page, udf_get_block);
112}
113
114static int udf_readpages(struct file *file, struct address_space *mapping,
115 struct list_head *pages, unsigned nr_pages)
116{
117 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Nick Pigginbe021ee2007-10-16 01:25:20 -0700120static int udf_write_begin(struct file *file, struct address_space *mapping,
121 loff_t pos, unsigned len, unsigned flags,
122 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200124 int ret;
125
126 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
127 if (unlikely(ret)) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200128 struct inode *inode = mapping->host;
129 struct udf_inode_info *iinfo = UDF_I(inode);
130 loff_t isize = inode->i_size;
131
132 if (pos + len > isize) {
133 truncate_pagecache(inode, pos + len, isize);
134 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
135 down_write(&iinfo->i_data_sem);
136 udf_truncate_extents(inode);
137 up_write(&iinfo->i_data_sem);
138 }
139 }
Christoph Hellwig155130a2010-06-04 11:29:58 +0200140 }
141
142 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static sector_t udf_bmap(struct address_space *mapping, sector_t block)
146{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700147 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700150const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700151 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900152 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700153 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400154 .writepages = udf_writepages,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700155 .write_begin = udf_write_begin,
156 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700157 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Jan Karad2eb8c32011-12-10 02:30:48 +0100160/*
161 * Expand file stored in ICB to a normal one-block-file
162 *
163 * This function requires i_data_sem for writing and releases it.
164 * This function requires i_mutex held
165 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200166int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct page *page;
169 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800170 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200171 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct writeback_control udf_wbc = {
173 .sync_mode = WB_SYNC_NONE,
174 .nr_to_write = 1,
175 };
176
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800177 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800179 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800181 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200182 /* from now on we have normal address_space methods */
183 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100184 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100188 /*
189 * Release i_data_sem so that we can lock a page - page lock ranks
190 * above i_data_sem. i_mutex still protects us against file changes.
191 */
192 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Jan Kara7e49b6f2010-10-22 00:30:26 +0200194 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
195 if (!page)
196 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700197
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700198 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800200 memset(kaddr + iinfo->i_lenAlloc, 0x00,
201 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
202 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
203 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 flush_dcache_page(page);
205 SetPageUptodate(page);
206 kunmap(page);
207 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100208 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800209 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
210 iinfo->i_lenAlloc);
211 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800213 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800215 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200216 /* from now on we have normal address_space methods */
217 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100218 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200219 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
220 if (err) {
221 /* Restore everything back so that we don't lose data... */
222 lock_page(page);
223 kaddr = kmap(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100224 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200225 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
226 inode->i_size);
227 kunmap(page);
228 unlock_page(page);
229 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
230 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100231 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200235
236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700239struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
240 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700243 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200244 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700246 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800249 loff_t f_pos = udf_ext0_offset(inode);
250 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800252 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
255 alloctype = ICBTAG_FLAG_AD_SHORT;
256 else
257 alloctype = ICBTAG_FLAG_AD_LONG;
258
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700259 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800260 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 mark_inode_dirty(inode);
262 return NULL;
263 }
264
265 /* alloc block, and copy data to it */
266 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800267 iinfo->i_location.partitionReferenceNum,
268 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!(*block))
270 return NULL;
271 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800272 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800273 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (!newblock)
275 return NULL;
276 dbh = udf_tgetblk(inode->i_sb, newblock);
277 if (!dbh)
278 return NULL;
279 lock_buffer(dbh);
280 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
281 set_buffer_uptodate(dbh);
282 unlock_buffer(dbh);
283 mark_buffer_dirty_inode(dbh, inode);
284
Marcin Slusarz4b111112008-02-08 04:20:36 -0800285 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800286 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700287 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 dfibh.soffset = dfibh.eoffset = 0;
289 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800290 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800291 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800292 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
293 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700295 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return NULL;
297 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800298 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 sfi->descTag.tagLocation = cpu_to_le32(*block);
300 dfibh.soffset = dfibh.eoffset;
301 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
302 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
303 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800304 sfi->fileIdent +
305 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800306 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700307 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return NULL;
309 }
310 }
311 mark_buffer_dirty_inode(dbh, inode);
312
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800313 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
314 iinfo->i_lenAlloc);
315 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800317 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800318 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100319 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700320 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800321 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700322 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100323 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* UniqueID stuff */
325
Jan Kara3bf25cb2007-05-08 00:35:16 -0700326 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 mark_inode_dirty(inode);
328 return dbh;
329}
330
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700331static int udf_get_block(struct inode *inode, sector_t block,
332 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800335 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800336 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700338 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 phys = udf_block_map(inode, block);
340 if (phys)
341 map_bh(bh_result, inode->i_sb, phys);
342 return 0;
343 }
344
345 err = -EIO;
346 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800347 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100348
349 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800350 if (block == iinfo->i_next_alloc_block + 1) {
351 iinfo->i_next_alloc_block++;
352 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Jan Kara7b0b0932011-12-10 01:43:33 +0100356 phys = inode_getblk(inode, block, &err, &new);
357 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (new)
361 set_buffer_new(bh_result);
362 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700363
364abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100365 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700369static struct buffer_head *udf_getblk(struct inode *inode, long block,
370 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700372 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct buffer_head dummy;
374
375 dummy.b_state = 0;
376 dummy.b_blocknr = -1000;
377 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700378 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700380 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 lock_buffer(bh);
382 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
383 set_buffer_uptodate(bh);
384 unlock_buffer(bh);
385 mark_buffer_dirty_inode(bh, inode);
386 }
387 return bh;
388 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return NULL;
391}
392
Jan Kara31170b62007-05-08 00:35:21 -0700393/* Extend the file by 'blocks' blocks, return the number of extents added */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200394static int udf_do_extend_file(struct inode *inode,
395 struct extent_position *last_pos,
396 struct kernel_long_ad *last_ext,
397 sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700398{
399 sector_t add;
400 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
401 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200402 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700403 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800404 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200405 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700406
407 /* The previous extent is fake and we should not extend by anything
408 * - there's nothing to do... */
409 if (!blocks && fake)
410 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700411
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800412 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700413 /* Round the last extent up to a multiple of block size */
414 if (last_ext->extLength & (sb->s_blocksize - 1)) {
415 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700416 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
417 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
418 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800419 iinfo->i_lenExtents =
420 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700421 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700422 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700423
Jan Kara31170b62007-05-08 00:35:21 -0700424 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800425 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
426 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700427 /* Save the extent so that we can reattach it to the end */
428 prealloc_loc = last_ext->extLocation;
429 prealloc_len = last_ext->extLength;
430 /* Mark the extent as a hole */
431 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700432 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700433 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800434 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700435 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436
Jan Kara31170b62007-05-08 00:35:21 -0700437 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800438 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
439 EXT_NOT_RECORDED_NOT_ALLOCATED) {
440 add = ((1 << 30) - sb->s_blocksize -
441 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
442 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700443 if (add > blocks)
444 add = blocks;
445 blocks -= add;
446 last_ext->extLength += add << sb->s_blocksize_bits;
447 }
448
449 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200450 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700451 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700452 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800453 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200454 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800455 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700456
Jan Kara31170b62007-05-08 00:35:21 -0700457 /* Managed to do everything necessary? */
458 if (!blocks)
459 goto out;
460
461 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
462 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800463 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700464 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800465 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
466 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700467
Jan Kara31170b62007-05-08 00:35:21 -0700468 /* Create enough extents to cover the whole hole */
469 while (blocks > add) {
470 blocks -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200471 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
472 last_ext->extLength, 1);
473 if (err)
474 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700475 count++;
476 }
477 if (blocks) {
478 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700479 (blocks << sb->s_blocksize_bits);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200480 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
481 last_ext->extLength, 1);
482 if (err)
483 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700484 count++;
485 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700486
487out:
Jan Kara31170b62007-05-08 00:35:21 -0700488 /* Do we have some preallocated blocks saved? */
489 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200490 err = udf_add_aext(inode, last_pos, &prealloc_loc,
491 prealloc_len, 1);
492 if (err)
493 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700494 last_ext->extLocation = prealloc_loc;
495 last_ext->extLength = prealloc_len;
496 count++;
497 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700498
Jan Kara31170b62007-05-08 00:35:21 -0700499 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800500 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200501 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800502 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200503 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700504 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200505 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700506
Jan Kara31170b62007-05-08 00:35:21 -0700507 return count;
508}
509
Jan Kara7e49b6f2010-10-22 00:30:26 +0200510static int udf_extend_file(struct inode *inode, loff_t newsize)
511{
512
513 struct extent_position epos;
514 struct kernel_lb_addr eloc;
515 uint32_t elen;
516 int8_t etype;
517 struct super_block *sb = inode->i_sb;
518 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
519 int adsize;
520 struct udf_inode_info *iinfo = UDF_I(inode);
521 struct kernel_long_ad extent;
522 int err;
523
524 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
525 adsize = sizeof(struct short_ad);
526 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
527 adsize = sizeof(struct long_ad);
528 else
529 BUG();
530
531 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
532
533 /* File has extent covering the new size (could happen when extending
534 * inside a block)? */
535 if (etype != -1)
536 return 0;
537 if (newsize & (sb->s_blocksize - 1))
538 offset++;
539 /* Extended file just to the boundary of the last file block? */
540 if (offset == 0)
541 return 0;
542
543 /* Truncate is extending the file by 'offset' blocks */
544 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
545 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
546 /* File has no extents at all or has empty last
547 * indirect extent! Create a fake extent... */
548 extent.extLocation.logicalBlockNum = 0;
549 extent.extLocation.partitionReferenceNum = 0;
550 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
551 } else {
552 epos.offset -= adsize;
553 etype = udf_next_aext(inode, &epos, &extent.extLocation,
554 &extent.extLength, 0);
555 extent.extLength |= etype << 30;
556 }
557 err = udf_do_extend_file(inode, &epos, &extent, offset);
558 if (err < 0)
559 goto out;
560 err = 0;
561 iinfo->i_lenExtents = newsize;
562out:
563 brelse(epos.bh);
564 return err;
565}
566
Jan Kara7b0b0932011-12-10 01:43:33 +0100567static sector_t inode_getblk(struct inode *inode, sector_t block,
568 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Jan Kara31170b62007-05-08 00:35:21 -0700570 static sector_t last_block;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200571 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700572 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700574 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200575 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700577 loff_t lbcount = 0, b_off = 0;
578 uint32_t newblocknum, newblock;
579 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800581 struct udf_inode_info *iinfo = UDF_I(inode);
582 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700583 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Jan Kara7b0b0932011-12-10 01:43:33 +0100585 *err = 0;
586 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700587 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800588 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700589 prev_epos.bh = NULL;
590 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700591 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700594 alternate between laarr[0] and laarr[1] for locations of the
595 current extent, and the previous extent */
596 do {
597 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700598 brelse(prev_epos.bh);
599 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700600 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700602 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700603 brelse(cur_epos.bh);
604 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700605 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 lbcount += elen;
609
Jan Karaff116fc2007-05-08 00:35:14 -0700610 prev_epos.block = cur_epos.block;
611 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Jan Karaff116fc2007-05-08 00:35:14 -0700613 prev_epos.offset = cur_epos.offset;
614 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Marcin Slusarz4b111112008-02-08 04:20:36 -0800616 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
617 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
619
620 c = !c;
621
622 laarr[c].extLength = (etype << 30) | elen;
623 laarr[c].extLocation = eloc;
624
625 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
626 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700627 ((elen + inode->i_sb->s_blocksize - 1) >>
628 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700630 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 } while (lbcount + elen <= b_off);
632
633 b_off -= lbcount;
634 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700635 /*
636 * Move prev_epos and cur_epos into indirect extent if we are at
637 * the pointer to it
638 */
639 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
640 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700643 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700645 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
646 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700648 ((elen + inode->i_sb->s_blocksize - 1) &
649 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200650 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700652 brelse(prev_epos.bh);
653 brelse(cur_epos.bh);
654 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200655 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Jan Kara7b0b0932011-12-10 01:43:33 +0100656 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Jan Kara31170b62007-05-08 00:35:21 -0700659 last_block = block;
660 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700661 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700662 int ret;
663
664 if (count) {
665 if (c)
666 laarr[0] = laarr[1];
667 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700668 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700669 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800670 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200671 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700672 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200673 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800674 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700675 startnum = (offset > 0);
676 }
677 /* Create extents for the hole between EOF and offset */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200678 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
679 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700680 brelse(prev_epos.bh);
681 brelse(cur_epos.bh);
682 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200683 *err = ret;
Jan Kara7b0b0932011-12-10 01:43:33 +0100684 return 0;
Jan Kara31170b62007-05-08 00:35:21 -0700685 }
686 c = 0;
687 offset = 0;
688 count += ret;
689 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800690 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
691 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700692 /* Is there any real extent? - otherwise we overwrite
693 * the fake one... */
694 if (count)
695 c = !c;
696 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700697 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800698 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200699 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700700 count++;
701 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700702 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700703 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700705 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 endnum = startnum = ((count > 2) ? 2 : count);
707
Marcin Slusarz4b111112008-02-08 04:20:36 -0800708 /* if the current extent is in position 0,
709 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700710 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700711 laarr[2] = laarr[0];
712 laarr[0] = laarr[1];
713 laarr[1] = laarr[2];
714 c = 1;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Marcin Slusarz4b111112008-02-08 04:20:36 -0800717 /* if the current block is located in an extent,
718 read the next extent */
719 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
720 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700721 laarr[c + 1].extLength = (etype << 30) | elen;
722 laarr[c + 1].extLocation = eloc;
723 count++;
724 startnum++;
725 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800726 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 lastblock = 1;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700731 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800732 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800734 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800735 if (iinfo->i_next_alloc_block == block)
736 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700738 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800739 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800740 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
Marcin Slusarz4b111112008-02-08 04:20:36 -0800743 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800744 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800745 goal, err);
746 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700747 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 *err = -ENOSPC;
Jan Kara7b0b0932011-12-10 01:43:33 +0100749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800751 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Marcin Slusarz4b111112008-02-08 04:20:36 -0800754 /* if the extent the requsted block is located in contains multiple
755 * blocks, split the extent into at most three extents. blocks prior
756 * to requested block, requested block, and blocks after requested
757 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
759
760#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200761 /* We preallocate blocks only for regular files. It also makes sense
762 * for directories but there's a problem when to drop the
763 * preallocation. We might use some delayed work for that but I feel
764 * it's overengineering for a filesystem like UDF. */
765 if (S_ISREG(inode->i_mode))
766 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#endif
768
769 /* merge any continuous blocks in laarr */
770 udf_merge_extents(inode, laarr, &endnum);
771
772 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700773 * of extents is greater than the old number, and deleting extents if
774 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700775 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jan Kara3bf25cb2007-05-08 00:35:16 -0700777 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Marcin Slusarz4b111112008-02-08 04:20:36 -0800779 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800780 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100781 if (!newblock) {
782 *err = -EIO;
783 return 0;
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800786 iinfo->i_next_alloc_block = block;
787 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 inode->i_ctime = current_fs_time(inode->i_sb);
789
790 if (IS_SYNC(inode))
791 udf_sync_inode(inode);
792 else
793 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700794
Jan Kara7b0b0932011-12-10 01:43:33 +0100795 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700798static void udf_split_extents(struct inode *inode, int *c, int offset,
799 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200800 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700801 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800803 unsigned long blocksize = inode->i_sb->s_blocksize;
804 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800807 (laarr[*c].extLength >> 30) ==
808 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int curr = *c;
810 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800811 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 int8_t etype = (laarr[curr].extLength >> 30);
813
Marcin Slusarz4b111112008-02-08 04:20:36 -0800814 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700815 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800816 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700817 laarr[curr + 2] = laarr[curr + 1];
818 laarr[curr + 1] = laarr[curr];
819 } else {
820 laarr[curr + 3] = laarr[curr + 1];
821 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700824 if (offset) {
825 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800826 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200827 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800828 0, offset);
829 laarr[curr].extLength =
830 EXT_NOT_RECORDED_NOT_ALLOCATED |
831 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800833 laarr[curr].extLocation.
834 partitionReferenceNum = 0;
835 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800837 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700838 curr++;
839 (*c)++;
840 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 laarr[curr].extLocation.logicalBlockNum = newblocknum;
844 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
845 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800846 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800848 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700849 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700851 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800853 laarr[curr].extLocation.logicalBlockNum +=
854 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700855 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800856 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700857 curr++;
858 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
861}
862
863static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200864 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700865 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 int start, length = 0, currlength = 0, i;
868
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700869 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!lastblock)
871 return;
872 else
873 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700874 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800875 if ((laarr[c + 1].extLength >> 30) ==
876 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700877 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800878 length = currlength =
879 (((laarr[c + 1].extLength &
880 UDF_EXTENT_LENGTH_MASK) +
881 inode->i_sb->s_blocksize - 1) >>
882 inode->i_sb->s_blocksize_bits);
883 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 start = c;
885 }
886
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700887 for (i = start + 1; i <= *endnum; i++) {
888 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (lastblock)
890 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800891 } else if ((laarr[i].extLength >> 30) ==
892 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
893 length += (((laarr[i].extLength &
894 UDF_EXTENT_LENGTH_MASK) +
895 inode->i_sb->s_blocksize - 1) >>
896 inode->i_sb->s_blocksize_bits);
897 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
899 }
900
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700901 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700903 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800904 inode->i_sb->s_blocksize - 1) >>
905 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800907 laarr[start].extLocation.partitionReferenceNum,
908 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
909 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
910 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700911 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800912 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800914 (numalloc <<
915 inode->i_sb->s_blocksize_bits);
916 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700917 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200918 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700919 (*endnum)++;
920 laarr[c + 1].extLocation.logicalBlockNum = next;
921 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800922 laarr[c].extLocation.
923 partitionReferenceNum;
924 laarr[c + 1].extLength =
925 EXT_NOT_RECORDED_ALLOCATED |
926 (numalloc <<
927 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700928 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700931 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800932 int elen = ((laarr[i].extLength &
933 UDF_EXTENT_LENGTH_MASK) +
934 inode->i_sb->s_blocksize - 1) >>
935 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700937 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800939 (numalloc <<
940 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700942 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700944 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800945 memmove(&laarr[i],
946 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200947 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800948 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700949 i--;
950 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800953 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800954 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 }
957}
958
959static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200960 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700961 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800964 unsigned long blocksize = inode->i_sb->s_blocksize;
965 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700967 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200968 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
969 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Marcin Slusarz4b111112008-02-08 04:20:36 -0800971 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
972 (((li->extLength >> 30) ==
973 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
974 ((lip1->extLocation.logicalBlockNum -
975 li->extLocation.logicalBlockNum) ==
976 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
977 blocksize - 1) >> blocksize_bits)))) {
978
979 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
980 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
981 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
982 lip1->extLength = (lip1->extLength -
983 (li->extLength &
984 UDF_EXTENT_LENGTH_MASK) +
985 UDF_EXTENT_LENGTH_MASK) &
986 ~(blocksize - 1);
987 li->extLength = (li->extLength &
988 UDF_EXTENT_FLAG_MASK) +
989 (UDF_EXTENT_LENGTH_MASK + 1) -
990 blocksize;
991 lip1->extLocation.logicalBlockNum =
992 li->extLocation.logicalBlockNum +
993 ((li->extLength &
994 UDF_EXTENT_LENGTH_MASK) >>
995 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700996 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800997 li->extLength = lip1->extLength +
998 (((li->extLength &
999 UDF_EXTENT_LENGTH_MASK) +
1000 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001001 if (*endnum > (i + 2))
1002 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001003 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001004 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001005 i--;
1006 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001008 } else if (((li->extLength >> 30) ==
1009 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1010 ((lip1->extLength >> 30) ==
1011 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001012 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001013 ((li->extLength &
1014 UDF_EXTENT_LENGTH_MASK) +
1015 blocksize - 1) >> blocksize_bits);
1016 li->extLocation.logicalBlockNum = 0;
1017 li->extLocation.partitionReferenceNum = 0;
1018
1019 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1020 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1021 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1022 lip1->extLength = (lip1->extLength -
1023 (li->extLength &
1024 UDF_EXTENT_LENGTH_MASK) +
1025 UDF_EXTENT_LENGTH_MASK) &
1026 ~(blocksize - 1);
1027 li->extLength = (li->extLength &
1028 UDF_EXTENT_FLAG_MASK) +
1029 (UDF_EXTENT_LENGTH_MASK + 1) -
1030 blocksize;
1031 } else {
1032 li->extLength = lip1->extLength +
1033 (((li->extLength &
1034 UDF_EXTENT_LENGTH_MASK) +
1035 blocksize - 1) & ~(blocksize - 1));
1036 if (*endnum > (i + 2))
1037 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001038 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001039 (*endnum - (i + 2)));
1040 i--;
1041 (*endnum)--;
1042 }
1043 } else if ((li->extLength >> 30) ==
1044 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1045 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001046 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001047 ((li->extLength &
1048 UDF_EXTENT_LENGTH_MASK) +
1049 blocksize - 1) >> blocksize_bits);
1050 li->extLocation.logicalBlockNum = 0;
1051 li->extLocation.partitionReferenceNum = 0;
1052 li->extLength = (li->extLength &
1053 UDF_EXTENT_LENGTH_MASK) |
1054 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 }
1057}
1058
1059static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001060 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001061 int startnum, int endnum,
1062 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001065 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 uint32_t tmplen;
1067
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001068 if (startnum > endnum) {
1069 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001070 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001071 laarr[i].extLength);
1072 } else if (startnum < endnum) {
1073 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001074 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001075 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001076 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001077 &laarr[i].extLength, 1);
1078 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 }
1081
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001082 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001083 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001084 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001085 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087}
1088
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001089struct buffer_head *udf_bread(struct inode *inode, int block,
1090 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001092 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 bh = udf_getblk(inode, block, create, err);
1095 if (!bh)
1096 return NULL;
1097
1098 if (buffer_uptodate(bh))
1099 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 wait_on_buffer(bh);
1104 if (buffer_uptodate(bh))
1105 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 brelse(bh);
1108 *err = -EIO;
1109 return NULL;
1110}
1111
Jan Kara7e49b6f2010-10-22 00:30:26 +02001112int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001115 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +02001116 int bsize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001119 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001120 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001122 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001124 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001125 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001126 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001127 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1128 if (bsize <
1129 (udf_file_entry_alloc_offset(inode) + newsize)) {
1130 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001131 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001132 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001133 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001134 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001135 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001136 goto set_size;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001139 err = udf_extend_file(inode, newsize);
1140 if (err) {
1141 up_write(&iinfo->i_data_sem);
1142 return err;
1143 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001144set_size:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001145 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001146 up_write(&iinfo->i_data_sem);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001147 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001148 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1149 down_write(&iinfo->i_data_sem);
1150 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1151 0x00, bsize - newsize -
1152 udf_file_entry_alloc_offset(inode));
1153 iinfo->i_lenAlloc = newsize;
1154 truncate_setsize(inode, newsize);
1155 up_write(&iinfo->i_data_sem);
1156 goto update_time;
1157 }
1158 err = block_truncate_page(inode->i_mapping, newsize,
1159 udf_get_block);
1160 if (err)
1161 return err;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001162 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001163 truncate_setsize(inode, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001165 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001166 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001167update_time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1169 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001170 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 else
1172 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001176static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 struct buffer_head *bh = NULL;
1179 struct fileEntry *fe;
1180 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001181 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /*
1184 * Set defaults, but the inode is still incomplete!
1185 * Note: get_new_inode() sets the following on a new inode:
1186 * i_sb = sb
1187 * i_no = ino
1188 * i_flags = sb->s_flags
1189 * i_state = 0
1190 * clean_inode(): zero fills and sets
1191 * i_count = 1
1192 * i_nlink = 1
1193 * i_op = NULL;
1194 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001195 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001196 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001197 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 make_bad_inode(inode);
1199 return;
1200 }
1201
1202 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001203 ident != TAG_IDENT_USE) {
Joe Perches78ace702011-10-10 01:08:05 -07001204 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1205 inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001206 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 make_bad_inode(inode);
1208 return;
1209 }
1210
1211 fe = (struct fileEntry *)bh->b_data;
1212
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001213 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001214 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Pekka Enberg97e961f2008-10-15 12:29:03 +02001216 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001217 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001218 if (ident == TAG_IDENT_IE && ibh) {
1219 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001220 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001221 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001222
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001223 ie = (struct indirectEntry *)ibh->b_data;
1224 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001225
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001226 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001227 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001228 &ident))) {
1229 if (ident == TAG_IDENT_FE ||
1230 ident == TAG_IDENT_EFE) {
1231 memcpy(&iinfo->i_location,
1232 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001233 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001234 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001235 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001236 brelse(nbh);
1237 __udf_read_inode(inode);
1238 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001239 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001240 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001242 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001243 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001244 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Joe Perches78ace702011-10-10 01:08:05 -07001245 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1246 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001247 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 make_bad_inode(inode);
1249 return;
1250 }
1251 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001252
Jan Kara3bf25cb2007-05-08 00:35:16 -07001253 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1257{
1258 struct fileEntry *fe;
1259 struct extendedFileEntry *efe;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001260 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001261 struct udf_inode_info *iinfo = UDF_I(inode);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001262 unsigned int link_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 fe = (struct fileEntry *)bh->b_data;
1265 efe = (struct extendedFileEntry *)bh->b_data;
1266
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001267 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001268 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001269 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001270 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001272 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001273 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001274 iinfo->i_unique = 0;
1275 iinfo->i_lenEAttr = 0;
1276 iinfo->i_lenExtents = 0;
1277 iinfo->i_lenAlloc = 0;
1278 iinfo->i_next_alloc_block = 0;
1279 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001280 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001281 iinfo->i_efe = 1;
1282 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001283 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1284 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001285 make_bad_inode(inode);
1286 return;
1287 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001288 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001289 bh->b_data + sizeof(struct extendedFileEntry),
1290 inode->i_sb->s_blocksize -
1291 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001292 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001293 iinfo->i_efe = 0;
1294 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001295 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1296 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001297 make_bad_inode(inode);
1298 return;
1299 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001300 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001301 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001302 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001303 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001304 iinfo->i_efe = 0;
1305 iinfo->i_use = 1;
1306 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001307 ((struct unallocSpaceEntry *)bh->b_data)->
1308 lengthAllocDescs);
1309 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1310 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001311 make_bad_inode(inode);
1312 return;
1313 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001314 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001315 bh->b_data + sizeof(struct unallocSpaceEntry),
1316 inode->i_sb->s_blocksize -
1317 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return;
1319 }
1320
Jan Karac03cad22010-10-20 22:17:28 +02001321 read_lock(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001323 if (inode->i_uid == -1 ||
1324 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1325 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001326 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001329 if (inode->i_gid == -1 ||
1330 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1331 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001332 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001334 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001335 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001336 inode->i_mode = sbi->s_fmode;
1337 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001338 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001339 inode->i_mode = sbi->s_dmode;
1340 else
1341 inode->i_mode = udf_convert_permissions(fe);
1342 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001343 read_unlock(&sbi->s_cred_lock);
1344
Miklos Szeredibfe86842011-10-28 14:13:29 +02001345 link_count = le16_to_cpu(fe->fileLinkCount);
1346 if (!link_count)
1347 link_count = 1;
1348 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001349
1350 inode->i_size = le64_to_cpu(fe->informationLength);
1351 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001353 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001355 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Marcin Slusarz56774802008-02-10 11:25:31 +01001357 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001358 inode->i_atime = sbi->s_record_time;
1359
Marcin Slusarz56774802008-02-10 11:25:31 +01001360 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1361 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001362 inode->i_mtime = sbi->s_record_time;
1363
Marcin Slusarz56774802008-02-10 11:25:31 +01001364 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001365 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001367 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1368 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1369 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001370 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001371 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001372 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001373 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Marcin Slusarz56774802008-02-10 11:25:31 +01001375 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001376 inode->i_atime = sbi->s_record_time;
1377
Marcin Slusarz56774802008-02-10 11:25:31 +01001378 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1379 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001380 inode->i_mtime = sbi->s_record_time;
1381
Marcin Slusarz56774802008-02-10 11:25:31 +01001382 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001383 iinfo->i_crtime = sbi->s_record_time;
1384
Marcin Slusarz56774802008-02-10 11:25:31 +01001385 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001386 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001388 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1389 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1390 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001391 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001394 switch (fe->icbTag.fileType) {
1395 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001396 inode->i_op = &udf_dir_inode_operations;
1397 inode->i_fop = &udf_dir_operations;
1398 inode->i_mode |= S_IFDIR;
1399 inc_nlink(inode);
1400 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001401 case ICBTAG_FILE_TYPE_REALTIME:
1402 case ICBTAG_FILE_TYPE_REGULAR:
1403 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001404 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001405 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001406 inode->i_data.a_ops = &udf_adinicb_aops;
1407 else
1408 inode->i_data.a_ops = &udf_aops;
1409 inode->i_op = &udf_file_inode_operations;
1410 inode->i_fop = &udf_file_operations;
1411 inode->i_mode |= S_IFREG;
1412 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001413 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001414 inode->i_mode |= S_IFBLK;
1415 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001416 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001417 inode->i_mode |= S_IFCHR;
1418 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001419 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001420 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1421 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001422 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001423 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1424 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001425 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001426 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001427 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001428 inode->i_mode = S_IFLNK | S_IRWXUGO;
1429 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001430 case ICBTAG_FILE_TYPE_MAIN:
1431 udf_debug("METADATA FILE-----\n");
1432 break;
1433 case ICBTAG_FILE_TYPE_MIRROR:
1434 udf_debug("METADATA MIRROR FILE-----\n");
1435 break;
1436 case ICBTAG_FILE_TYPE_BITMAP:
1437 udf_debug("METADATA BITMAP FILE-----\n");
1438 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001439 default:
Joe Perches78ace702011-10-10 01:08:05 -07001440 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1441 inode->i_ino, fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001442 make_bad_inode(inode);
1443 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001445 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001446 struct deviceSpec *dsea =
1447 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001448 if (dsea) {
1449 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001450 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1451 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001453 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456}
1457
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001458static int udf_alloc_i_data(struct inode *inode, size_t size)
1459{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001460 struct udf_inode_info *iinfo = UDF_I(inode);
1461 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001462
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001463 if (!iinfo->i_ext.i_data) {
Joe Perches78ace702011-10-10 01:08:05 -07001464 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1465 inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001466 return -ENOMEM;
1467 }
1468
1469 return 0;
1470}
1471
Al Virofaa17292011-07-26 03:18:29 -04001472static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Al Virofaa17292011-07-26 03:18:29 -04001474 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 uint32_t permissions;
1476 uint32_t flags;
1477
1478 permissions = le32_to_cpu(fe->permissions);
1479 flags = le16_to_cpu(fe->icbTag.flags);
1480
Marcin Slusarz4b111112008-02-08 04:20:36 -08001481 mode = ((permissions) & S_IRWXO) |
1482 ((permissions >> 2) & S_IRWXG) |
1483 ((permissions >> 4) & S_IRWXU) |
1484 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1485 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1486 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 return mode;
1489}
1490
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001491int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Jan Kara49521de2010-10-20 17:42:44 +02001493 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Jan Kara49521de2010-10-20 17:42:44 +02001496static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 return udf_update_inode(inode, 1);
1499}
1500
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001501static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 struct buffer_head *bh = NULL;
1504 struct fileEntry *fe;
1505 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001506 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 uint32_t udfperms;
1508 uint16_t icbflags;
1509 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001511 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001512 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001513 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Jan Kara5833ded2010-01-08 16:52:59 +01001515 bh = udf_tgetblk(inode->i_sb,
1516 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001517 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001518 udf_debug("getblk failure\n");
1519 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
Jan Karaaae917c2010-01-08 16:46:29 +01001522 lock_buffer(bh);
1523 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 fe = (struct fileEntry *)bh->b_data;
1525 efe = (struct extendedFileEntry *)bh->b_data;
1526
Jan Karaaae917c2010-01-08 16:46:29 +01001527 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001529 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001531 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001532 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001533 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001534 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001535 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1536 use->descTag.tagLocation =
1537 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001538 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001539 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001541 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001542 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001543 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001544 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Jan Karaaae917c2010-01-08 16:46:29 +01001546 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548
Phillip Susi4d6660e2006-03-07 21:55:24 -08001549 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1550 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001551 else
1552 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Phillip Susi4d6660e2006-03-07 21:55:24 -08001554 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1555 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001556 else
1557 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Marcin Slusarz4b111112008-02-08 04:20:36 -08001559 udfperms = ((inode->i_mode & S_IRWXO)) |
1560 ((inode->i_mode & S_IRWXG) << 2) |
1561 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Marcin Slusarz4b111112008-02-08 04:20:36 -08001563 udfperms |= (le32_to_cpu(fe->permissions) &
1564 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1565 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1566 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 fe->permissions = cpu_to_le32(udfperms);
1568
1569 if (S_ISDIR(inode->i_mode))
1570 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1571 else
1572 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1573
1574 fe->informationLength = cpu_to_le64(inode->i_size);
1575
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001576 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001577 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001578 struct deviceSpec *dsea =
1579 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001580 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001582 udf_add_extendedattr(inode,
1583 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001584 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 dsea->attrType = cpu_to_le32(12);
1586 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001587 dsea->attrLength = cpu_to_le32(
1588 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001589 sizeof(struct regid));
1590 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001592 eid = (struct regid *)dsea->impUse;
1593 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 strcpy(eid->ident, UDF_ID_DEVELOPER);
1595 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1596 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1597 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1598 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1599 }
1600
Steve Nickelb2527bf2012-02-16 12:53:53 -05001601 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1602 lb_recorded = 0; /* No extents => no blocks! */
1603 else
1604 lb_recorded =
1605 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1606 (blocksize_bits - 9);
1607
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001608 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001609 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001610 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001611 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001612 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Marcin Slusarz56774802008-02-10 11:25:31 +01001614 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1615 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1616 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001617 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1619 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1620 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001621 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1622 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1623 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001624 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1626 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001627 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001628 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001629 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001630 inode->i_sb->s_blocksize -
1631 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001633 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001635 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1636 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1637 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1638 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001639
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001640 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1641 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1642 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1643 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001644
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001645 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1646 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1647 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1648 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Marcin Slusarz56774802008-02-10 11:25:31 +01001650 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1651 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1652 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1653 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001655 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1657 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1658 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001659 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1660 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1661 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001662 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1664 crclen = sizeof(struct extendedFileEntry);
1665 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001666 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 fe->icbTag.strategyType = cpu_to_le16(4096);
1668 fe->icbTag.strategyParameter = cpu_to_le16(1);
1669 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001670 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 fe->icbTag.strategyType = cpu_to_le16(4);
1672 fe->icbTag.numEntries = cpu_to_le16(1);
1673 }
1674
1675 if (S_ISDIR(inode->i_mode))
1676 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1677 else if (S_ISREG(inode->i_mode))
1678 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1679 else if (S_ISLNK(inode->i_mode))
1680 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1681 else if (S_ISBLK(inode->i_mode))
1682 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1683 else if (S_ISCHR(inode->i_mode))
1684 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1685 else if (S_ISFIFO(inode->i_mode))
1686 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1687 else if (S_ISSOCK(inode->i_mode))
1688 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1689
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001690 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001691 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1692 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1693 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1694 (le16_to_cpu(fe->icbTag.flags) &
1695 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1696 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001699 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 fe->descTag.descVersion = cpu_to_le16(3);
1701 else
1702 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001703 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001704 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001705 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001706 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001708 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001709 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001710 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Jan Karaaae917c2010-01-08 16:46:29 +01001712out:
Jan Kara5833ded2010-01-08 16:52:59 +01001713 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001714 unlock_buffer(bh);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /* write the data blocks */
1717 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001718 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001720 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001721 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1722 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 err = -EIO;
1724 }
1725 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001726 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return err;
1729}
1730
Pekka Enberg97e961f2008-10-15 12:29:03 +02001731struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1734 struct inode *inode = iget_locked(sb, block);
1735
1736 if (!inode)
1737 return NULL;
1738
1739 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001740 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 __udf_read_inode(inode);
1742 unlock_new_inode(inode);
1743 }
1744
1745 if (is_bad_inode(inode))
1746 goto out_iput;
1747
Pekka Enberg97e961f2008-10-15 12:29:03 +02001748 if (ino->logicalBlockNum >= UDF_SB(sb)->
1749 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001751 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 make_bad_inode(inode);
1753 goto out_iput;
1754 }
1755
1756 return inode;
1757
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001758 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 iput(inode);
1760 return NULL;
1761}
1762
Jan Kara7e49b6f2010-10-22 00:30:26 +02001763int udf_add_aext(struct inode *inode, struct extent_position *epos,
1764 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001767 struct short_ad *sad = NULL;
1768 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 struct allocExtDesc *aed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001771 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Jan Karaff116fc2007-05-08 00:35:14 -07001773 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001774 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001775 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001776 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 else
Jan Karaff116fc2007-05-08 00:35:14 -07001778 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001780 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001781 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001782 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001783 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001785 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001787 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001788 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct buffer_head *nbh;
1790 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001791 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Marcin Slusarz4b111112008-02-08 04:20:36 -08001793 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1794 obloc.partitionReferenceNum,
1795 obloc.logicalBlockNum, &err);
1796 if (!epos->block.logicalBlockNum)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001797 return -ENOSPC;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001798 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001799 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001800 0));
1801 if (!nbh)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001802 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 lock_buffer(nbh);
1804 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1805 set_buffer_uptodate(nbh);
1806 unlock_buffer(nbh);
1807 mark_buffer_dirty_inode(nbh, inode);
1808
1809 aed = (struct allocExtDesc *)(nbh->b_data);
1810 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001811 aed->previousAllocExtLocation =
1812 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001813 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001814 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 aed->lengthAllocDescs = cpu_to_le32(adsize);
1816 sptr = ptr - adsize;
1817 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1818 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001819 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001820 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001821 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 aed->lengthAllocDescs = cpu_to_le32(0);
1823 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001824 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001826 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001827 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001828 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001829 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001830 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 mark_inode_dirty(inode);
1832 }
1833 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001834 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001836 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 else
1838 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001839 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001840 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001841 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001842 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001843 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1844 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001845 sad->extPosition =
1846 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001847 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001848 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001849 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001850 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1851 inode->i_sb->s_blocksize);
1852 lad->extLocation = cpu_to_lelb(epos->block);
1853 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001856 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001857 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001858 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001859 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001861 udf_update_tag(epos->bh->b_data,
1862 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001863 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001864 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001865 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001867 }
Jan Karaff116fc2007-05-08 00:35:14 -07001868 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870
Jan Kara7e49b6f2010-10-22 00:30:26 +02001871 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001873 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001874 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001876 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001877 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001878 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001879 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1880 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1881 udf_update_tag(epos->bh->b_data,
1882 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001884 udf_update_tag(epos->bh->b_data,
1885 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001886 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888
Jan Kara7e49b6f2010-10-22 00:30:26 +02001889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Jan Kara7e49b6f2010-10-22 00:30:26 +02001892void udf_write_aext(struct inode *inode, struct extent_position *epos,
1893 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 int adsize;
1896 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001897 struct short_ad *sad;
1898 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001899 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Jan Karaff116fc2007-05-08 00:35:14 -07001901 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001902 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001903 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001904 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 else
Jan Karaff116fc2007-05-08 00:35:14 -07001906 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001908 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001909 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001910 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001911 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001912 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001913 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001914 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001915 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001916 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001917 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001918 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001919 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001920 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001921 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001922 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001923 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001926 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001927 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001928 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001929 struct allocExtDesc *aed =
1930 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001931 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001932 le32_to_cpu(aed->lengthAllocDescs) +
1933 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
Jan Karaff116fc2007-05-08 00:35:14 -07001935 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001936 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001941 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Marcin Slusarz4b111112008-02-08 04:20:36 -08001944int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001945 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 int8_t etype;
1948
Jan Karaff116fc2007-05-08 00:35:14 -07001949 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001950 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001951 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001952 epos->block = *eloc;
1953 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001954 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001955 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001956 epos->bh = udf_tread(inode->i_sb, block);
1957 if (!epos->bh) {
1958 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return -1;
1960 }
1961 }
1962
1963 return etype;
1964}
1965
Marcin Slusarz4b111112008-02-08 04:20:36 -08001966int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001967 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 int alen;
1970 int8_t etype;
1971 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001972 struct short_ad *sad;
1973 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001974 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001975
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001976 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001977 if (!epos->offset)
1978 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001979 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001980 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001981 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001982 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001983 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001984 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001985 if (!epos->offset)
1986 epos->offset = sizeof(struct allocExtDesc);
1987 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001988 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001989 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1990 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001993 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001994 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001995 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1996 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001998 etype = le32_to_cpu(sad->extLength) >> 30;
1999 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002000 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002001 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002002 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2003 break;
2004 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002005 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2006 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002007 return -1;
2008 etype = le32_to_cpu(lad->extLength) >> 30;
2009 *eloc = lelb_to_cpu(lad->extLocation);
2010 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2011 break;
2012 default:
Joe Perchesa983f362011-10-10 01:08:07 -07002013 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002014 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016
2017 return etype;
2018}
2019
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002020static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002021 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002023 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 uint32_t oelen;
2025 int8_t etype;
2026
Jan Karaff116fc2007-05-08 00:35:14 -07002027 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002028 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002030 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002031 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 neloc = oeloc;
2033 nelen = (etype << 30) | oelen;
2034 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002035 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002036 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return (nelen >> 30);
2039}
2040
Marcin Slusarz4b111112008-02-08 04:20:36 -08002041int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002042 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Jan Karaff116fc2007-05-08 00:35:14 -07002044 struct extent_position oepos;
2045 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int8_t etype;
2047 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002048 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002050 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002051 get_bh(epos.bh);
2052 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002055 iinfo = UDF_I(inode);
2056 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002057 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002058 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002059 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 else
2061 adsize = 0;
2062
Jan Karaff116fc2007-05-08 00:35:14 -07002063 oepos = epos;
2064 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return -1;
2066
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002067 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002068 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002069 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002070 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002071 brelse(oepos.bh);
2072 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002073 oepos.bh = epos.bh;
2074 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002077 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 elen = 0;
2079
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002080 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002081 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2082 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2083 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002084 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002085 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002087 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002088 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002089 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002090 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002091 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002092 udf_update_tag(oepos.bh->b_data,
2093 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002095 udf_update_tag(oepos.bh->b_data,
2096 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002097 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002099 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002100 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002101 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002102 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002104 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002105 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002106 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002107 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002108 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002109 udf_update_tag(oepos.bh->b_data,
2110 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002112 udf_update_tag(oepos.bh->b_data,
2113 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002114 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002117
Jan Kara3bf25cb2007-05-08 00:35:16 -07002118 brelse(epos.bh);
2119 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return (elen >> 30);
2122}
2123
Marcin Slusarz4b111112008-02-08 04:20:36 -08002124int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002125 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002126 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002128 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002129 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002130 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002132 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002134 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002135 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002136 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002137 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002140 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002141 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2142 if (etype == -1) {
2143 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002144 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return -1;
2146 }
2147 lbcount += *elen;
2148 } while (lbcount <= bcount);
2149
Marcin Slusarz4b111112008-02-08 04:20:36 -08002150 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 return etype;
2153}
2154
Jan Kara60448b12007-05-08 00:35:13 -07002155long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002157 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002158 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002159 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002160 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 int ret;
2162
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002163 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Marcin Slusarz4b111112008-02-08 04:20:36 -08002165 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2166 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002167 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 else
2169 ret = 0;
2170
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002171 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002172 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2175 return udf_fixed_to_variable(ret);
2176 else
2177 return ret;
2178}