blob: dacd8f4cea8a494d3167d7b3ed73848752b78ab0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * namei.c
3 *
4 * PURPOSE
5 * Inode name 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-2004 Ben Fennema
14 * (C) 1999-2000 Stelias Computing Inc
15 *
16 * HISTORY
17 *
18 * 12/12/98 blf Created. Split out the lookup code from dir.c
19 * 04/19/99 blf link, mknod, symlink support
20 */
21
22#include "udfdecl.h"
23
24#include "udf_i.h"
25#include "udf_sb.h"
26#include <linux/string.h>
27#include <linux/errno.h>
28#include <linux/mm.h>
29#include <linux/slab.h>
30#include <linux/quotaops.h>
31#include <linux/smp_lock.h>
32#include <linux/buffer_head.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040033#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070035static inline int udf_match(int len1, const char *name1, int len2,
36 const char *name2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 if (len1 != len2)
39 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return !memcmp(name1, name2, len1);
42}
43
44int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070045 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
Marcin Slusarz4b111112008-02-08 04:20:36 -080046 uint8_t *impuse, uint8_t *fileident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
49 uint16_t crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int offset;
51 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
52 uint8_t lfi = cfi->lengthFileIdent;
53 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070054 sizeof(struct fileIdentDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int adinicb = 0;
56
Marcin Slusarzc0b34432008-02-08 04:20:42 -080057 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 adinicb = 1;
59
60 offset = fibh->soffset + sizeof(struct fileIdentDesc);
61
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070062 if (impuse) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070063 if (adinicb || (offset + liu < 0)) {
64 memcpy((uint8_t *)sfi->impUse, impuse, liu);
65 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 memcpy(fibh->ebh->b_data + offset, impuse, liu);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070067 } else {
68 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
Marcin Slusarz4b111112008-02-08 04:20:36 -080069 memcpy(fibh->ebh->b_data, impuse - offset,
70 liu + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72 }
73
74 offset += liu;
75
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070076 if (fileident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070077 if (adinicb || (offset + lfi < 0)) {
78 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
79 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070081 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -080082 memcpy((uint8_t *)sfi->fileIdent + liu, fileident,
83 -offset);
84 memcpy(fibh->ebh->b_data, fileident - offset,
85 lfi + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87 }
88
89 offset += lfi;
90
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070091 if (adinicb || (offset + padlen < 0)) {
92 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
93 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 memset(fibh->ebh->b_data + offset, 0x00, padlen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070095 } else {
96 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 memset(fibh->ebh->b_data, 0x00, padlen + offset);
98 }
99
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700100 crc = udf_crc((uint8_t *)cfi + sizeof(tag),
101 sizeof(struct fileIdentDesc) - sizeof(tag), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700103 if (fibh->sbh == fibh->ebh) {
104 crc = udf_crc((uint8_t *)sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800105 crclen + sizeof(tag) -
106 sizeof(struct fileIdentDesc), crc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700107 } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800108 crc = udf_crc(fibh->ebh->b_data +
109 sizeof(struct fileIdentDesc) +
110 fibh->soffset,
111 crclen + sizeof(tag) -
112 sizeof(struct fileIdentDesc),
113 crc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700114 } else {
115 crc = udf_crc((uint8_t *)sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800116 -fibh->soffset - sizeof(struct fileIdentDesc),
117 crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
119 }
120
121 cfi->descTag.descCRC = cpu_to_le16(crc);
122 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
Marcin Slusarz3f2587b2008-02-08 04:20:39 -0800123 cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700125 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800126 memcpy((uint8_t *)sfi, (uint8_t *)cfi,
127 sizeof(struct fileIdentDesc));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700128 } else {
129 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
130 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700131 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700134 if (adinicb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700136 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (fibh->sbh != fibh->ebh)
138 mark_buffer_dirty_inode(fibh->ebh, inode);
139 mark_buffer_dirty_inode(fibh->sbh, inode);
140 }
141 return 0;
142}
143
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700144static struct fileIdentDesc *udf_find_entry(struct inode *dir,
145 struct dentry *dentry,
146 struct udf_fileident_bh *fibh,
147 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700149 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 loff_t f_pos;
151 int block, flen;
152 char fname[UDF_NAME_LEN];
153 char *nameptr;
154 uint8_t lfi;
155 uint16_t liu;
KAMBAROV, ZAURec471dc2005-06-28 20:45:10 -0700156 loff_t size;
Jan Karaff116fc2007-05-08 00:35:14 -0700157 kernel_lb_addr eloc;
158 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700159 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700160 struct extent_position epos = {};
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800161 struct udf_inode_info *dinfo = UDF_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jan Karaaf793292008-02-08 04:20:50 -0800163 size = udf_ext0_offset(dir) + dir->i_size;
164 f_pos = udf_ext0_offset(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jan Karaaf793292008-02-08 04:20:50 -0800166 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800167 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 fibh->sbh = fibh->ebh = NULL;
Jan Karaaf793292008-02-08 04:20:50 -0800169 else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800170 &epos, &eloc, &elen, &offset) ==
171 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700173 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800174 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700175 epos.offset -= sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800176 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700177 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800178 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 offset = 0;
180
Marcin Slusarz4b111112008-02-08 04:20:36 -0800181 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
182 if (!fibh->sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700183 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return NULL;
185 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700186 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700187 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return NULL;
189 }
190
Jan Karaaf793292008-02-08 04:20:50 -0800191 while (f_pos < size) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700192 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
193 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700194 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700196 brelse(fibh->ebh);
197 brelse(fibh->sbh);
198 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return NULL;
200 }
201
202 liu = le16_to_cpu(cfi->lengthOfImpUse);
203 lfi = cfi->lengthFileIdent;
204
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700205 if (fibh->sbh == fibh->ebh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 nameptr = fi->fileIdent + liu;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700207 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int poffset; /* Unpaded ending offset */
209
Marcin Slusarz4b111112008-02-08 04:20:36 -0800210 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
211 liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Marcin Slusarz4b111112008-02-08 04:20:36 -0800213 if (poffset >= lfi)
214 nameptr = (uint8_t *)(fibh->ebh->b_data +
215 poffset - lfi);
216 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 nameptr = fname;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800218 memcpy(nameptr, fi->fileIdent + liu,
219 lfi - poffset);
220 memcpy(nameptr + lfi - poffset,
221 fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
224
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700225 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
226 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 continue;
228 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700229
230 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
231 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 continue;
233 }
234
235 if (!lfi)
236 continue;
237
Marcin Slusarz4b111112008-02-08 04:20:36 -0800238 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
239 if (flen && udf_match(flen, fname, dentry->d_name.len,
240 dentry->d_name.name)) {
241 brelse(epos.bh);
242 return fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700247 brelse(fibh->ebh);
248 brelse(fibh->sbh);
249 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return NULL;
252}
253
254/*
255 * udf_lookup
256 *
257 * PURPOSE
258 * Look-up the inode for a given name.
259 *
260 * DESCRIPTION
261 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
262 * available for a directory. The filesystem is useless if this routine is
263 * not available for at least the filesystem's root directory.
264 *
265 * This routine is passed an incomplete dentry - it must be completed by
266 * calling d_add(dentry, inode). If the name does not exist, then the
267 * specified inode must be set to null. An error should only be returned
268 * when the lookup fails for a reason other than the name not existing.
269 * Note that the directory inode semaphore is held during the call.
270 *
271 * Refer to lookup_dentry() in fs/namei.c
272 * lookup_dentry() -> lookup() -> real_lookup() -> .
273 *
274 * PRE-CONDITIONS
275 * dir Pointer to inode of parent directory.
276 * dentry Pointer to dentry to complete.
277 * nd Pointer to lookup nameidata
278 *
279 * POST-CONDITIONS
280 * <return> Zero on success.
281 *
282 * HISTORY
283 * July 1, 1997 - Andrew E. Mileski
284 * Written, tested, and released.
285 */
286
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700287static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
288 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct inode *inode = NULL;
Jayachandran Cdb9a3692006-02-03 03:04:50 -0800291 struct fileIdentDesc cfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct udf_fileident_bh fibh;
293
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294 if (dentry->d_name.len > UDF_NAME_LEN - 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return ERR_PTR(-ENAMETOOLONG);
296
297 lock_kernel();
298#ifdef UDF_RECOVERY
299 /* temporary shorthand for specifying files by inode number */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700300 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700301 kernel_lb_addr lb = {
302 .logicalBlockNum = 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800303 .partitionReferenceNum =
304 simple_strtoul(dentry->d_name.name + 3,
305 NULL, 0),
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 inode = udf_iget(dir->i_sb, lb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700308 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 unlock_kernel();
310 return ERR_PTR(-EACCES);
311 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800312 } else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700313#endif /* UDF_RECOVERY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700315 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700317 brelse(fibh.ebh);
318 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700321 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 unlock_kernel();
323 return ERR_PTR(-EACCES);
324 }
325 }
326 unlock_kernel();
327 d_add(dentry, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return NULL;
330}
331
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700332static struct fileIdentDesc *udf_add_entry(struct inode *dir,
333 struct dentry *dentry,
334 struct udf_fileident_bh *fibh,
335 struct fileIdentDesc *cfi, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800337 struct super_block *sb = dir->i_sb;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700338 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
340 int namelen;
341 loff_t f_pos;
342 int flen;
343 char *nameptr;
Jan Karaaf793292008-02-08 04:20:50 -0800344 loff_t size = udf_ext0_offset(dir) + dir->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int nfidlen;
346 uint8_t lfi;
347 uint16_t liu;
348 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700349 kernel_lb_addr eloc;
350 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700351 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700352 struct extent_position epos = {};
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800353 struct udf_inode_info *dinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700355 if (dentry) {
356 if (!dentry->d_name.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 *err = -EINVAL;
358 return NULL;
359 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800360 namelen = udf_put_filename(sb, dentry->d_name.name, name,
361 dentry->d_name.len);
362 if (!namelen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *err = -ENAMETOOLONG;
364 return NULL;
365 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700366 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 namelen = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
371
Jan Karaaf793292008-02-08 04:20:50 -0800372 f_pos = udf_ext0_offset(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Jan Karaaf793292008-02-08 04:20:50 -0800374 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800375 dinfo = UDF_I(dir);
376 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 fibh->sbh = fibh->ebh = NULL;
Jan Karaaf793292008-02-08 04:20:50 -0800378 else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800379 &epos, &eloc, &elen, &offset) ==
380 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700382 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800383 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700384 epos.offset -= sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800385 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700386 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800387 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 offset = 0;
389
Marcin Slusarz4b111112008-02-08 04:20:36 -0800390 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
391 if (!fibh->sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700392 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *err = -EIO;
394 return NULL;
395 }
396
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800397 block = dinfo->i_location.logicalBlockNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700399 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800400 block = udf_get_lb_pblock(dir->i_sb, dinfo->i_location, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 fibh->sbh = fibh->ebh = NULL;
402 fibh->soffset = fibh->eoffset = sb->s_blocksize;
403 goto add;
404 }
405
Jan Karaaf793292008-02-08 04:20:50 -0800406 while (f_pos < size) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700407 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
408 &elen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700410 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700412 brelse(fibh->ebh);
413 brelse(fibh->sbh);
414 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *err = -EIO;
416 return NULL;
417 }
418
419 liu = le16_to_cpu(cfi->lengthOfImpUse);
420 lfi = cfi->lengthFileIdent;
421
Marcin Slusarz4b111112008-02-08 04:20:36 -0800422 if (fibh->sbh == fibh->ebh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 nameptr = fi->fileIdent + liu;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800424 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int poffset; /* Unpaded ending offset */
426
Marcin Slusarz4b111112008-02-08 04:20:36 -0800427 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
428 liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Marcin Slusarz4b111112008-02-08 04:20:36 -0800430 if (poffset >= lfi)
431 nameptr = (char *)(fibh->ebh->b_data +
432 poffset - lfi);
433 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 nameptr = fname;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800435 memcpy(nameptr, fi->fileIdent + liu,
436 lfi - poffset);
437 memcpy(nameptr + lfi - poffset,
438 fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 }
441
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700442 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800443 if (((sizeof(struct fileIdentDesc) +
444 liu + lfi + 3) & ~3) == nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700445 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 cfi->descTag.tagSerialNum = cpu_to_le16(1);
447 cfi->fileVersionNum = cpu_to_le16(1);
448 cfi->fileCharacteristics = 0;
449 cfi->lengthFileIdent = namelen;
450 cfi->lengthOfImpUse = cpu_to_le16(0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800451 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
452 name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return fi;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800454 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *err = -EIO;
456 return NULL;
457 }
458 }
459 }
460
461 if (!lfi || !dentry)
462 continue;
463
Marcin Slusarz4b111112008-02-08 04:20:36 -0800464 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
465 if (flen && udf_match(flen, fname, dentry->d_name.len,
466 dentry->d_name.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700468 brelse(fibh->ebh);
469 brelse(fibh->sbh);
470 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *err = -EEXIST;
472 return NULL;
473 }
474 }
475
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700476add:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 f_pos += nfidlen;
478
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800479 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700480 sb->s_blocksize - fibh->eoffset < nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700481 brelse(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700482 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 fibh->soffset -= udf_ext0_offset(dir);
484 fibh->eoffset -= udf_ext0_offset(dir);
Jan Karaaf793292008-02-08 04:20:50 -0800485 f_pos -= udf_ext0_offset(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700487 brelse(fibh->ebh);
488 brelse(fibh->sbh);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800489 fibh->sbh = fibh->ebh =
490 udf_expand_dir_adinicb(dir, &block, err);
491 if (!fibh->sbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800493 epos.block = dinfo->i_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 eloc.logicalBlockNum = block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800495 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800496 dinfo->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 elen = dir->i_sb->s_blocksize;
Jan Karaff116fc2007-05-08 00:35:14 -0700498 epos.offset = udf_file_entry_alloc_offset(dir);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800499 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700500 epos.offset += sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800501 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700502 epos.offset += sizeof(long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700505 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 fibh->soffset = fibh->eoffset;
507 fibh->eoffset += nfidlen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700508 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700509 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 fibh->sbh = fibh->ebh;
511 }
512
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800513 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
514 block = dinfo->i_location.logicalBlockNum;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800515 fi = (struct fileIdentDesc *)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800516 (dinfo->i_ext.i_data +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800517 fibh->soffset -
Marcin Slusarz4b111112008-02-08 04:20:36 -0800518 udf_ext0_offset(dir) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800519 dinfo->i_lenEAttr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700520 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800521 block = eloc.logicalBlockNum +
522 ((elen - 1) >>
523 dir->i_sb->s_blocksize_bits);
524 fi = (struct fileIdentDesc *)
525 (fibh->sbh->b_data + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700527 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 fibh->soffset = fibh->eoffset - sb->s_blocksize;
529 fibh->eoffset += nfidlen - sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700530 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700531 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 fibh->sbh = fibh->ebh;
533 }
534
535 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700536 dir->i_sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800537 fibh->ebh = udf_bread(dir,
Jan Karaaf793292008-02-08 04:20:50 -0800538 f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700539 if (!fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700540 brelse(epos.bh);
541 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return NULL;
543 }
544
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700545 if (!fibh->soffset) {
Jan Karaff116fc2007-05-08 00:35:14 -0700546 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549 dir->i_sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800550 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700551 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jan Kara3bf25cb2007-05-08 00:35:16 -0700553 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 fibh->sbh = fibh->ebh;
555 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700556 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 fi = (struct fileIdentDesc *)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800558 (fibh->sbh->b_data + sb->s_blocksize +
559 fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 }
562
563 memset(cfi, 0, sizeof(struct fileIdentDesc));
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800564 if (UDF_SB(sb)->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800565 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
566 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800568 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
569 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 cfi->fileVersionNum = cpu_to_le16(1);
571 cfi->lengthFileIdent = namelen;
572 cfi->lengthOfImpUse = cpu_to_le16(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700573 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700574 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 dir->i_size += nfidlen;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800576 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
577 dinfo->i_lenAlloc += nfidlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 mark_inode_dirty(dir);
579 return fi;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700580 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700581 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700583 brelse(fibh->ebh);
584 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *err = -EIO;
586 return NULL;
587 }
588}
589
590static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700591 struct udf_fileident_bh *fibh,
592 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
597 memset(&(cfi->icb), 0x00, sizeof(long_ad));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
600}
601
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700602static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
603 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct udf_fileident_bh fibh;
606 struct inode *inode;
607 struct fileIdentDesc cfi, *fi;
608 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800609 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 lock_kernel();
612 inode = udf_new_inode(dir, mode, &err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 unlock_kernel();
615 return err;
616 }
617
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800618 iinfo = UDF_I(inode);
619 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 inode->i_data.a_ops = &udf_adinicb_aops;
621 else
622 inode->i_data.a_ops = &udf_aops;
623 inode->i_op = &udf_file_inode_operations;
624 inode->i_fop = &udf_file_operations;
625 inode->i_mode = mode;
626 mark_inode_dirty(inode);
627
Marcin Slusarz4b111112008-02-08 04:20:36 -0800628 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
629 if (!fi) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700630 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 mark_inode_dirty(inode);
632 iput(inode);
633 unlock_kernel();
634 return err;
635 }
636 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800637 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700638 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800639 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800641 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700644 brelse(fibh.ebh);
645 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 unlock_kernel();
647 d_instantiate(dentry, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650}
651
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700652static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
653 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700655 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct udf_fileident_bh fibh;
657 struct fileIdentDesc cfi, *fi;
658 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800659 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (!old_valid_dev(rdev))
662 return -EINVAL;
663
664 lock_kernel();
665 err = -EIO;
666 inode = udf_new_inode(dir, mode, &err);
667 if (!inode)
668 goto out;
669
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800670 iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 inode->i_uid = current->fsuid;
672 init_special_inode(inode, mode, rdev);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800673 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
674 if (!fi) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700675 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 mark_inode_dirty(inode);
677 iput(inode);
678 unlock_kernel();
679 return err;
680 }
681 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800682 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700683 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800684 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800686 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 mark_inode_dirty(inode);
689
690 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700691 brelse(fibh.ebh);
692 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 d_instantiate(dentry, inode);
694 err = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700695
696out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 unlock_kernel();
698 return err;
699}
700
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700701static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700703 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct udf_fileident_bh fibh;
705 struct fileIdentDesc cfi, *fi;
706 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800707 struct udf_inode_info *dinfo = UDF_I(dir);
708 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 lock_kernel();
711 err = -EMLINK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto out;
714
715 err = -EIO;
716 inode = udf_new_inode(dir, S_IFDIR, &err);
717 if (!inode)
718 goto out;
719
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800720 iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 inode->i_op = &udf_dir_inode_operations;
722 inode->i_fop = &udf_dir_operations;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800723 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
724 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 inode->i_nlink--;
726 mark_inode_dirty(inode);
727 iput(inode);
728 goto out;
729 }
730 inode->i_nlink = 2;
731 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800732 cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700733 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800734 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800735 cfi.fileCharacteristics =
736 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700738 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 inode->i_mode = S_IFDIR | mode;
740 if (dir->i_mode & S_ISGID)
741 inode->i_mode |= S_ISGID;
742 mark_inode_dirty(inode);
743
Marcin Slusarz4b111112008-02-08 04:20:36 -0800744 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
745 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 inode->i_nlink = 0;
747 mark_inode_dirty(inode);
748 iput(inode);
749 goto out;
750 }
751 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800752 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700753 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800754 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
756 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Dave Hansend8c76e62006-09-30 23:29:04 -0700757 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 mark_inode_dirty(dir);
759 d_instantiate(dentry, inode);
760 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700761 brelse(fibh.ebh);
762 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 err = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700764
765out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 unlock_kernel();
767 return err;
768}
769
770static int empty_dir(struct inode *dir)
771{
772 struct fileIdentDesc *fi, cfi;
773 struct udf_fileident_bh fibh;
774 loff_t f_pos;
Jan Karaaf793292008-02-08 04:20:50 -0800775 loff_t size = udf_ext0_offset(dir) + dir->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700777 kernel_lb_addr eloc;
778 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700779 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700780 struct extent_position epos = {};
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800781 struct udf_inode_info *dinfo = UDF_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jan Karaaf793292008-02-08 04:20:50 -0800783 f_pos = udf_ext0_offset(dir);
784 fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800786 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 fibh.sbh = fibh.ebh = NULL;
Jan Karaaf793292008-02-08 04:20:50 -0800788 else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800789 &epos, &eloc, &elen, &offset) ==
790 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800793 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700794 epos.offset -= sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800795 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700796 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800797 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 offset = 0;
799
Marcin Slusarz4b111112008-02-08 04:20:36 -0800800 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
801 if (!fibh.sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700802 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700805 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700806 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808 }
809
Jan Karaaf793292008-02-08 04:20:50 -0800810 while (f_pos < size) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700811 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
812 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700813 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700815 brelse(fibh.ebh);
816 brelse(fibh.sbh);
817 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return 0;
819 }
820
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700821 if (cfi.lengthFileIdent &&
822 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700824 brelse(fibh.ebh);
825 brelse(fibh.sbh);
826 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
828 }
829 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700832 brelse(fibh.ebh);
833 brelse(fibh.sbh);
834 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 1;
837}
838
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700839static int udf_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700842 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct udf_fileident_bh fibh;
844 struct fileIdentDesc *fi, cfi;
845 kernel_lb_addr tloc;
846
847 retval = -ENOENT;
848 lock_kernel();
849 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
850 if (!fi)
851 goto out;
852
853 retval = -EIO;
854 tloc = lelb_to_cpu(cfi.icb.extLocation);
855 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
856 goto end_rmdir;
857 retval = -ENOTEMPTY;
858 if (!empty_dir(inode))
859 goto end_rmdir;
860 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
861 if (retval)
862 goto end_rmdir;
863 if (inode->i_nlink != 2)
864 udf_warning(inode->i_sb, "udf_rmdir",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700865 "empty directory has nlink != 2 (%d)",
866 inode->i_nlink);
Dave Hansence71ec32006-09-30 23:29:06 -0700867 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 inode->i_size = 0;
Stephen Mollettc007c062007-05-08 00:31:31 -0700869 inode_dec_link_count(dir);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800870 inode->i_ctime = dir->i_ctime = dir->i_mtime =
871 current_fs_time(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 mark_inode_dirty(dir);
873
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700874end_rmdir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700876 brelse(fibh.ebh);
877 brelse(fibh.sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700878
879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unlock_kernel();
881 return retval;
882}
883
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700884static int udf_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700887 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct udf_fileident_bh fibh;
889 struct fileIdentDesc *fi;
890 struct fileIdentDesc cfi;
891 kernel_lb_addr tloc;
892
893 retval = -ENOENT;
894 lock_kernel();
895 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
896 if (!fi)
897 goto out;
898
899 retval = -EIO;
900 tloc = lelb_to_cpu(cfi.icb.extLocation);
901 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
902 goto end_unlink;
903
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700904 if (!inode->i_nlink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 udf_debug("Deleting nonexistent file (%lu), %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700906 inode->i_ino, inode->i_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 inode->i_nlink = 1;
908 }
909 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
910 if (retval)
911 goto end_unlink;
912 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
913 mark_inode_dirty(dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700914 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 inode->i_ctime = dir->i_ctime;
916 retval = 0;
917
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700918end_unlink:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700920 brelse(fibh.ebh);
921 brelse(fibh.sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700922
923out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 unlock_kernel();
925 return retval;
926}
927
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700928static int udf_symlink(struct inode *dir, struct dentry *dentry,
929 const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700931 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct pathComponent *pc;
933 char *compstart;
934 struct udf_fileident_bh fibh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700935 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int eoffset, elen = 0;
937 struct fileIdentDesc *fi;
938 struct fileIdentDesc cfi;
939 char *ea;
940 int err;
941 int block;
942 char name[UDF_NAME_LEN];
943 int namelen;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800944 struct buffer_head *bh;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800945 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 lock_kernel();
Marcin Slusarz4b111112008-02-08 04:20:36 -0800948 inode = udf_new_inode(dir, S_IFLNK, &err);
949 if (!inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto out;
951
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800952 iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 inode->i_mode = S_IFLNK | S_IRWXUGO;
954 inode->i_data.a_ops = &udf_symlink_aops;
955 inode->i_op = &page_symlink_inode_operations;
956
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800957 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
Jan Karaff116fc2007-05-08 00:35:14 -0700958 kernel_lb_addr eloc;
959 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800962 iinfo->i_location.partitionReferenceNum,
963 iinfo->i_location.logicalBlockNum, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!block)
965 goto out_no_entry;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800966 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700967 epos.offset = udf_file_entry_alloc_offset(inode);
968 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 eloc.logicalBlockNum = block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800970 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800971 iinfo->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 elen = inode->i_sb->s_blocksize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800973 iinfo->i_lenExtents = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700974 udf_add_aext(inode, &epos, eloc, elen, 0);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700975 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 block = udf_get_pblock(inode->i_sb, block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800978 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800979 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700980 epos.bh = udf_tread(inode->i_sb, block);
981 lock_buffer(epos.bh);
982 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
983 set_buffer_uptodate(epos.bh);
984 unlock_buffer(epos.bh);
985 mark_buffer_dirty_inode(epos.bh, inode);
986 ea = epos.bh->b_data + udf_ext0_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800987 } else
988 ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
991 pc = (struct pathComponent *)ea;
992
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700993 if (*symname == '/') {
994 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 symname++;
996 } while (*symname == '/');
997
998 pc->componentType = 1;
999 pc->lengthComponentIdent = 0;
1000 pc->componentFileVersionNum = 0;
1001 pc += sizeof(struct pathComponent);
1002 elen += sizeof(struct pathComponent);
1003 }
1004
1005 err = -ENAMETOOLONG;
1006
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001007 while (*symname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (elen + sizeof(struct pathComponent) > eoffset)
1009 goto out_no_entry;
1010
1011 pc = (struct pathComponent *)(ea + elen);
1012
1013 compstart = (char *)symname;
1014
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001015 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 symname++;
1017 } while (*symname && *symname != '/');
1018
1019 pc->componentType = 5;
1020 pc->lengthComponentIdent = 0;
1021 pc->componentFileVersionNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001022 if (compstart[0] == '.') {
1023 if ((symname - compstart) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 pc->componentType = 4;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001025 else if ((symname - compstart) == 2 &&
1026 compstart[1] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 pc->componentType = 3;
1028 }
1029
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001030 if (pc->componentType == 5) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001031 namelen = udf_put_filename(inode->i_sb, compstart, name,
1032 symname - compstart);
1033 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto out_no_entry;
1035
Marcin Slusarz4b111112008-02-08 04:20:36 -08001036 if (elen + sizeof(struct pathComponent) + namelen >
1037 eoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 goto out_no_entry;
1039 else
1040 pc->lengthComponentIdent = namelen;
1041
1042 memcpy(pc->componentIdent, name, namelen);
1043 }
1044
1045 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1046
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001047 if (*symname) {
1048 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 symname++;
1050 } while (*symname == '/');
1051 }
1052 }
1053
Jan Kara3bf25cb2007-05-08 00:35:16 -07001054 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 inode->i_size = elen;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001056 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1057 iinfo->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 mark_inode_dirty(inode);
1059
Marcin Slusarz4b111112008-02-08 04:20:36 -08001060 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1061 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto out_no_entry;
1063 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001064 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001065 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1066 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001067 struct logicalVolIntegrityDesc *lvid =
1068 (struct logicalVolIntegrityDesc *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct logicalVolHeaderDesc *lvhd;
1070 uint64_t uniqueID;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001071 lvhd = (struct logicalVolHeaderDesc *)
1072 lvid->logicalVolContentsUse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001074 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1075 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1077 uniqueID += 16;
1078 lvhd->uniqueID = cpu_to_le64(uniqueID);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001079 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001082 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001085 brelse(fibh.ebh);
1086 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 d_instantiate(dentry, inode);
1088 err = 0;
1089
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001090out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 unlock_kernel();
1092 return err;
1093
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001094out_no_entry:
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001095 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 iput(inode);
1097 goto out;
1098}
1099
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001100static int udf_link(struct dentry *old_dentry, struct inode *dir,
1101 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct inode *inode = old_dentry->d_inode;
1104 struct udf_fileident_bh fibh;
1105 struct fileIdentDesc cfi, *fi;
1106 int err;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001107 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001110 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 unlock_kernel();
1112 return -EMLINK;
1113 }
1114
Marcin Slusarz4b111112008-02-08 04:20:36 -08001115 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1116 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 unlock_kernel();
1118 return err;
1119 }
1120 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001121 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001122 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1123 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001124 struct logicalVolIntegrityDesc *lvid =
1125 (struct logicalVolIntegrityDesc *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct logicalVolHeaderDesc *lvhd;
1127 uint64_t uniqueID;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001128 lvhd = (struct logicalVolHeaderDesc *)
1129 (lvid->logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001131 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1132 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1134 uniqueID += 16;
1135 lvhd->uniqueID = cpu_to_le64(uniqueID);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001136 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001139 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 mark_inode_dirty(dir);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001143 brelse(fibh.ebh);
1144 brelse(fibh.sbh);
Dave Hansend8c76e62006-09-30 23:29:04 -07001145 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 inode->i_ctime = current_fs_time(inode->i_sb);
1147 mark_inode_dirty(inode);
1148 atomic_inc(&inode->i_count);
1149 d_instantiate(dentry, inode);
1150 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 0;
1153}
1154
1155/* Anybody can rename anything with this: the permission checks are left to the
1156 * higher-level routines.
1157 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001158static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1159 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001161 struct inode *old_inode = old_dentry->d_inode;
1162 struct inode *new_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct udf_fileident_bh ofibh, nfibh;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001164 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1165 struct fileIdentDesc ocfi, ncfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct buffer_head *dir_bh = NULL;
1167 int retval = -ENOENT;
1168 kernel_lb_addr tloc;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001169 struct udf_inode_info *old_iinfo = UDF_I(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 lock_kernel();
Marcin Slusarz4b111112008-02-08 04:20:36 -08001172 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1173 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001175 brelse(ofibh.ebh);
1176 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1179 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001180 != old_inode->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 goto end_rename;
1182
1183 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001184 if (nfi) {
1185 if (!new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001187 brelse(nfibh.ebh);
1188 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 nfi = NULL;
1190 }
1191 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001192 if (S_ISDIR(old_inode->i_mode)) {
Marcin Slusarz7f3fbd02008-02-08 04:20:49 -08001193 int offset = udf_ext0_offset(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001195 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 retval = -ENOTEMPTY;
1197 if (!empty_dir(new_inode))
1198 goto end_rename;
1199 }
1200 retval = -EIO;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001201 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001202 dir_fi = udf_get_fileident(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001203 old_iinfo->i_ext.i_data -
1204 (old_iinfo->i_efe ?
Marcin Slusarz4b111112008-02-08 04:20:36 -08001205 sizeof(struct extendedFileEntry) :
1206 sizeof(struct fileEntry)),
1207 old_inode->i_sb->s_blocksize, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001208 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1210 if (!dir_bh)
1211 goto end_rename;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001212 dir_fi = udf_get_fileident(dir_bh->b_data,
1213 old_inode->i_sb->s_blocksize, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 if (!dir_fi)
1216 goto end_rename;
1217 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001218 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1219 old_dir->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto end_rename;
1221
1222 retval = -EMLINK;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001223 if (!new_inode &&
1224 new_dir->i_nlink >=
1225 (256 << sizeof(new_dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 goto end_rename;
1227 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001228 if (!nfi) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001229 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1230 &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (!nfi)
1232 goto end_rename;
1233 }
1234
1235 /*
1236 * Like most other Unix systems, set the ctime for inodes on a
1237 * rename.
1238 */
1239 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1240 mark_inode_dirty(old_inode);
1241
1242 /*
1243 * ok, that's it
1244 */
1245 ncfi.fileVersionNum = ocfi.fileVersionNum;
1246 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1247 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1248 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1249
1250 /* The old fid may have moved - find it again */
1251 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1252 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1253
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001254 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001256 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1259 mark_inode_dirty(old_dir);
1260
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001261 if (dir_fi) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001262 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001263 udf_update_tag((char *)dir_fi,
1264 (sizeof(struct fileIdentDesc) +
1265 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001266 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 mark_inode_dirty(old_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001268 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 mark_buffer_dirty_inode(dir_bh, old_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001270
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001271 inode_dec_link_count(old_dir);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001272 if (new_inode)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001273 inode_dec_link_count(new_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001274 else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001275 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 mark_inode_dirty(new_dir);
1277 }
1278 }
1279
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001280 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001282 brelse(ofibh.ebh);
1283 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285
1286 retval = 0;
1287
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001288end_rename:
Jan Kara3bf25cb2007-05-08 00:35:16 -07001289 brelse(dir_bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001290 if (nfi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001292 brelse(nfibh.ebh);
1293 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return retval;
1298}
1299
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001300const struct inode_operations udf_dir_inode_operations = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001301 .lookup = udf_lookup,
1302 .create = udf_create,
1303 .link = udf_link,
1304 .unlink = udf_unlink,
1305 .symlink = udf_symlink,
1306 .mkdir = udf_mkdir,
1307 .rmdir = udf_rmdir,
1308 .mknod = udf_mknod,
1309 .rename = udf_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310};