blob: 2b7759371ff6735c9932ef73076a9504b603b21e [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
Ian Abbott5eec54f2012-09-05 17:44:31 +010098static void udf_write_failed(struct address_space *mapping, loff_t to)
99{
100 struct inode *inode = mapping->host;
101 struct udf_inode_info *iinfo = UDF_I(inode);
102 loff_t isize = inode->i_size;
103
104 if (to > isize) {
105 truncate_pagecache(inode, to, isize);
106 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
107 down_write(&iinfo->i_data_sem);
108 udf_truncate_extents(inode);
109 up_write(&iinfo->i_data_sem);
110 }
111 }
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int udf_writepage(struct page *page, struct writeback_control *wbc)
115{
116 return block_write_full_page(page, udf_get_block, wbc);
117}
118
Namjae Jeon378b8e12012-08-31 12:49:07 -0400119static int udf_writepages(struct address_space *mapping,
120 struct writeback_control *wbc)
121{
122 return mpage_writepages(mapping, wbc, udf_get_block);
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int udf_readpage(struct file *file, struct page *page)
126{
Namjae Jeonbc112322011-10-03 14:02:59 +0900127 return mpage_readpage(page, udf_get_block);
128}
129
130static int udf_readpages(struct file *file, struct address_space *mapping,
131 struct list_head *pages, unsigned nr_pages)
132{
133 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Nick Pigginbe021ee2007-10-16 01:25:20 -0700136static int udf_write_begin(struct file *file, struct address_space *mapping,
137 loff_t pos, unsigned len, unsigned flags,
138 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200140 int ret;
141
142 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100143 if (unlikely(ret))
144 udf_write_failed(mapping, pos + len);
145 return ret;
146}
Jan Kara7e49b6f2010-10-22 00:30:26 +0200147
Ian Abbott5eec54f2012-09-05 17:44:31 +0100148static ssize_t udf_direct_IO(int rw, struct kiocb *iocb,
149 const struct iovec *iov,
150 loff_t offset, unsigned long nr_segs)
151{
152 struct file *file = iocb->ki_filp;
153 struct address_space *mapping = file->f_mapping;
154 struct inode *inode = mapping->host;
155 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200156
Ian Abbott5eec54f2012-09-05 17:44:31 +0100157 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
158 udf_get_block);
159 if (unlikely(ret < 0 && (rw & WRITE)))
160 udf_write_failed(mapping, offset + iov_length(iov, nr_segs));
Christoph Hellwig155130a2010-06-04 11:29:58 +0200161 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164static sector_t udf_bmap(struct address_space *mapping, sector_t block)
165{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700166 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700169const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700170 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900171 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700172 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400173 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100174 .write_begin = udf_write_begin,
175 .write_end = generic_write_end,
176 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700177 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
Jan Karad2eb8c32011-12-10 02:30:48 +0100180/*
181 * Expand file stored in ICB to a normal one-block-file
182 *
183 * This function requires i_data_sem for writing and releases it.
184 * This function requires i_mutex held
185 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200186int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct page *page;
189 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800190 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200191 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct writeback_control udf_wbc = {
193 .sync_mode = WB_SYNC_NONE,
194 .nr_to_write = 1,
195 };
196
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800197 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800199 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800201 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200202 /* from now on we have normal address_space methods */
203 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100204 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100208 /*
209 * Release i_data_sem so that we can lock a page - page lock ranks
210 * above i_data_sem. i_mutex still protects us against file changes.
211 */
212 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jan Kara7e49b6f2010-10-22 00:30:26 +0200214 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
215 if (!page)
216 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700217
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700218 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800220 memset(kaddr + iinfo->i_lenAlloc, 0x00,
221 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
222 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
223 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 flush_dcache_page(page);
225 SetPageUptodate(page);
226 kunmap(page);
227 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100228 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800229 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
230 iinfo->i_lenAlloc);
231 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800233 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800235 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200236 /* from now on we have normal address_space methods */
237 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100238 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200239 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
240 if (err) {
241 /* Restore everything back so that we don't lose data... */
242 lock_page(page);
243 kaddr = kmap(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100244 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200245 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
246 inode->i_size);
247 kunmap(page);
248 unlock_page(page);
249 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
250 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100251 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200255
256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700259struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
260 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700263 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200264 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700266 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800269 loff_t f_pos = udf_ext0_offset(inode);
270 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800272 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
275 alloctype = ICBTAG_FLAG_AD_SHORT;
276 else
277 alloctype = ICBTAG_FLAG_AD_LONG;
278
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700279 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800280 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 mark_inode_dirty(inode);
282 return NULL;
283 }
284
285 /* alloc block, and copy data to it */
286 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800287 iinfo->i_location.partitionReferenceNum,
288 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!(*block))
290 return NULL;
291 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800292 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800293 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!newblock)
295 return NULL;
296 dbh = udf_tgetblk(inode->i_sb, newblock);
297 if (!dbh)
298 return NULL;
299 lock_buffer(dbh);
300 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
301 set_buffer_uptodate(dbh);
302 unlock_buffer(dbh);
303 mark_buffer_dirty_inode(dbh, inode);
304
Marcin Slusarz4b111112008-02-08 04:20:36 -0800305 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800306 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700307 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 dfibh.soffset = dfibh.eoffset = 0;
309 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800310 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800311 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800312 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
313 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700314 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700315 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return NULL;
317 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800318 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 sfi->descTag.tagLocation = cpu_to_le32(*block);
320 dfibh.soffset = dfibh.eoffset;
321 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
322 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
323 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800324 sfi->fileIdent +
325 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800326 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700327 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return NULL;
329 }
330 }
331 mark_buffer_dirty_inode(dbh, inode);
332
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800333 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
334 iinfo->i_lenAlloc);
335 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800337 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800338 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100339 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700340 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800341 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700342 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100343 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* UniqueID stuff */
345
Jan Kara3bf25cb2007-05-08 00:35:16 -0700346 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 mark_inode_dirty(inode);
348 return dbh;
349}
350
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700351static int udf_get_block(struct inode *inode, sector_t block,
352 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800355 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800356 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700358 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 phys = udf_block_map(inode, block);
360 if (phys)
361 map_bh(bh_result, inode->i_sb, phys);
362 return 0;
363 }
364
365 err = -EIO;
366 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800367 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100368
369 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800370 if (block == iinfo->i_next_alloc_block + 1) {
371 iinfo->i_next_alloc_block++;
372 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jan Kara7b0b0932011-12-10 01:43:33 +0100376 phys = inode_getblk(inode, block, &err, &new);
377 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (new)
381 set_buffer_new(bh_result);
382 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700383
384abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100385 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700389static struct buffer_head *udf_getblk(struct inode *inode, long block,
390 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700392 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct buffer_head dummy;
394
395 dummy.b_state = 0;
396 dummy.b_blocknr = -1000;
397 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700398 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700400 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 lock_buffer(bh);
402 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
403 set_buffer_uptodate(bh);
404 unlock_buffer(bh);
405 mark_buffer_dirty_inode(bh, inode);
406 }
407 return bh;
408 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return NULL;
411}
412
Jan Kara31170b62007-05-08 00:35:21 -0700413/* Extend the file by 'blocks' blocks, return the number of extents added */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200414static int udf_do_extend_file(struct inode *inode,
415 struct extent_position *last_pos,
416 struct kernel_long_ad *last_ext,
417 sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700418{
419 sector_t add;
420 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
421 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200422 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700423 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800424 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200425 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700426
427 /* The previous extent is fake and we should not extend by anything
428 * - there's nothing to do... */
429 if (!blocks && fake)
430 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700431
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800432 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700433 /* Round the last extent up to a multiple of block size */
434 if (last_ext->extLength & (sb->s_blocksize - 1)) {
435 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
437 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
438 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800439 iinfo->i_lenExtents =
440 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700441 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700442 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700443
Jan Kara31170b62007-05-08 00:35:21 -0700444 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800445 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
446 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700447 /* Save the extent so that we can reattach it to the end */
448 prealloc_loc = last_ext->extLocation;
449 prealloc_len = last_ext->extLength;
450 /* Mark the extent as a hole */
451 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700452 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700453 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800454 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700455 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700456
Jan Kara31170b62007-05-08 00:35:21 -0700457 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800458 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
459 EXT_NOT_RECORDED_NOT_ALLOCATED) {
460 add = ((1 << 30) - sb->s_blocksize -
461 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
462 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700463 if (add > blocks)
464 add = blocks;
465 blocks -= add;
466 last_ext->extLength += add << sb->s_blocksize_bits;
467 }
468
469 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200470 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700471 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700472 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800473 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200474 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800475 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700476
Jan Kara31170b62007-05-08 00:35:21 -0700477 /* Managed to do everything necessary? */
478 if (!blocks)
479 goto out;
480
481 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
482 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800483 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700484 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800485 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
486 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700487
Jan Kara31170b62007-05-08 00:35:21 -0700488 /* Create enough extents to cover the whole hole */
489 while (blocks > add) {
490 blocks -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200491 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
492 last_ext->extLength, 1);
493 if (err)
494 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700495 count++;
496 }
497 if (blocks) {
498 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700499 (blocks << sb->s_blocksize_bits);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200500 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
501 last_ext->extLength, 1);
502 if (err)
503 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700504 count++;
505 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700506
507out:
Jan Kara31170b62007-05-08 00:35:21 -0700508 /* Do we have some preallocated blocks saved? */
509 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200510 err = udf_add_aext(inode, last_pos, &prealloc_loc,
511 prealloc_len, 1);
512 if (err)
513 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700514 last_ext->extLocation = prealloc_loc;
515 last_ext->extLength = prealloc_len;
516 count++;
517 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700518
Jan Kara31170b62007-05-08 00:35:21 -0700519 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800520 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200521 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800522 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200523 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700524 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200525 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700526
Jan Kara31170b62007-05-08 00:35:21 -0700527 return count;
528}
529
Jan Kara7e49b6f2010-10-22 00:30:26 +0200530static int udf_extend_file(struct inode *inode, loff_t newsize)
531{
532
533 struct extent_position epos;
534 struct kernel_lb_addr eloc;
535 uint32_t elen;
536 int8_t etype;
537 struct super_block *sb = inode->i_sb;
538 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
539 int adsize;
540 struct udf_inode_info *iinfo = UDF_I(inode);
541 struct kernel_long_ad extent;
542 int err;
543
544 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
545 adsize = sizeof(struct short_ad);
546 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
547 adsize = sizeof(struct long_ad);
548 else
549 BUG();
550
551 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
552
553 /* File has extent covering the new size (could happen when extending
554 * inside a block)? */
555 if (etype != -1)
556 return 0;
557 if (newsize & (sb->s_blocksize - 1))
558 offset++;
559 /* Extended file just to the boundary of the last file block? */
560 if (offset == 0)
561 return 0;
562
563 /* Truncate is extending the file by 'offset' blocks */
564 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
565 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
566 /* File has no extents at all or has empty last
567 * indirect extent! Create a fake extent... */
568 extent.extLocation.logicalBlockNum = 0;
569 extent.extLocation.partitionReferenceNum = 0;
570 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
571 } else {
572 epos.offset -= adsize;
573 etype = udf_next_aext(inode, &epos, &extent.extLocation,
574 &extent.extLength, 0);
575 extent.extLength |= etype << 30;
576 }
577 err = udf_do_extend_file(inode, &epos, &extent, offset);
578 if (err < 0)
579 goto out;
580 err = 0;
581 iinfo->i_lenExtents = newsize;
582out:
583 brelse(epos.bh);
584 return err;
585}
586
Jan Kara7b0b0932011-12-10 01:43:33 +0100587static sector_t inode_getblk(struct inode *inode, sector_t block,
588 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Jan Kara31170b62007-05-08 00:35:21 -0700590 static sector_t last_block;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200591 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700592 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700594 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200595 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700597 loff_t lbcount = 0, b_off = 0;
598 uint32_t newblocknum, newblock;
599 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800601 struct udf_inode_info *iinfo = UDF_I(inode);
602 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700603 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Jan Kara7b0b0932011-12-10 01:43:33 +0100605 *err = 0;
606 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700607 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800608 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700609 prev_epos.bh = NULL;
610 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700611 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 alternate between laarr[0] and laarr[1] for locations of the
615 current extent, and the previous extent */
616 do {
617 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700618 brelse(prev_epos.bh);
619 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700620 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700622 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700623 brelse(cur_epos.bh);
624 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700625 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 lbcount += elen;
629
Jan Karaff116fc2007-05-08 00:35:14 -0700630 prev_epos.block = cur_epos.block;
631 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jan Karaff116fc2007-05-08 00:35:14 -0700633 prev_epos.offset = cur_epos.offset;
634 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Marcin Slusarz4b111112008-02-08 04:20:36 -0800636 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
637 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639
640 c = !c;
641
642 laarr[c].extLength = (etype << 30) | elen;
643 laarr[c].extLocation = eloc;
644
645 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
646 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700647 ((elen + inode->i_sb->s_blocksize - 1) >>
648 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700650 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 } while (lbcount + elen <= b_off);
652
653 b_off -= lbcount;
654 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700655 /*
656 * Move prev_epos and cur_epos into indirect extent if we are at
657 * the pointer to it
658 */
659 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
660 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700663 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700665 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
666 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700668 ((elen + inode->i_sb->s_blocksize - 1) &
669 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200670 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700672 brelse(prev_epos.bh);
673 brelse(cur_epos.bh);
674 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200675 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Jan Kara7b0b0932011-12-10 01:43:33 +0100676 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
Jan Kara31170b62007-05-08 00:35:21 -0700679 last_block = block;
680 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700681 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700682 int ret;
683
684 if (count) {
685 if (c)
686 laarr[0] = laarr[1];
687 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700688 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700689 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800690 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200691 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700692 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200693 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800694 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700695 startnum = (offset > 0);
696 }
697 /* Create extents for the hole between EOF and offset */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200698 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
699 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700700 brelse(prev_epos.bh);
701 brelse(cur_epos.bh);
702 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200703 *err = ret;
Jan Kara7b0b0932011-12-10 01:43:33 +0100704 return 0;
Jan Kara31170b62007-05-08 00:35:21 -0700705 }
706 c = 0;
707 offset = 0;
708 count += ret;
709 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800710 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
711 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700712 /* Is there any real extent? - otherwise we overwrite
713 * the fake one... */
714 if (count)
715 c = !c;
716 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700717 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800718 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200719 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700720 count++;
721 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700722 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700723 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700725 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 endnum = startnum = ((count > 2) ? 2 : count);
727
Marcin Slusarz4b111112008-02-08 04:20:36 -0800728 /* if the current extent is in position 0,
729 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700730 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700731 laarr[2] = laarr[0];
732 laarr[0] = laarr[1];
733 laarr[1] = laarr[2];
734 c = 1;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Marcin Slusarz4b111112008-02-08 04:20:36 -0800737 /* if the current block is located in an extent,
738 read the next extent */
739 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
740 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700741 laarr[c + 1].extLength = (etype << 30) | elen;
742 laarr[c + 1].extLocation = eloc;
743 count++;
744 startnum++;
745 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800746 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 lastblock = 1;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700751 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800752 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800754 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800755 if (iinfo->i_next_alloc_block == block)
756 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700758 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800759 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800760 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
Marcin Slusarz4b111112008-02-08 04:20:36 -0800763 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800764 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800765 goal, err);
766 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700767 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900768 brelse(cur_epos.bh);
769 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *err = -ENOSPC;
Jan Kara7b0b0932011-12-10 01:43:33 +0100771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800773 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
Marcin Slusarz4b111112008-02-08 04:20:36 -0800776 /* if the extent the requsted block is located in contains multiple
777 * blocks, split the extent into at most three extents. blocks prior
778 * to requested block, requested block, and blocks after requested
779 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
781
782#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200783 /* We preallocate blocks only for regular files. It also makes sense
784 * for directories but there's a problem when to drop the
785 * preallocation. We might use some delayed work for that but I feel
786 * it's overengineering for a filesystem like UDF. */
787 if (S_ISREG(inode->i_mode))
788 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789#endif
790
791 /* merge any continuous blocks in laarr */
792 udf_merge_extents(inode, laarr, &endnum);
793
794 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700795 * of extents is greater than the old number, and deleting extents if
796 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700797 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jan Kara3bf25cb2007-05-08 00:35:16 -0700799 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900800 brelse(cur_epos.bh);
801 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Marcin Slusarz4b111112008-02-08 04:20:36 -0800803 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800804 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100805 if (!newblock) {
806 *err = -EIO;
807 return 0;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800810 iinfo->i_next_alloc_block = block;
811 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 inode->i_ctime = current_fs_time(inode->i_sb);
813
814 if (IS_SYNC(inode))
815 udf_sync_inode(inode);
816 else
817 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700818
Jan Kara7b0b0932011-12-10 01:43:33 +0100819 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700822static void udf_split_extents(struct inode *inode, int *c, int offset,
823 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200824 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700825 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800827 unsigned long blocksize = inode->i_sb->s_blocksize;
828 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800831 (laarr[*c].extLength >> 30) ==
832 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int curr = *c;
834 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800835 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int8_t etype = (laarr[curr].extLength >> 30);
837
Marcin Slusarz4b111112008-02-08 04:20:36 -0800838 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700839 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800840 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700841 laarr[curr + 2] = laarr[curr + 1];
842 laarr[curr + 1] = laarr[curr];
843 } else {
844 laarr[curr + 3] = laarr[curr + 1];
845 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700848 if (offset) {
849 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800850 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200851 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800852 0, offset);
853 laarr[curr].extLength =
854 EXT_NOT_RECORDED_NOT_ALLOCATED |
855 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800857 laarr[curr].extLocation.
858 partitionReferenceNum = 0;
859 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800861 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700862 curr++;
863 (*c)++;
864 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 laarr[curr].extLocation.logicalBlockNum = newblocknum;
868 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
869 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800870 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800872 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700873 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700875 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800877 laarr[curr].extLocation.logicalBlockNum +=
878 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700879 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800880 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700881 curr++;
882 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 }
885}
886
887static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200888 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700889 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 int start, length = 0, currlength = 0, i;
892
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700893 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!lastblock)
895 return;
896 else
897 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700898 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800899 if ((laarr[c + 1].extLength >> 30) ==
900 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700901 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800902 length = currlength =
903 (((laarr[c + 1].extLength &
904 UDF_EXTENT_LENGTH_MASK) +
905 inode->i_sb->s_blocksize - 1) >>
906 inode->i_sb->s_blocksize_bits);
907 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 start = c;
909 }
910
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700911 for (i = start + 1; i <= *endnum; i++) {
912 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (lastblock)
914 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800915 } else if ((laarr[i].extLength >> 30) ==
916 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
917 length += (((laarr[i].extLength &
918 UDF_EXTENT_LENGTH_MASK) +
919 inode->i_sb->s_blocksize - 1) >>
920 inode->i_sb->s_blocksize_bits);
921 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 break;
923 }
924
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700925 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700927 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800928 inode->i_sb->s_blocksize - 1) >>
929 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800931 laarr[start].extLocation.partitionReferenceNum,
932 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
933 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
934 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700935 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800936 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800938 (numalloc <<
939 inode->i_sb->s_blocksize_bits);
940 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700941 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200942 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700943 (*endnum)++;
944 laarr[c + 1].extLocation.logicalBlockNum = next;
945 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800946 laarr[c].extLocation.
947 partitionReferenceNum;
948 laarr[c + 1].extLength =
949 EXT_NOT_RECORDED_ALLOCATED |
950 (numalloc <<
951 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700952 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700955 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800956 int elen = ((laarr[i].extLength &
957 UDF_EXTENT_LENGTH_MASK) +
958 inode->i_sb->s_blocksize - 1) >>
959 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700961 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800963 (numalloc <<
964 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700966 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700968 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800969 memmove(&laarr[i],
970 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200971 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800972 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700973 i--;
974 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800977 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800978 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 }
981}
982
983static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200984 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700985 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800988 unsigned long blocksize = inode->i_sb->s_blocksize;
989 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700991 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200992 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
993 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Marcin Slusarz4b111112008-02-08 04:20:36 -0800995 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
996 (((li->extLength >> 30) ==
997 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
998 ((lip1->extLocation.logicalBlockNum -
999 li->extLocation.logicalBlockNum) ==
1000 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1001 blocksize - 1) >> blocksize_bits)))) {
1002
1003 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1004 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1005 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1006 lip1->extLength = (lip1->extLength -
1007 (li->extLength &
1008 UDF_EXTENT_LENGTH_MASK) +
1009 UDF_EXTENT_LENGTH_MASK) &
1010 ~(blocksize - 1);
1011 li->extLength = (li->extLength &
1012 UDF_EXTENT_FLAG_MASK) +
1013 (UDF_EXTENT_LENGTH_MASK + 1) -
1014 blocksize;
1015 lip1->extLocation.logicalBlockNum =
1016 li->extLocation.logicalBlockNum +
1017 ((li->extLength &
1018 UDF_EXTENT_LENGTH_MASK) >>
1019 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001020 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001021 li->extLength = lip1->extLength +
1022 (((li->extLength &
1023 UDF_EXTENT_LENGTH_MASK) +
1024 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001025 if (*endnum > (i + 2))
1026 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001027 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001028 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001029 i--;
1030 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001032 } else if (((li->extLength >> 30) ==
1033 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1034 ((lip1->extLength >> 30) ==
1035 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001036 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001037 ((li->extLength &
1038 UDF_EXTENT_LENGTH_MASK) +
1039 blocksize - 1) >> blocksize_bits);
1040 li->extLocation.logicalBlockNum = 0;
1041 li->extLocation.partitionReferenceNum = 0;
1042
1043 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1044 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1045 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1046 lip1->extLength = (lip1->extLength -
1047 (li->extLength &
1048 UDF_EXTENT_LENGTH_MASK) +
1049 UDF_EXTENT_LENGTH_MASK) &
1050 ~(blocksize - 1);
1051 li->extLength = (li->extLength &
1052 UDF_EXTENT_FLAG_MASK) +
1053 (UDF_EXTENT_LENGTH_MASK + 1) -
1054 blocksize;
1055 } else {
1056 li->extLength = lip1->extLength +
1057 (((li->extLength &
1058 UDF_EXTENT_LENGTH_MASK) +
1059 blocksize - 1) & ~(blocksize - 1));
1060 if (*endnum > (i + 2))
1061 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001062 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001063 (*endnum - (i + 2)));
1064 i--;
1065 (*endnum)--;
1066 }
1067 } else if ((li->extLength >> 30) ==
1068 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1069 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001070 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001071 ((li->extLength &
1072 UDF_EXTENT_LENGTH_MASK) +
1073 blocksize - 1) >> blocksize_bits);
1074 li->extLocation.logicalBlockNum = 0;
1075 li->extLocation.partitionReferenceNum = 0;
1076 li->extLength = (li->extLength &
1077 UDF_EXTENT_LENGTH_MASK) |
1078 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 }
1081}
1082
1083static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001084 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001085 int startnum, int endnum,
1086 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001089 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 uint32_t tmplen;
1091
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001092 if (startnum > endnum) {
1093 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001094 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001095 laarr[i].extLength);
1096 } else if (startnum < endnum) {
1097 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001098 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001099 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001100 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001101 &laarr[i].extLength, 1);
1102 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 }
1105
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001106 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001107 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001108 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001109 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111}
1112
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001113struct buffer_head *udf_bread(struct inode *inode, int block,
1114 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001116 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 bh = udf_getblk(inode, block, create, err);
1119 if (!bh)
1120 return NULL;
1121
1122 if (buffer_uptodate(bh))
1123 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 wait_on_buffer(bh);
1128 if (buffer_uptodate(bh))
1129 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 brelse(bh);
1132 *err = -EIO;
1133 return NULL;
1134}
1135
Jan Kara7e49b6f2010-10-22 00:30:26 +02001136int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001139 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +02001140 int bsize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001143 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001146 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001148 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001149 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001150 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001151 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1152 if (bsize <
1153 (udf_file_entry_alloc_offset(inode) + newsize)) {
1154 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001155 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001156 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001157 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001158 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001159 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001160 goto set_size;
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001163 err = udf_extend_file(inode, newsize);
1164 if (err) {
1165 up_write(&iinfo->i_data_sem);
1166 return err;
1167 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001168set_size:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001169 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001170 up_write(&iinfo->i_data_sem);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001171 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001172 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1173 down_write(&iinfo->i_data_sem);
1174 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1175 0x00, bsize - newsize -
1176 udf_file_entry_alloc_offset(inode));
1177 iinfo->i_lenAlloc = newsize;
1178 truncate_setsize(inode, newsize);
1179 up_write(&iinfo->i_data_sem);
1180 goto update_time;
1181 }
1182 err = block_truncate_page(inode->i_mapping, newsize,
1183 udf_get_block);
1184 if (err)
1185 return err;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001186 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001187 truncate_setsize(inode, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001189 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001190 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001191update_time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1193 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001194 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 else
1196 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001200static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct buffer_head *bh = NULL;
1203 struct fileEntry *fe;
1204 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001205 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /*
1208 * Set defaults, but the inode is still incomplete!
1209 * Note: get_new_inode() sets the following on a new inode:
1210 * i_sb = sb
1211 * i_no = ino
1212 * i_flags = sb->s_flags
1213 * i_state = 0
1214 * clean_inode(): zero fills and sets
1215 * i_count = 1
1216 * i_nlink = 1
1217 * i_op = NULL;
1218 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001219 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001220 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001221 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 make_bad_inode(inode);
1223 return;
1224 }
1225
1226 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001227 ident != TAG_IDENT_USE) {
Joe Perches78ace702011-10-10 01:08:05 -07001228 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1229 inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001230 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 make_bad_inode(inode);
1232 return;
1233 }
1234
1235 fe = (struct fileEntry *)bh->b_data;
1236
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001237 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001238 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Pekka Enberg97e961f2008-10-15 12:29:03 +02001240 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001241 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001242 if (ident == TAG_IDENT_IE && ibh) {
1243 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001244 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001245 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001246
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001247 ie = (struct indirectEntry *)ibh->b_data;
1248 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001249
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001250 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001251 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001252 &ident))) {
1253 if (ident == TAG_IDENT_FE ||
1254 ident == TAG_IDENT_EFE) {
1255 memcpy(&iinfo->i_location,
1256 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001257 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001258 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001259 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001260 brelse(nbh);
1261 __udf_read_inode(inode);
1262 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001263 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001264 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001266 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001267 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001268 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Joe Perches78ace702011-10-10 01:08:05 -07001269 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1270 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001271 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 make_bad_inode(inode);
1273 return;
1274 }
1275 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001276
Jan Kara3bf25cb2007-05-08 00:35:16 -07001277 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1281{
1282 struct fileEntry *fe;
1283 struct extendedFileEntry *efe;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001284 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001285 struct udf_inode_info *iinfo = UDF_I(inode);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001286 unsigned int link_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 fe = (struct fileEntry *)bh->b_data;
1289 efe = (struct extendedFileEntry *)bh->b_data;
1290
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001291 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001292 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001293 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001294 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001296 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001297 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001298 iinfo->i_unique = 0;
1299 iinfo->i_lenEAttr = 0;
1300 iinfo->i_lenExtents = 0;
1301 iinfo->i_lenAlloc = 0;
1302 iinfo->i_next_alloc_block = 0;
1303 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001304 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001305 iinfo->i_efe = 1;
1306 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001307 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1308 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001309 make_bad_inode(inode);
1310 return;
1311 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001312 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001313 bh->b_data + sizeof(struct extendedFileEntry),
1314 inode->i_sb->s_blocksize -
1315 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001316 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001317 iinfo->i_efe = 0;
1318 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001319 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1320 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001321 make_bad_inode(inode);
1322 return;
1323 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001324 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001325 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001326 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001327 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001328 iinfo->i_efe = 0;
1329 iinfo->i_use = 1;
1330 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001331 ((struct unallocSpaceEntry *)bh->b_data)->
1332 lengthAllocDescs);
1333 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1334 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001335 make_bad_inode(inode);
1336 return;
1337 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001338 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001339 bh->b_data + sizeof(struct unallocSpaceEntry),
1340 inode->i_sb->s_blocksize -
1341 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return;
1343 }
1344
Jan Karac03cad22010-10-20 22:17:28 +02001345 read_lock(&sbi->s_cred_lock);
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001346 i_uid_write(inode, le32_to_cpu(fe->uid));
1347 if (!uid_valid(inode->i_uid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001348 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1349 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001350 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001352 i_gid_write(inode, le32_to_cpu(fe->gid));
1353 if (!gid_valid(inode->i_gid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001354 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1355 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001356 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001358 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001359 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001360 inode->i_mode = sbi->s_fmode;
1361 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001362 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001363 inode->i_mode = sbi->s_dmode;
1364 else
1365 inode->i_mode = udf_convert_permissions(fe);
1366 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001367 read_unlock(&sbi->s_cred_lock);
1368
Miklos Szeredibfe86842011-10-28 14:13:29 +02001369 link_count = le16_to_cpu(fe->fileLinkCount);
1370 if (!link_count)
1371 link_count = 1;
1372 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001373
1374 inode->i_size = le64_to_cpu(fe->informationLength);
1375 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001377 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001379 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Marcin Slusarz56774802008-02-10 11:25:31 +01001381 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001382 inode->i_atime = sbi->s_record_time;
1383
Marcin Slusarz56774802008-02-10 11:25:31 +01001384 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1385 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001386 inode->i_mtime = sbi->s_record_time;
1387
Marcin Slusarz56774802008-02-10 11:25:31 +01001388 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001389 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001391 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1392 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1393 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001394 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001395 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001396 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001397 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Marcin Slusarz56774802008-02-10 11:25:31 +01001399 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001400 inode->i_atime = sbi->s_record_time;
1401
Marcin Slusarz56774802008-02-10 11:25:31 +01001402 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1403 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001404 inode->i_mtime = sbi->s_record_time;
1405
Marcin Slusarz56774802008-02-10 11:25:31 +01001406 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001407 iinfo->i_crtime = sbi->s_record_time;
1408
Marcin Slusarz56774802008-02-10 11:25:31 +01001409 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001410 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001412 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1413 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1414 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001415 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001418 switch (fe->icbTag.fileType) {
1419 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001420 inode->i_op = &udf_dir_inode_operations;
1421 inode->i_fop = &udf_dir_operations;
1422 inode->i_mode |= S_IFDIR;
1423 inc_nlink(inode);
1424 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001425 case ICBTAG_FILE_TYPE_REALTIME:
1426 case ICBTAG_FILE_TYPE_REGULAR:
1427 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001428 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001429 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001430 inode->i_data.a_ops = &udf_adinicb_aops;
1431 else
1432 inode->i_data.a_ops = &udf_aops;
1433 inode->i_op = &udf_file_inode_operations;
1434 inode->i_fop = &udf_file_operations;
1435 inode->i_mode |= S_IFREG;
1436 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001437 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001438 inode->i_mode |= S_IFBLK;
1439 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001440 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001441 inode->i_mode |= S_IFCHR;
1442 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001443 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001444 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1445 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001446 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001447 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1448 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001449 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001450 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001451 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001452 inode->i_mode = S_IFLNK | S_IRWXUGO;
1453 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001454 case ICBTAG_FILE_TYPE_MAIN:
1455 udf_debug("METADATA FILE-----\n");
1456 break;
1457 case ICBTAG_FILE_TYPE_MIRROR:
1458 udf_debug("METADATA MIRROR FILE-----\n");
1459 break;
1460 case ICBTAG_FILE_TYPE_BITMAP:
1461 udf_debug("METADATA BITMAP FILE-----\n");
1462 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001463 default:
Joe Perches78ace702011-10-10 01:08:05 -07001464 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1465 inode->i_ino, fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001466 make_bad_inode(inode);
1467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001469 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001470 struct deviceSpec *dsea =
1471 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001472 if (dsea) {
1473 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001474 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1475 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001477 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480}
1481
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001482static int udf_alloc_i_data(struct inode *inode, size_t size)
1483{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001484 struct udf_inode_info *iinfo = UDF_I(inode);
1485 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001486
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001487 if (!iinfo->i_ext.i_data) {
Joe Perches78ace702011-10-10 01:08:05 -07001488 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1489 inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001490 return -ENOMEM;
1491 }
1492
1493 return 0;
1494}
1495
Al Virofaa17292011-07-26 03:18:29 -04001496static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Al Virofaa17292011-07-26 03:18:29 -04001498 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 uint32_t permissions;
1500 uint32_t flags;
1501
1502 permissions = le32_to_cpu(fe->permissions);
1503 flags = le16_to_cpu(fe->icbTag.flags);
1504
Marcin Slusarz4b111112008-02-08 04:20:36 -08001505 mode = ((permissions) & S_IRWXO) |
1506 ((permissions >> 2) & S_IRWXG) |
1507 ((permissions >> 4) & S_IRWXU) |
1508 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1509 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1510 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 return mode;
1513}
1514
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001515int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Jan Kara49521de2010-10-20 17:42:44 +02001517 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Jan Kara49521de2010-10-20 17:42:44 +02001520static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 return udf_update_inode(inode, 1);
1523}
1524
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001525static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct buffer_head *bh = NULL;
1528 struct fileEntry *fe;
1529 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001530 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 uint32_t udfperms;
1532 uint16_t icbflags;
1533 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001535 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001536 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001537 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Jan Kara5833ded2010-01-08 16:52:59 +01001539 bh = udf_tgetblk(inode->i_sb,
1540 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001541 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001542 udf_debug("getblk failure\n");
1543 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
Jan Karaaae917c2010-01-08 16:46:29 +01001546 lock_buffer(bh);
1547 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 fe = (struct fileEntry *)bh->b_data;
1549 efe = (struct extendedFileEntry *)bh->b_data;
1550
Jan Karaaae917c2010-01-08 16:46:29 +01001551 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001553 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001555 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001556 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001557 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001558 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001559 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1560 use->descTag.tagLocation =
1561 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001562 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001563 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001565 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001566 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001567 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001568 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Jan Karaaae917c2010-01-08 16:46:29 +01001570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
Phillip Susi4d6660e2006-03-07 21:55:24 -08001573 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1574 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001575 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001576 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Phillip Susi4d6660e2006-03-07 21:55:24 -08001578 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1579 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001580 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001581 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Marcin Slusarz4b111112008-02-08 04:20:36 -08001583 udfperms = ((inode->i_mode & S_IRWXO)) |
1584 ((inode->i_mode & S_IRWXG) << 2) |
1585 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Marcin Slusarz4b111112008-02-08 04:20:36 -08001587 udfperms |= (le32_to_cpu(fe->permissions) &
1588 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1589 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1590 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 fe->permissions = cpu_to_le32(udfperms);
1592
1593 if (S_ISDIR(inode->i_mode))
1594 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1595 else
1596 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1597
1598 fe->informationLength = cpu_to_le64(inode->i_size);
1599
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001600 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001601 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001602 struct deviceSpec *dsea =
1603 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001604 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001606 udf_add_extendedattr(inode,
1607 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001608 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 dsea->attrType = cpu_to_le32(12);
1610 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001611 dsea->attrLength = cpu_to_le32(
1612 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001613 sizeof(struct regid));
1614 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001616 eid = (struct regid *)dsea->impUse;
1617 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 strcpy(eid->ident, UDF_ID_DEVELOPER);
1619 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1620 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1621 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1622 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1623 }
1624
Steve Nickelb2527bf2012-02-16 12:53:53 -05001625 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1626 lb_recorded = 0; /* No extents => no blocks! */
1627 else
1628 lb_recorded =
1629 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1630 (blocksize_bits - 9);
1631
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001632 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001633 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001634 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001635 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001636 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Marcin Slusarz56774802008-02-10 11:25:31 +01001638 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1639 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1640 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001641 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1643 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1644 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001645 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1646 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1647 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001648 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1650 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001651 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001652 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001653 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001654 inode->i_sb->s_blocksize -
1655 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001657 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001659 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1660 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1661 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1662 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001663
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001664 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1665 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1666 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1667 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001668
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001669 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1670 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1671 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1672 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Marcin Slusarz56774802008-02-10 11:25:31 +01001674 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1675 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1676 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1677 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001679 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1681 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1682 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001683 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1684 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1685 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001686 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1688 crclen = sizeof(struct extendedFileEntry);
1689 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001690 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 fe->icbTag.strategyType = cpu_to_le16(4096);
1692 fe->icbTag.strategyParameter = cpu_to_le16(1);
1693 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001694 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 fe->icbTag.strategyType = cpu_to_le16(4);
1696 fe->icbTag.numEntries = cpu_to_le16(1);
1697 }
1698
1699 if (S_ISDIR(inode->i_mode))
1700 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1701 else if (S_ISREG(inode->i_mode))
1702 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1703 else if (S_ISLNK(inode->i_mode))
1704 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1705 else if (S_ISBLK(inode->i_mode))
1706 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1707 else if (S_ISCHR(inode->i_mode))
1708 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1709 else if (S_ISFIFO(inode->i_mode))
1710 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1711 else if (S_ISSOCK(inode->i_mode))
1712 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1713
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001714 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001715 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1716 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1717 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1718 (le16_to_cpu(fe->icbTag.flags) &
1719 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1720 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001723 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 fe->descTag.descVersion = cpu_to_le16(3);
1725 else
1726 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001727 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001728 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001729 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001730 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001732 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001733 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001734 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Jan Karaaae917c2010-01-08 16:46:29 +01001736out:
Jan Kara5833ded2010-01-08 16:52:59 +01001737 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001738 unlock_buffer(bh);
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /* write the data blocks */
1741 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001742 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001744 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001745 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1746 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 err = -EIO;
1748 }
1749 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001750 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return err;
1753}
1754
Pekka Enberg97e961f2008-10-15 12:29:03 +02001755struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
1757 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1758 struct inode *inode = iget_locked(sb, block);
1759
1760 if (!inode)
1761 return NULL;
1762
1763 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001764 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 __udf_read_inode(inode);
1766 unlock_new_inode(inode);
1767 }
1768
1769 if (is_bad_inode(inode))
1770 goto out_iput;
1771
Pekka Enberg97e961f2008-10-15 12:29:03 +02001772 if (ino->logicalBlockNum >= UDF_SB(sb)->
1773 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001775 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 make_bad_inode(inode);
1777 goto out_iput;
1778 }
1779
1780 return inode;
1781
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001782 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 iput(inode);
1784 return NULL;
1785}
1786
Jan Kara7e49b6f2010-10-22 00:30:26 +02001787int udf_add_aext(struct inode *inode, struct extent_position *epos,
1788 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001791 struct short_ad *sad = NULL;
1792 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct allocExtDesc *aed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001795 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Jan Karaff116fc2007-05-08 00:35:14 -07001797 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001798 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001799 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001800 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 else
Jan Karaff116fc2007-05-08 00:35:14 -07001802 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001804 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001805 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001806 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001807 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001809 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001811 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001812 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct buffer_head *nbh;
1814 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001815 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Marcin Slusarz4b111112008-02-08 04:20:36 -08001817 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1818 obloc.partitionReferenceNum,
1819 obloc.logicalBlockNum, &err);
1820 if (!epos->block.logicalBlockNum)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001821 return -ENOSPC;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001822 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001823 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001824 0));
1825 if (!nbh)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001826 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 lock_buffer(nbh);
1828 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1829 set_buffer_uptodate(nbh);
1830 unlock_buffer(nbh);
1831 mark_buffer_dirty_inode(nbh, inode);
1832
1833 aed = (struct allocExtDesc *)(nbh->b_data);
1834 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001835 aed->previousAllocExtLocation =
1836 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001837 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001838 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 aed->lengthAllocDescs = cpu_to_le32(adsize);
1840 sptr = ptr - adsize;
1841 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1842 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001843 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001844 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001845 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 aed->lengthAllocDescs = cpu_to_le32(0);
1847 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001848 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001850 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001851 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001852 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001853 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001854 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 mark_inode_dirty(inode);
1856 }
1857 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001858 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001860 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 else
1862 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001863 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001864 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001865 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001866 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001867 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1868 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001869 sad->extPosition =
1870 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001871 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001872 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001873 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001874 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1875 inode->i_sb->s_blocksize);
1876 lad->extLocation = cpu_to_lelb(epos->block);
1877 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1878 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001880 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001881 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001882 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001883 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001885 udf_update_tag(epos->bh->b_data,
1886 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001887 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001888 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001889 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001891 }
Jan Karaff116fc2007-05-08 00:35:14 -07001892 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
Jan Kara7e49b6f2010-10-22 00:30:26 +02001895 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001897 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001898 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001900 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001901 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001902 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001903 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1904 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1905 udf_update_tag(epos->bh->b_data,
1906 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001908 udf_update_tag(epos->bh->b_data,
1909 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001910 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
Jan Kara7e49b6f2010-10-22 00:30:26 +02001913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Jan Kara7e49b6f2010-10-22 00:30:26 +02001916void udf_write_aext(struct inode *inode, struct extent_position *epos,
1917 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 int adsize;
1920 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001921 struct short_ad *sad;
1922 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001923 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Jan Karaff116fc2007-05-08 00:35:14 -07001925 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001926 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001927 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001928 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 else
Jan Karaff116fc2007-05-08 00:35:14 -07001930 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001932 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001933 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001934 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001935 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001936 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001937 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001938 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001939 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001940 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001941 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001942 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001943 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001944 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001945 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001946 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001947 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001950 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001951 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001952 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001953 struct allocExtDesc *aed =
1954 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001955 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001956 le32_to_cpu(aed->lengthAllocDescs) +
1957 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
Jan Karaff116fc2007-05-08 00:35:14 -07001959 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001960 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001965 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Marcin Slusarz4b111112008-02-08 04:20:36 -08001968int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001969 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 int8_t etype;
1972
Jan Karaff116fc2007-05-08 00:35:14 -07001973 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001974 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001975 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001976 epos->block = *eloc;
1977 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001978 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001979 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001980 epos->bh = udf_tread(inode->i_sb, block);
1981 if (!epos->bh) {
1982 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return -1;
1984 }
1985 }
1986
1987 return etype;
1988}
1989
Marcin Slusarz4b111112008-02-08 04:20:36 -08001990int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001991 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 int alen;
1994 int8_t etype;
1995 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001996 struct short_ad *sad;
1997 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001998 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001999
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002000 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002001 if (!epos->offset)
2002 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002003 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002004 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002005 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002006 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002007 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002008 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002009 if (!epos->offset)
2010 epos->offset = sizeof(struct allocExtDesc);
2011 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002012 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002013 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2014 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002017 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002018 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002019 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2020 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002022 etype = le32_to_cpu(sad->extLength) >> 30;
2023 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002024 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002025 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002026 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2027 break;
2028 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002029 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2030 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002031 return -1;
2032 etype = le32_to_cpu(lad->extLength) >> 30;
2033 *eloc = lelb_to_cpu(lad->extLocation);
2034 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2035 break;
2036 default:
Joe Perchesa983f362011-10-10 01:08:07 -07002037 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002038 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040
2041 return etype;
2042}
2043
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002044static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002045 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002047 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 uint32_t oelen;
2049 int8_t etype;
2050
Jan Karaff116fc2007-05-08 00:35:14 -07002051 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002052 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002054 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002055 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 neloc = oeloc;
2057 nelen = (etype << 30) | oelen;
2058 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002059 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002060 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return (nelen >> 30);
2063}
2064
Marcin Slusarz4b111112008-02-08 04:20:36 -08002065int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002066 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Jan Karaff116fc2007-05-08 00:35:14 -07002068 struct extent_position oepos;
2069 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 int8_t etype;
2071 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002072 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002074 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002075 get_bh(epos.bh);
2076 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002079 iinfo = UDF_I(inode);
2080 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002081 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002082 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002083 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 else
2085 adsize = 0;
2086
Jan Karaff116fc2007-05-08 00:35:14 -07002087 oepos = epos;
2088 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return -1;
2090
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002091 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002092 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002093 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002094 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002095 brelse(oepos.bh);
2096 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002097 oepos.bh = epos.bh;
2098 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002101 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 elen = 0;
2103
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002104 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002105 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2106 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2107 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002108 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002109 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002111 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002112 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002113 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002114 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002115 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002116 udf_update_tag(oepos.bh->b_data,
2117 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002119 udf_update_tag(oepos.bh->b_data,
2120 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002121 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002123 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002124 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002125 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002126 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002128 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002129 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002130 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002131 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002132 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002133 udf_update_tag(oepos.bh->b_data,
2134 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002136 udf_update_tag(oepos.bh->b_data,
2137 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002138 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002141
Jan Kara3bf25cb2007-05-08 00:35:16 -07002142 brelse(epos.bh);
2143 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return (elen >> 30);
2146}
2147
Marcin Slusarz4b111112008-02-08 04:20:36 -08002148int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002149 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002150 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002152 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002153 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002154 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002156 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002158 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002159 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002160 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002161 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002164 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002165 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2166 if (etype == -1) {
2167 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002168 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return -1;
2170 }
2171 lbcount += *elen;
2172 } while (lbcount <= bcount);
2173
Marcin Slusarz4b111112008-02-08 04:20:36 -08002174 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 return etype;
2177}
2178
Jan Kara60448b12007-05-08 00:35:13 -07002179long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002181 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002182 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002183 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002184 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int ret;
2186
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002187 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Marcin Slusarz4b111112008-02-08 04:20:36 -08002189 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2190 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002191 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 else
2193 ret = 0;
2194
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002195 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002196 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2199 return udf_fixed_to_variable(ret);
2200 else
2201 return ret;
2202}