blob: 23e530659fa5d3d19d37c22017cd4b6dbef1ad5b [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 = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
KAMBAROV, ZAURec471dc2005-06-28 20:45:10 -0700162 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 f_pos = (udf_ext0_offset(dir) >> 2);
164
Marcin Slusarz4b111112008-02-08 04:20:36 -0800165 fibh->soffset = fibh->eoffset =
166 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800167 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 fibh->sbh = fibh->ebh = NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800169 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
170 &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 Slusarzc0b34432008-02-08 04:20:42 -0800174 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700175 epos.offset -= sizeof(short_ad);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800176 else if (UDF_I(dir)->i_alloc_type ==
177 ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700178 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800179 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 offset = 0;
181
Marcin Slusarz4b111112008-02-08 04:20:36 -0800182 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
183 if (!fibh->sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700184 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return NULL;
186 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700187 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700188 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return NULL;
190 }
191
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700192 while ((f_pos < size)) {
193 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
194 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700195 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700197 brelse(fibh->ebh);
198 brelse(fibh->sbh);
199 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return NULL;
201 }
202
203 liu = le16_to_cpu(cfi->lengthOfImpUse);
204 lfi = cfi->lengthFileIdent;
205
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700206 if (fibh->sbh == fibh->ebh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 nameptr = fi->fileIdent + liu;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700208 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int poffset; /* Unpaded ending offset */
210
Marcin Slusarz4b111112008-02-08 04:20:36 -0800211 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
212 liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Marcin Slusarz4b111112008-02-08 04:20:36 -0800214 if (poffset >= lfi)
215 nameptr = (uint8_t *)(fibh->ebh->b_data +
216 poffset - lfi);
217 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 nameptr = fname;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800219 memcpy(nameptr, fi->fileIdent + liu,
220 lfi - poffset);
221 memcpy(nameptr + lfi - poffset,
222 fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700226 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
227 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 continue;
229 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700230
231 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
232 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 continue;
234 }
235
236 if (!lfi)
237 continue;
238
Marcin Slusarz4b111112008-02-08 04:20:36 -0800239 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
240 if (flen && udf_match(flen, fname, dentry->d_name.len,
241 dentry->d_name.name)) {
242 brelse(epos.bh);
243 return fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700248 brelse(fibh->ebh);
249 brelse(fibh->sbh);
250 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return NULL;
253}
254
255/*
256 * udf_lookup
257 *
258 * PURPOSE
259 * Look-up the inode for a given name.
260 *
261 * DESCRIPTION
262 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
263 * available for a directory. The filesystem is useless if this routine is
264 * not available for at least the filesystem's root directory.
265 *
266 * This routine is passed an incomplete dentry - it must be completed by
267 * calling d_add(dentry, inode). If the name does not exist, then the
268 * specified inode must be set to null. An error should only be returned
269 * when the lookup fails for a reason other than the name not existing.
270 * Note that the directory inode semaphore is held during the call.
271 *
272 * Refer to lookup_dentry() in fs/namei.c
273 * lookup_dentry() -> lookup() -> real_lookup() -> .
274 *
275 * PRE-CONDITIONS
276 * dir Pointer to inode of parent directory.
277 * dentry Pointer to dentry to complete.
278 * nd Pointer to lookup nameidata
279 *
280 * POST-CONDITIONS
281 * <return> Zero on success.
282 *
283 * HISTORY
284 * July 1, 1997 - Andrew E. Mileski
285 * Written, tested, and released.
286 */
287
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
289 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct inode *inode = NULL;
Jayachandran Cdb9a3692006-02-03 03:04:50 -0800292 struct fileIdentDesc cfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct udf_fileident_bh fibh;
294
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700295 if (dentry->d_name.len > UDF_NAME_LEN - 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return ERR_PTR(-ENAMETOOLONG);
297
298 lock_kernel();
299#ifdef UDF_RECOVERY
300 /* temporary shorthand for specifying files by inode number */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700301 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700302 kernel_lb_addr lb = {
303 .logicalBlockNum = 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800304 .partitionReferenceNum =
305 simple_strtoul(dentry->d_name.name + 3,
306 NULL, 0),
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700307 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 inode = udf_iget(dir->i_sb, lb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700309 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unlock_kernel();
311 return ERR_PTR(-EACCES);
312 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800313 } else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700314#endif /* UDF_RECOVERY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700316 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700318 brelse(fibh.ebh);
319 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700322 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unlock_kernel();
324 return ERR_PTR(-EACCES);
325 }
326 }
327 unlock_kernel();
328 d_add(dentry, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return NULL;
331}
332
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700333static struct fileIdentDesc *udf_add_entry(struct inode *dir,
334 struct dentry *dentry,
335 struct udf_fileident_bh *fibh,
336 struct fileIdentDesc *cfi, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800338 struct super_block *sb = dir->i_sb;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
341 int namelen;
342 loff_t f_pos;
343 int flen;
344 char *nameptr;
345 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
346 int nfidlen;
347 uint8_t lfi;
348 uint16_t liu;
349 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700350 kernel_lb_addr eloc;
351 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700352 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700353 struct extent_position epos = {};
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
372 f_pos = (udf_ext0_offset(dir) >> 2);
373
Marcin Slusarz4b111112008-02-08 04:20:36 -0800374 fibh->soffset = fibh->eoffset =
375 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800376 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 fibh->sbh = fibh->ebh = NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800378 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
379 &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 Slusarzc0b34432008-02-08 04:20:42 -0800383 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700384 epos.offset -= sizeof(short_ad);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800385 else if (UDF_I(dir)->i_alloc_type ==
386 ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700387 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800388 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 offset = 0;
390
Marcin Slusarz4b111112008-02-08 04:20:36 -0800391 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
392 if (!fibh->sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700393 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *err = -EIO;
395 return NULL;
396 }
397
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800398 block = UDF_I(dir)->i_location.logicalBlockNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700400 } else {
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800401 block = udf_get_lb_pblock(dir->i_sb, UDF_I(dir)->i_location, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 fibh->sbh = fibh->ebh = NULL;
403 fibh->soffset = fibh->eoffset = sb->s_blocksize;
404 goto add;
405 }
406
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700407 while ((f_pos < size)) {
408 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
409 &elen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700411 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700413 brelse(fibh->ebh);
414 brelse(fibh->sbh);
415 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *err = -EIO;
417 return NULL;
418 }
419
420 liu = le16_to_cpu(cfi->lengthOfImpUse);
421 lfi = cfi->lengthFileIdent;
422
Marcin Slusarz4b111112008-02-08 04:20:36 -0800423 if (fibh->sbh == fibh->ebh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 nameptr = fi->fileIdent + liu;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800425 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int poffset; /* Unpaded ending offset */
427
Marcin Slusarz4b111112008-02-08 04:20:36 -0800428 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
429 liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Marcin Slusarz4b111112008-02-08 04:20:36 -0800431 if (poffset >= lfi)
432 nameptr = (char *)(fibh->ebh->b_data +
433 poffset - lfi);
434 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 nameptr = fname;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800436 memcpy(nameptr, fi->fileIdent + liu,
437 lfi - poffset);
438 memcpy(nameptr + lfi - poffset,
439 fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 }
442
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700443 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800444 if (((sizeof(struct fileIdentDesc) +
445 liu + lfi + 3) & ~3) == nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700446 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 cfi->descTag.tagSerialNum = cpu_to_le16(1);
448 cfi->fileVersionNum = cpu_to_le16(1);
449 cfi->fileCharacteristics = 0;
450 cfi->lengthFileIdent = namelen;
451 cfi->lengthOfImpUse = cpu_to_le16(0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800452 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
453 name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return fi;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800455 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *err = -EIO;
457 return NULL;
458 }
459 }
460 }
461
462 if (!lfi || !dentry)
463 continue;
464
Marcin Slusarz4b111112008-02-08 04:20:36 -0800465 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
466 if (flen && udf_match(flen, fname, dentry->d_name.len,
467 dentry->d_name.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700469 brelse(fibh->ebh);
470 brelse(fibh->sbh);
471 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 *err = -EEXIST;
473 return NULL;
474 }
475 }
476
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700477add:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 f_pos += nfidlen;
479
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800480 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700481 sb->s_blocksize - fibh->eoffset < nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700482 brelse(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700483 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 fibh->soffset -= udf_ext0_offset(dir);
485 fibh->eoffset -= udf_ext0_offset(dir);
486 f_pos -= (udf_ext0_offset(dir) >> 2);
487 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700488 brelse(fibh->ebh);
489 brelse(fibh->sbh);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800490 fibh->sbh = fibh->ebh =
491 udf_expand_dir_adinicb(dir, &block, err);
492 if (!fibh->sbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return NULL;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800494 epos.block = UDF_I(dir)->i_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 eloc.logicalBlockNum = block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800496 eloc.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800497 UDF_I(dir)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 elen = dir->i_sb->s_blocksize;
Jan Karaff116fc2007-05-08 00:35:14 -0700499 epos.offset = udf_file_entry_alloc_offset(dir);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800500 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700501 epos.offset += sizeof(short_ad);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800502 else if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700503 epos.offset += sizeof(long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700506 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 fibh->soffset = fibh->eoffset;
508 fibh->eoffset += nfidlen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700509 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700510 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 fibh->sbh = fibh->ebh;
512 }
513
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800514 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
515 block = UDF_I(dir)->i_location.logicalBlockNum;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800516 fi = (struct fileIdentDesc *)
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800517 (UDF_I(dir)->i_ext.i_data +
518 fibh->soffset -
Marcin Slusarz4b111112008-02-08 04:20:36 -0800519 udf_ext0_offset(dir) +
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800520 UDF_I(dir)->i_lenEAttr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800522 block = eloc.logicalBlockNum +
523 ((elen - 1) >>
524 dir->i_sb->s_blocksize_bits);
525 fi = (struct fileIdentDesc *)
526 (fibh->sbh->b_data + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700528 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 fibh->soffset = fibh->eoffset - sb->s_blocksize;
530 fibh->eoffset += nfidlen - sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700531 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700532 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 fibh->sbh = fibh->ebh;
534 }
535
536 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700537 dir->i_sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800538 fibh->ebh = udf_bread(dir,
539 f_pos >> (dir->i_sb->s_blocksize_bits - 2),
540 1, err);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700541 if (!fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700542 brelse(epos.bh);
543 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return NULL;
545 }
546
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700547 if (!fibh->soffset) {
Jan Karaff116fc2007-05-08 00:35:14 -0700548 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700549 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700551 dir->i_sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800552 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Jan Kara3bf25cb2007-05-08 00:35:16 -0700555 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 fibh->sbh = fibh->ebh;
557 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700558 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 fi = (struct fileIdentDesc *)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800560 (fibh->sbh->b_data + sb->s_blocksize +
561 fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 }
564
565 memset(cfi, 0, sizeof(struct fileIdentDesc));
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800566 if (UDF_SB(sb)->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800567 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
568 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800570 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
571 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 cfi->fileVersionNum = cpu_to_le16(1);
573 cfi->lengthFileIdent = namelen;
574 cfi->lengthOfImpUse = cpu_to_le16(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700575 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700576 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 dir->i_size += nfidlen;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800578 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
579 UDF_I(dir)->i_lenAlloc += nfidlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 mark_inode_dirty(dir);
581 return fi;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700582 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700583 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700585 brelse(fibh->ebh);
586 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 *err = -EIO;
588 return NULL;
589 }
590}
591
592static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700593 struct udf_fileident_bh *fibh,
594 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
599 memset(&(cfi->icb), 0x00, sizeof(long_ad));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
602}
603
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700604static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
605 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct udf_fileident_bh fibh;
608 struct inode *inode;
609 struct fileIdentDesc cfi, *fi;
610 int err;
611
612 lock_kernel();
613 inode = udf_new_inode(dir, mode, &err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unlock_kernel();
616 return err;
617 }
618
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800619 if (UDF_I(inode)->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 Slusarzc0b34432008-02-08 04:20:42 -0800637 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700638 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800639 cpu_to_le32(UDF_I(inode)->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;
659
660 if (!old_valid_dev(rdev))
661 return -EINVAL;
662
663 lock_kernel();
664 err = -EIO;
665 inode = udf_new_inode(dir, mode, &err);
666 if (!inode)
667 goto out;
668
669 inode->i_uid = current->fsuid;
670 init_special_inode(inode, mode, rdev);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800671 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
672 if (!fi) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700673 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 mark_inode_dirty(inode);
675 iput(inode);
676 unlock_kernel();
677 return err;
678 }
679 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800680 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700681 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800682 cpu_to_le32(UDF_I(inode)->i_unique & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800684 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 mark_inode_dirty(inode);
687
688 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700689 brelse(fibh.ebh);
690 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 d_instantiate(dentry, inode);
692 err = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700693
694out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 unlock_kernel();
696 return err;
697}
698
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700699static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700701 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct udf_fileident_bh fibh;
703 struct fileIdentDesc cfi, *fi;
704 int err;
705
706 lock_kernel();
707 err = -EMLINK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700708 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto out;
710
711 err = -EIO;
712 inode = udf_new_inode(dir, S_IFDIR, &err);
713 if (!inode)
714 goto out;
715
716 inode->i_op = &udf_dir_inode_operations;
717 inode->i_fop = &udf_dir_operations;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800718 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
719 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 inode->i_nlink--;
721 mark_inode_dirty(inode);
722 iput(inode);
723 goto out;
724 }
725 inode->i_nlink = 2;
726 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800727 cfi.icb.extLocation = cpu_to_lelb(UDF_I(dir)->i_location);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700728 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800729 cpu_to_le32(UDF_I(dir)->i_unique & 0x00000000FFFFFFFFUL);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800730 cfi.fileCharacteristics =
731 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700733 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 inode->i_mode = S_IFDIR | mode;
735 if (dir->i_mode & S_ISGID)
736 inode->i_mode |= S_ISGID;
737 mark_inode_dirty(inode);
738
Marcin Slusarz4b111112008-02-08 04:20:36 -0800739 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
740 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 inode->i_nlink = 0;
742 mark_inode_dirty(inode);
743 iput(inode);
744 goto out;
745 }
746 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800747 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700748 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800749 cpu_to_le32(UDF_I(inode)->i_unique & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
751 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Dave Hansend8c76e62006-09-30 23:29:04 -0700752 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 mark_inode_dirty(dir);
754 d_instantiate(dentry, inode);
755 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700756 brelse(fibh.ebh);
757 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 err = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700759
760out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 unlock_kernel();
762 return err;
763}
764
765static int empty_dir(struct inode *dir)
766{
767 struct fileIdentDesc *fi, cfi;
768 struct udf_fileident_bh fibh;
769 loff_t f_pos;
770 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
771 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700772 kernel_lb_addr eloc;
773 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700774 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700775 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 f_pos = (udf_ext0_offset(dir) >> 2);
778
Marcin Slusarz4b111112008-02-08 04:20:36 -0800779 fibh.soffset = fibh.eoffset =
780 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800782 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 fibh.sbh = fibh.ebh = NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800784 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
785 &epos, &eloc, &elen, &offset) ==
786 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700788 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800789 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700790 epos.offset -= sizeof(short_ad);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800791 else if (UDF_I(dir)->i_alloc_type ==
792 ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700793 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800794 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 offset = 0;
796
Marcin Slusarz4b111112008-02-08 04:20:36 -0800797 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
798 if (!fibh.sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700799 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
801 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700802 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700803 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
805 }
806
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700807 while ((f_pos < size)) {
808 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
809 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700810 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700812 brelse(fibh.ebh);
813 brelse(fibh.sbh);
814 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return 0;
816 }
817
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700818 if (cfi.lengthFileIdent &&
819 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700821 brelse(fibh.ebh);
822 brelse(fibh.sbh);
823 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825 }
826 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700829 brelse(fibh.ebh);
830 brelse(fibh.sbh);
831 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return 1;
834}
835
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700836static int udf_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700839 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct udf_fileident_bh fibh;
841 struct fileIdentDesc *fi, cfi;
842 kernel_lb_addr tloc;
843
844 retval = -ENOENT;
845 lock_kernel();
846 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
847 if (!fi)
848 goto out;
849
850 retval = -EIO;
851 tloc = lelb_to_cpu(cfi.icb.extLocation);
852 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
853 goto end_rmdir;
854 retval = -ENOTEMPTY;
855 if (!empty_dir(inode))
856 goto end_rmdir;
857 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
858 if (retval)
859 goto end_rmdir;
860 if (inode->i_nlink != 2)
861 udf_warning(inode->i_sb, "udf_rmdir",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700862 "empty directory has nlink != 2 (%d)",
863 inode->i_nlink);
Dave Hansence71ec32006-09-30 23:29:06 -0700864 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 inode->i_size = 0;
Stephen Mollettc007c062007-05-08 00:31:31 -0700866 inode_dec_link_count(dir);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800867 inode->i_ctime = dir->i_ctime = dir->i_mtime =
868 current_fs_time(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 mark_inode_dirty(dir);
870
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700871end_rmdir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700873 brelse(fibh.ebh);
874 brelse(fibh.sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700875
876out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 unlock_kernel();
878 return retval;
879}
880
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700881static int udf_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700884 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct udf_fileident_bh fibh;
886 struct fileIdentDesc *fi;
887 struct fileIdentDesc cfi;
888 kernel_lb_addr tloc;
889
890 retval = -ENOENT;
891 lock_kernel();
892 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
893 if (!fi)
894 goto out;
895
896 retval = -EIO;
897 tloc = lelb_to_cpu(cfi.icb.extLocation);
898 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
899 goto end_unlink;
900
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700901 if (!inode->i_nlink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 udf_debug("Deleting nonexistent file (%lu), %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700903 inode->i_ino, inode->i_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 inode->i_nlink = 1;
905 }
906 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
907 if (retval)
908 goto end_unlink;
909 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
910 mark_inode_dirty(dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700911 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 inode->i_ctime = dir->i_ctime;
913 retval = 0;
914
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700915end_unlink:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700917 brelse(fibh.ebh);
918 brelse(fibh.sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700919
920out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unlock_kernel();
922 return retval;
923}
924
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700925static int udf_symlink(struct inode *dir, struct dentry *dentry,
926 const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700928 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct pathComponent *pc;
930 char *compstart;
931 struct udf_fileident_bh fibh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700932 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int eoffset, elen = 0;
934 struct fileIdentDesc *fi;
935 struct fileIdentDesc cfi;
936 char *ea;
937 int err;
938 int block;
939 char name[UDF_NAME_LEN];
940 int namelen;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800941 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 lock_kernel();
Marcin Slusarz4b111112008-02-08 04:20:36 -0800944 inode = udf_new_inode(dir, S_IFLNK, &err);
945 if (!inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 goto out;
947
948 inode->i_mode = S_IFLNK | S_IRWXUGO;
949 inode->i_data.a_ops = &udf_symlink_aops;
950 inode->i_op = &page_symlink_inode_operations;
951
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800952 if (UDF_I(inode)->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
Jan Karaff116fc2007-05-08 00:35:14 -0700953 kernel_lb_addr eloc;
954 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 block = udf_new_block(inode->i_sb, inode,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800957 UDF_I(inode)->i_location.partitionReferenceNum,
958 UDF_I(inode)->i_location.logicalBlockNum, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!block)
960 goto out_no_entry;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800961 epos.block = UDF_I(inode)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700962 epos.offset = udf_file_entry_alloc_offset(inode);
963 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 eloc.logicalBlockNum = block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800965 eloc.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800966 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 elen = inode->i_sb->s_blocksize;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800968 UDF_I(inode)->i_lenExtents = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700969 udf_add_aext(inode, &epos, eloc, elen, 0);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700970 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 block = udf_get_pblock(inode->i_sb, block,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800973 UDF_I(inode)->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800974 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700975 epos.bh = udf_tread(inode->i_sb, block);
976 lock_buffer(epos.bh);
977 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
978 set_buffer_uptodate(epos.bh);
979 unlock_buffer(epos.bh);
980 mark_buffer_dirty_inode(epos.bh, inode);
981 ea = epos.bh->b_data + udf_ext0_offset(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700982 } else {
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800983 ea = UDF_I(inode)->i_ext.i_data + UDF_I(inode)->i_lenEAttr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
987 pc = (struct pathComponent *)ea;
988
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700989 if (*symname == '/') {
990 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 symname++;
992 } while (*symname == '/');
993
994 pc->componentType = 1;
995 pc->lengthComponentIdent = 0;
996 pc->componentFileVersionNum = 0;
997 pc += sizeof(struct pathComponent);
998 elen += sizeof(struct pathComponent);
999 }
1000
1001 err = -ENAMETOOLONG;
1002
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001003 while (*symname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (elen + sizeof(struct pathComponent) > eoffset)
1005 goto out_no_entry;
1006
1007 pc = (struct pathComponent *)(ea + elen);
1008
1009 compstart = (char *)symname;
1010
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001011 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 symname++;
1013 } while (*symname && *symname != '/');
1014
1015 pc->componentType = 5;
1016 pc->lengthComponentIdent = 0;
1017 pc->componentFileVersionNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001018 if (compstart[0] == '.') {
1019 if ((symname - compstart) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 pc->componentType = 4;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001021 else if ((symname - compstart) == 2 &&
1022 compstart[1] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 pc->componentType = 3;
1024 }
1025
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001026 if (pc->componentType == 5) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001027 namelen = udf_put_filename(inode->i_sb, compstart, name,
1028 symname - compstart);
1029 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto out_no_entry;
1031
Marcin Slusarz4b111112008-02-08 04:20:36 -08001032 if (elen + sizeof(struct pathComponent) + namelen >
1033 eoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto out_no_entry;
1035 else
1036 pc->lengthComponentIdent = namelen;
1037
1038 memcpy(pc->componentIdent, name, namelen);
1039 }
1040
1041 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1042
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001043 if (*symname) {
1044 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 symname++;
1046 } while (*symname == '/');
1047 }
1048 }
1049
Jan Kara3bf25cb2007-05-08 00:35:16 -07001050 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 inode->i_size = elen;
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001052 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1053 UDF_I(inode)->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 mark_inode_dirty(inode);
1055
Marcin Slusarz4b111112008-02-08 04:20:36 -08001056 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1057 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto out_no_entry;
1059 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001060 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001061 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1062 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001063 struct logicalVolIntegrityDesc *lvid =
1064 (struct logicalVolIntegrityDesc *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct logicalVolHeaderDesc *lvhd;
1066 uint64_t uniqueID;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001067 lvhd = (struct logicalVolHeaderDesc *)
1068 lvid->logicalVolContentsUse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001070 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1071 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1073 uniqueID += 16;
1074 lvhd->uniqueID = cpu_to_le64(uniqueID);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001075 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001078 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001081 brelse(fibh.ebh);
1082 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 d_instantiate(dentry, inode);
1084 err = 0;
1085
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001086out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 unlock_kernel();
1088 return err;
1089
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001090out_no_entry:
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001091 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 iput(inode);
1093 goto out;
1094}
1095
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001096static int udf_link(struct dentry *old_dentry, struct inode *dir,
1097 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 struct inode *inode = old_dentry->d_inode;
1100 struct udf_fileident_bh fibh;
1101 struct fileIdentDesc cfi, *fi;
1102 int err;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001103 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001106 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 unlock_kernel();
1108 return -EMLINK;
1109 }
1110
Marcin Slusarz4b111112008-02-08 04:20:36 -08001111 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1112 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 unlock_kernel();
1114 return err;
1115 }
1116 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001117 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001118 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1119 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001120 struct logicalVolIntegrityDesc *lvid =
1121 (struct logicalVolIntegrityDesc *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct logicalVolHeaderDesc *lvhd;
1123 uint64_t uniqueID;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001124 lvhd = (struct logicalVolHeaderDesc *)
1125 (lvid->logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001127 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1128 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1130 uniqueID += 16;
1131 lvhd->uniqueID = cpu_to_le64(uniqueID);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001132 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001135 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 mark_inode_dirty(dir);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001139 brelse(fibh.ebh);
1140 brelse(fibh.sbh);
Dave Hansend8c76e62006-09-30 23:29:04 -07001141 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 inode->i_ctime = current_fs_time(inode->i_sb);
1143 mark_inode_dirty(inode);
1144 atomic_inc(&inode->i_count);
1145 d_instantiate(dentry, inode);
1146 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return 0;
1149}
1150
1151/* Anybody can rename anything with this: the permission checks are left to the
1152 * higher-level routines.
1153 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001154static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1155 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001157 struct inode *old_inode = old_dentry->d_inode;
1158 struct inode *new_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct udf_fileident_bh ofibh, nfibh;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001160 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1161 struct fileIdentDesc ocfi, ncfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct buffer_head *dir_bh = NULL;
1163 int retval = -ENOENT;
1164 kernel_lb_addr tloc;
1165
1166 lock_kernel();
Marcin Slusarz4b111112008-02-08 04:20:36 -08001167 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1168 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001170 brelse(ofibh.ebh);
1171 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1174 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001175 != old_inode->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto end_rename;
1177
1178 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001179 if (nfi) {
1180 if (!new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001182 brelse(nfibh.ebh);
1183 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 nfi = NULL;
1185 }
1186 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001187 if (S_ISDIR(old_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 uint32_t offset = udf_ext0_offset(old_inode);
1189
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001190 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 retval = -ENOTEMPTY;
1192 if (!empty_dir(new_inode))
1193 goto end_rename;
1194 }
1195 retval = -EIO;
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001196 if (UDF_I(old_inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001197 dir_fi = udf_get_fileident(
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001198 UDF_I(old_inode)->i_ext.i_data -
1199 (UDF_I(old_inode)->i_efe ?
Marcin Slusarz4b111112008-02-08 04:20:36 -08001200 sizeof(struct extendedFileEntry) :
1201 sizeof(struct fileEntry)),
1202 old_inode->i_sb->s_blocksize, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001203 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1205 if (!dir_bh)
1206 goto end_rename;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001207 dir_fi = udf_get_fileident(dir_bh->b_data,
1208 old_inode->i_sb->s_blocksize, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210 if (!dir_fi)
1211 goto end_rename;
1212 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001213 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1214 old_dir->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 goto end_rename;
1216
1217 retval = -EMLINK;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001218 if (!new_inode &&
1219 new_dir->i_nlink >=
1220 (256 << sizeof(new_dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto end_rename;
1222 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001223 if (!nfi) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001224 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1225 &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (!nfi)
1227 goto end_rename;
1228 }
1229
1230 /*
1231 * Like most other Unix systems, set the ctime for inodes on a
1232 * rename.
1233 */
1234 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1235 mark_inode_dirty(old_inode);
1236
1237 /*
1238 * ok, that's it
1239 */
1240 ncfi.fileVersionNum = ocfi.fileVersionNum;
1241 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1242 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1243 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1244
1245 /* The old fid may have moved - find it again */
1246 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1247 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1248
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001249 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001251 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1254 mark_inode_dirty(old_dir);
1255
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001256 if (dir_fi) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001257 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001258 udf_update_tag((char *)dir_fi,
1259 (sizeof(struct fileIdentDesc) +
1260 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001261 if (UDF_I(old_inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 mark_inode_dirty(old_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001263 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 mark_buffer_dirty_inode(dir_bh, old_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001265
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001266 inode_dec_link_count(old_dir);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001267 if (new_inode)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001268 inode_dec_link_count(new_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001269 else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001270 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 mark_inode_dirty(new_dir);
1272 }
1273 }
1274
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001275 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001277 brelse(ofibh.ebh);
1278 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
1281 retval = 0;
1282
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001283end_rename:
Jan Kara3bf25cb2007-05-08 00:35:16 -07001284 brelse(dir_bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001285 if (nfi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001287 brelse(nfibh.ebh);
1288 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return retval;
1293}
1294
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001295const struct inode_operations udf_dir_inode_operations = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001296 .lookup = udf_lookup,
1297 .create = udf_create,
1298 .link = udf_link,
1299 .unlink = udf_unlink,
1300 .symlink = udf_symlink,
1301 .mkdir = udf_mkdir,
1302 .rmdir = udf_rmdir,
1303 .mknod = udf_mknod,
1304 .rename = udf_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305};