blob: bec96a6b3343c8586fa30823673fb8f8db32cbe7 [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,
46 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;
50 uint8_t checksum = 0;
51 int i;
52 int offset;
53 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
54 uint8_t lfi = cfi->lengthFileIdent;
55 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070056 sizeof(struct fileIdentDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int adinicb = 0;
58
59 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
60 adinicb = 1;
61
62 offset = fibh->soffset + sizeof(struct fileIdentDesc);
63
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070064 if (impuse) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070065 if (adinicb || (offset + liu < 0)) {
66 memcpy((uint8_t *)sfi->impUse, impuse, liu);
67 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 memcpy(fibh->ebh->b_data + offset, impuse, liu);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070069 } else {
70 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
71 memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73 }
74
75 offset += liu;
76
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070077 if (fileident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070078 if (adinicb || (offset + lfi < 0)) {
79 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
80 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070082 } else {
83 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, -offset);
84 memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 }
87
88 offset += lfi;
89
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070090 if (adinicb || (offset + padlen < 0)) {
91 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
92 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 memset(fibh->ebh->b_data + offset, 0x00, padlen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070094 } else {
95 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 memset(fibh->ebh->b_data, 0x00, padlen + offset);
97 }
98
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070099 crc = udf_crc((uint8_t *)cfi + sizeof(tag),
100 sizeof(struct fileIdentDesc) - sizeof(tag), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700102 if (fibh->sbh == fibh->ebh) {
103 crc = udf_crc((uint8_t *)sfi->impUse,
104 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
105 } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
106 crc = udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) + fibh->soffset,
107 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
108 } else {
109 crc = udf_crc((uint8_t *)sfi->impUse,
110 -fibh->soffset - sizeof(struct fileIdentDesc), crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
112 }
113
114 cfi->descTag.descCRC = cpu_to_le16(crc);
115 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
116
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700117 for (i = 0; i < 16; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (i != 4)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700119 checksum += ((uint8_t *)&cfi->descTag)[i];
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 cfi->descTag.tagChecksum = checksum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700123 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
124 memcpy((uint8_t *)sfi, (uint8_t *)cfi, sizeof(struct fileIdentDesc));
125 } else {
126 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
127 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700131 if (adinicb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700133 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (fibh->sbh != fibh->ebh)
135 mark_buffer_dirty_inode(fibh->ebh, inode);
136 mark_buffer_dirty_inode(fibh->sbh, inode);
137 }
138 return 0;
139}
140
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700141static struct fileIdentDesc *udf_find_entry(struct inode *dir,
142 struct dentry *dentry,
143 struct udf_fileident_bh *fibh,
144 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700146 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 loff_t f_pos;
148 int block, flen;
149 char fname[UDF_NAME_LEN];
150 char *nameptr;
151 uint8_t lfi;
152 uint16_t liu;
KAMBAROV, ZAURec471dc2005-06-28 20:45:10 -0700153 loff_t size;
Jan Karaff116fc2007-05-08 00:35:14 -0700154 kernel_lb_addr eloc;
155 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700156 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700157 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
KAMBAROV, ZAURec471dc2005-06-28 20:45:10 -0700159 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 f_pos = (udf_ext0_offset(dir) >> 2);
161
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700162 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
163 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 fibh->sbh = fibh->ebh = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 } else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
166 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700168 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700170 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700172 epos.offset -= sizeof(long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700173 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700177 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700178 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return NULL;
180 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700181 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700182 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return NULL;
184 }
185
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700186 while ((f_pos < size)) {
187 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
188 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700191 brelse(fibh->ebh);
192 brelse(fibh->sbh);
193 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return NULL;
195 }
196
197 liu = le16_to_cpu(cfi->lengthOfImpUse);
198 lfi = cfi->lengthFileIdent;
199
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700200 if (fibh->sbh == fibh->ebh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 nameptr = fi->fileIdent + liu;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700202 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int poffset; /* Unpaded ending offset */
204
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700205 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700207 if (poffset >= lfi) {
208 nameptr = (uint8_t *)(fibh->ebh->b_data + poffset - lfi);
209 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 nameptr = fname;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700211 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
212 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 }
215
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700216 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
217 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 continue;
219 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700220
221 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
222 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 continue;
224 }
225
226 if (!lfi)
227 continue;
228
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700229 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi))) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700230 if (udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700231 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return fi;
233 }
234 }
235 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700238 brelse(fibh->ebh);
239 brelse(fibh->sbh);
240 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return NULL;
243}
244
245/*
246 * udf_lookup
247 *
248 * PURPOSE
249 * Look-up the inode for a given name.
250 *
251 * DESCRIPTION
252 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
253 * available for a directory. The filesystem is useless if this routine is
254 * not available for at least the filesystem's root directory.
255 *
256 * This routine is passed an incomplete dentry - it must be completed by
257 * calling d_add(dentry, inode). If the name does not exist, then the
258 * specified inode must be set to null. An error should only be returned
259 * when the lookup fails for a reason other than the name not existing.
260 * Note that the directory inode semaphore is held during the call.
261 *
262 * Refer to lookup_dentry() in fs/namei.c
263 * lookup_dentry() -> lookup() -> real_lookup() -> .
264 *
265 * PRE-CONDITIONS
266 * dir Pointer to inode of parent directory.
267 * dentry Pointer to dentry to complete.
268 * nd Pointer to lookup nameidata
269 *
270 * POST-CONDITIONS
271 * <return> Zero on success.
272 *
273 * HISTORY
274 * July 1, 1997 - Andrew E. Mileski
275 * Written, tested, and released.
276 */
277
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700278static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
279 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct inode *inode = NULL;
Jayachandran Cdb9a3692006-02-03 03:04:50 -0800282 struct fileIdentDesc cfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct udf_fileident_bh fibh;
284
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700285 if (dentry->d_name.len > UDF_NAME_LEN - 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return ERR_PTR(-ENAMETOOLONG);
287
288 lock_kernel();
289#ifdef UDF_RECOVERY
290 /* temporary shorthand for specifying files by inode number */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700291 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700292 kernel_lb_addr lb = {
293 .logicalBlockNum = 0,
294 .partitionReferenceNum = simple_strtoul(dentry->d_name.name + 3,
295 NULL, 0),
296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 inode = udf_iget(dir->i_sb, lb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unlock_kernel();
300 return ERR_PTR(-EACCES);
301 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700302 }
303 else
304#endif /* UDF_RECOVERY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700306 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700308 brelse(fibh.ebh);
309 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700312 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unlock_kernel();
314 return ERR_PTR(-EACCES);
315 }
316 }
317 unlock_kernel();
318 d_add(dentry, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return NULL;
321}
322
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700323static struct fileIdentDesc *udf_add_entry(struct inode *dir,
324 struct dentry *dentry,
325 struct udf_fileident_bh *fibh,
326 struct fileIdentDesc *cfi, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct super_block *sb;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700329 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
331 int namelen;
332 loff_t f_pos;
333 int flen;
334 char *nameptr;
335 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
336 int nfidlen;
337 uint8_t lfi;
338 uint16_t liu;
339 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700340 kernel_lb_addr eloc;
341 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700342 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700343 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 sb = dir->i_sb;
346
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347 if (dentry) {
348 if (!dentry->d_name.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 *err = -EINVAL;
350 return NULL;
351 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700352 if (!(namelen = udf_put_filename(sb, dentry->d_name.name, name,
353 dentry->d_name.len))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *err = -ENAMETOOLONG;
355 return NULL;
356 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700357 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 namelen = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
362
363 f_pos = (udf_ext0_offset(dir) >> 2);
364
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700365 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
366 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 fibh->sbh = fibh->ebh = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700368 } else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
369 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700371 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700373 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700375 epos.offset -= sizeof(long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700376 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700380 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700381 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 *err = -EIO;
383 return NULL;
384 }
385
386 block = UDF_I_LOCATION(dir).logicalBlockNum;
387
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700388 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
390 fibh->sbh = fibh->ebh = NULL;
391 fibh->soffset = fibh->eoffset = sb->s_blocksize;
392 goto add;
393 }
394
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700395 while ((f_pos < size)) {
396 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
397 &elen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700399 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700401 brelse(fibh->ebh);
402 brelse(fibh->sbh);
403 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 *err = -EIO;
405 return NULL;
406 }
407
408 liu = le16_to_cpu(cfi->lengthOfImpUse);
409 lfi = cfi->lengthFileIdent;
410
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700411 if (fibh->sbh == fibh->ebh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 nameptr = fi->fileIdent + liu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700413 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int poffset; /* Unpaded ending offset */
415
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700416 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700418 if (poffset >= lfi) {
419 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
420 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 nameptr = fname;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700422 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
423 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 }
426
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700428 if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700429 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 cfi->descTag.tagSerialNum = cpu_to_le16(1);
431 cfi->fileVersionNum = cpu_to_le16(1);
432 cfi->fileCharacteristics = 0;
433 cfi->lengthFileIdent = namelen;
434 cfi->lengthOfImpUse = cpu_to_le16(0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700435 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return fi;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700437 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *err = -EIO;
439 return NULL;
440 }
441 }
442 }
443
444 if (!lfi || !dentry)
445 continue;
446
447 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700448 udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700450 brelse(fibh->ebh);
451 brelse(fibh->sbh);
452 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 *err = -EEXIST;
454 return NULL;
455 }
456 }
457
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700458add:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 f_pos += nfidlen;
460
461 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700462 sb->s_blocksize - fibh->eoffset < nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700463 brelse(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700464 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 fibh->soffset -= udf_ext0_offset(dir);
466 fibh->eoffset -= udf_ext0_offset(dir);
467 f_pos -= (udf_ext0_offset(dir) >> 2);
468 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700469 brelse(fibh->ebh);
470 brelse(fibh->sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700471 if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return NULL;
Jan Karaff116fc2007-05-08 00:35:14 -0700473 epos.block = UDF_I_LOCATION(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 eloc.logicalBlockNum = block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700475 eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 elen = dir->i_sb->s_blocksize;
Jan Karaff116fc2007-05-08 00:35:14 -0700477 epos.offset = udf_file_entry_alloc_offset(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700479 epos.offset += sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700481 epos.offset += sizeof(long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700484 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 fibh->soffset = fibh->eoffset;
486 fibh->eoffset += nfidlen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700487 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700488 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 fibh->sbh = fibh->ebh;
490 }
491
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700492 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 block = UDF_I_LOCATION(dir).logicalBlockNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700494 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset -
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700495 udf_ext0_offset(dir) +
496 UDF_I_LENEATTR(dir));
497 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700499 dir->i_sb->s_blocksize_bits);
500 fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700502 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 fibh->soffset = fibh->eoffset - sb->s_blocksize;
504 fibh->eoffset += nfidlen - sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700505 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700506 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 fibh->sbh = fibh->ebh;
508 }
509
510 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700511 dir->i_sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700512 fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err);
513 if (!fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700514 brelse(epos.bh);
515 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return NULL;
517 }
518
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700519 if (!fibh->soffset) {
Jan Karaff116fc2007-05-08 00:35:14 -0700520 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700523 dir->i_sb->s_blocksize_bits);
524 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700525 block++;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Jan Kara3bf25cb2007-05-08 00:35:16 -0700528 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 fibh->sbh = fibh->ebh;
530 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 fi = (struct fileIdentDesc *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700533 (fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 }
536
537 memset(cfi, 0, sizeof(struct fileIdentDesc));
538 if (UDF_SB_UDFREV(sb) >= 0x0200)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700539 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700541 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 cfi->fileVersionNum = cpu_to_le16(1);
543 cfi->lengthFileIdent = namelen;
544 cfi->lengthOfImpUse = cpu_to_le16(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700545 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700546 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 dir->i_size += nfidlen;
548 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
549 UDF_I_LENALLOC(dir) += nfidlen;
550 mark_inode_dirty(dir);
551 return fi;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700552 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700553 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700555 brelse(fibh->ebh);
556 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *err = -EIO;
558 return NULL;
559 }
560}
561
562static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700563 struct udf_fileident_bh *fibh,
564 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
569 memset(&(cfi->icb), 0x00, sizeof(long_ad));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
572}
573
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700574static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
575 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct udf_fileident_bh fibh;
578 struct inode *inode;
579 struct fileIdentDesc cfi, *fi;
580 int err;
581
582 lock_kernel();
583 inode = udf_new_inode(dir, mode, &err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700584 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 unlock_kernel();
586 return err;
587 }
588
589 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
590 inode->i_data.a_ops = &udf_adinicb_aops;
591 else
592 inode->i_data.a_ops = &udf_aops;
593 inode->i_op = &udf_file_inode_operations;
594 inode->i_fop = &udf_file_operations;
595 inode->i_mode = mode;
596 mark_inode_dirty(inode);
597
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700598 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
599 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 mark_inode_dirty(inode);
601 iput(inode);
602 unlock_kernel();
603 return err;
604 }
605 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
606 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700607 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
608 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700610 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 mark_inode_dirty(dir);
612 }
613 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700614 brelse(fibh.ebh);
615 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 unlock_kernel();
617 d_instantiate(dentry, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
620}
621
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700622static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
623 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700625 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct udf_fileident_bh fibh;
627 struct fileIdentDesc cfi, *fi;
628 int err;
629
630 if (!old_valid_dev(rdev))
631 return -EINVAL;
632
633 lock_kernel();
634 err = -EIO;
635 inode = udf_new_inode(dir, mode, &err);
636 if (!inode)
637 goto out;
638
639 inode->i_uid = current->fsuid;
640 init_special_inode(inode, mode, rdev);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700641 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
642 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 mark_inode_dirty(inode);
644 iput(inode);
645 unlock_kernel();
646 return err;
647 }
648 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
649 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700650 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
651 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700653 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 mark_inode_dirty(dir);
655 }
656 mark_inode_dirty(inode);
657
658 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700659 brelse(fibh.ebh);
660 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 d_instantiate(dentry, inode);
662 err = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700663
664out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unlock_kernel();
666 return err;
667}
668
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700669static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700671 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct udf_fileident_bh fibh;
673 struct fileIdentDesc cfi, *fi;
674 int err;
675
676 lock_kernel();
677 err = -EMLINK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700678 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto out;
680
681 err = -EIO;
682 inode = udf_new_inode(dir, S_IFDIR, &err);
683 if (!inode)
684 goto out;
685
686 inode->i_op = &udf_dir_inode_operations;
687 inode->i_fop = &udf_dir_operations;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700688 if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 inode->i_nlink--;
690 mark_inode_dirty(inode);
691 iput(inode);
692 goto out;
693 }
694 inode->i_nlink = 2;
695 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
696 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700697 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
698 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
699 cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700701 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 inode->i_mode = S_IFDIR | mode;
703 if (dir->i_mode & S_ISGID)
704 inode->i_mode |= S_ISGID;
705 mark_inode_dirty(inode);
706
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700707 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 inode->i_nlink = 0;
709 mark_inode_dirty(inode);
710 iput(inode);
711 goto out;
712 }
713 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
714 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700715 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
716 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
718 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Dave Hansend8c76e62006-09-30 23:29:04 -0700719 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 mark_inode_dirty(dir);
721 d_instantiate(dentry, inode);
722 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700723 brelse(fibh.ebh);
724 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 err = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700726
727out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unlock_kernel();
729 return err;
730}
731
732static int empty_dir(struct inode *dir)
733{
734 struct fileIdentDesc *fi, cfi;
735 struct udf_fileident_bh fibh;
736 loff_t f_pos;
737 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
738 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700739 kernel_lb_addr eloc;
740 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700741 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700742 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 f_pos = (udf_ext0_offset(dir) >> 2);
745
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700746 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700748 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 fibh.sbh = fibh.ebh = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700750 } else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
751 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700753 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700755 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700757 epos.offset -= sizeof(long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700758 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700762 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700763 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
765 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700766 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700767 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769 }
770
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700771 while ((f_pos < size)) {
772 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
773 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700776 brelse(fibh.ebh);
777 brelse(fibh.sbh);
778 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return 0;
780 }
781
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700782 if (cfi.lengthFileIdent &&
783 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700785 brelse(fibh.ebh);
786 brelse(fibh.sbh);
787 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 0;
789 }
790 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700793 brelse(fibh.ebh);
794 brelse(fibh.sbh);
795 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 1;
798}
799
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700800static int udf_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700803 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct udf_fileident_bh fibh;
805 struct fileIdentDesc *fi, cfi;
806 kernel_lb_addr tloc;
807
808 retval = -ENOENT;
809 lock_kernel();
810 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
811 if (!fi)
812 goto out;
813
814 retval = -EIO;
815 tloc = lelb_to_cpu(cfi.icb.extLocation);
816 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
817 goto end_rmdir;
818 retval = -ENOTEMPTY;
819 if (!empty_dir(inode))
820 goto end_rmdir;
821 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
822 if (retval)
823 goto end_rmdir;
824 if (inode->i_nlink != 2)
825 udf_warning(inode->i_sb, "udf_rmdir",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700826 "empty directory has nlink != 2 (%d)",
827 inode->i_nlink);
Dave Hansence71ec32006-09-30 23:29:06 -0700828 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 inode->i_size = 0;
Stephen Mollettc007c062007-05-08 00:31:31 -0700830 inode_dec_link_count(dir);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700831 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 mark_inode_dirty(dir);
833
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700834end_rmdir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700836 brelse(fibh.ebh);
837 brelse(fibh.sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700838
839out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 unlock_kernel();
841 return retval;
842}
843
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700844static int udf_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700847 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct udf_fileident_bh fibh;
849 struct fileIdentDesc *fi;
850 struct fileIdentDesc cfi;
851 kernel_lb_addr tloc;
852
853 retval = -ENOENT;
854 lock_kernel();
855 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
856 if (!fi)
857 goto out;
858
859 retval = -EIO;
860 tloc = lelb_to_cpu(cfi.icb.extLocation);
861 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
862 goto end_unlink;
863
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700864 if (!inode->i_nlink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 udf_debug("Deleting nonexistent file (%lu), %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700866 inode->i_ino, inode->i_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 inode->i_nlink = 1;
868 }
869 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
870 if (retval)
871 goto end_unlink;
872 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
873 mark_inode_dirty(dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700874 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 inode->i_ctime = dir->i_ctime;
876 retval = 0;
877
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700878end_unlink:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700880 brelse(fibh.ebh);
881 brelse(fibh.sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700882
883out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unlock_kernel();
885 return retval;
886}
887
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700888static int udf_symlink(struct inode *dir, struct dentry *dentry,
889 const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700891 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct pathComponent *pc;
893 char *compstart;
894 struct udf_fileident_bh fibh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700895 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int eoffset, elen = 0;
897 struct fileIdentDesc *fi;
898 struct fileIdentDesc cfi;
899 char *ea;
900 int err;
901 int block;
902 char name[UDF_NAME_LEN];
903 int namelen;
904
905 lock_kernel();
906 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
907 goto out;
908
909 inode->i_mode = S_IFLNK | S_IRWXUGO;
910 inode->i_data.a_ops = &udf_symlink_aops;
911 inode->i_op = &page_symlink_inode_operations;
912
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700913 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB) {
Jan Karaff116fc2007-05-08 00:35:14 -0700914 kernel_lb_addr eloc;
915 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 block = udf_new_block(inode->i_sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700918 UDF_I_LOCATION(inode).partitionReferenceNum,
919 UDF_I_LOCATION(inode).logicalBlockNum, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (!block)
921 goto out_no_entry;
Jan Karaff116fc2007-05-08 00:35:14 -0700922 epos.block = UDF_I_LOCATION(inode);
923 epos.offset = udf_file_entry_alloc_offset(inode);
924 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 eloc.logicalBlockNum = block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700926 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 elen = inode->i_sb->s_blocksize;
928 UDF_I_LENEXTENTS(inode) = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700929 udf_add_aext(inode, &epos, eloc, elen, 0);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700930 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 block = udf_get_pblock(inode->i_sb, block,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700933 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700934 epos.bh = udf_tread(inode->i_sb, block);
935 lock_buffer(epos.bh);
936 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
937 set_buffer_uptodate(epos.bh);
938 unlock_buffer(epos.bh);
939 mark_buffer_dirty_inode(epos.bh, inode);
940 ea = epos.bh->b_data + udf_ext0_offset(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700941 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
946 pc = (struct pathComponent *)ea;
947
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700948 if (*symname == '/') {
949 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 symname++;
951 } while (*symname == '/');
952
953 pc->componentType = 1;
954 pc->lengthComponentIdent = 0;
955 pc->componentFileVersionNum = 0;
956 pc += sizeof(struct pathComponent);
957 elen += sizeof(struct pathComponent);
958 }
959
960 err = -ENAMETOOLONG;
961
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700962 while (*symname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (elen + sizeof(struct pathComponent) > eoffset)
964 goto out_no_entry;
965
966 pc = (struct pathComponent *)(ea + elen);
967
968 compstart = (char *)symname;
969
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700970 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 symname++;
972 } while (*symname && *symname != '/');
973
974 pc->componentType = 5;
975 pc->lengthComponentIdent = 0;
976 pc->componentFileVersionNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700977 if (compstart[0] == '.') {
978 if ((symname - compstart) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 pc->componentType = 4;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700980 else if ((symname - compstart) == 2 && compstart[1] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 pc->componentType = 3;
982 }
983
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700984 if (pc->componentType == 5) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700985 namelen = udf_put_filename(inode->i_sb, compstart, name,
986 symname - compstart);
987 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 goto out_no_entry;
989
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700990 if (elen + sizeof(struct pathComponent) + namelen > eoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto out_no_entry;
992 else
993 pc->lengthComponentIdent = namelen;
994
995 memcpy(pc->componentIdent, name, namelen);
996 }
997
998 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
999
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001000 if (*symname) {
1001 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 symname++;
1003 } while (*symname == '/');
1004 }
1005 }
1006
Jan Kara3bf25cb2007-05-08 00:35:16 -07001007 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 inode->i_size = elen;
1009 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1010 UDF_I_LENALLOC(inode) = inode->i_size;
1011 mark_inode_dirty(inode);
1012
1013 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1014 goto out_no_entry;
1015 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1016 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001017 if (UDF_SB_LVIDBH(inode->i_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct logicalVolHeaderDesc *lvhd;
1019 uint64_t uniqueID;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001020 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001022 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1023 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1025 uniqueID += 16;
1026 lvhd->uniqueID = cpu_to_le64(uniqueID);
1027 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1028 }
1029 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001030 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 mark_inode_dirty(dir);
1032 }
1033 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001034 brelse(fibh.ebh);
1035 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 d_instantiate(dentry, inode);
1037 err = 0;
1038
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001039out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 unlock_kernel();
1041 return err;
1042
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001043out_no_entry:
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001044 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 iput(inode);
1046 goto out;
1047}
1048
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001049static int udf_link(struct dentry *old_dentry, struct inode *dir,
1050 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct inode *inode = old_dentry->d_inode;
1053 struct udf_fileident_bh fibh;
1054 struct fileIdentDesc cfi, *fi;
1055 int err;
1056
1057 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001058 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 unlock_kernel();
1060 return -EMLINK;
1061 }
1062
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001063 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 unlock_kernel();
1065 return err;
1066 }
1067 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1068 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001069 if (UDF_SB_LVIDBH(inode->i_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct logicalVolHeaderDesc *lvhd;
1071 uint64_t uniqueID;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001072 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->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);
1079 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1080 }
1081 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001082 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 mark_inode_dirty(dir);
1084 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001087 brelse(fibh.ebh);
1088 brelse(fibh.sbh);
Dave Hansend8c76e62006-09-30 23:29:04 -07001089 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 inode->i_ctime = current_fs_time(inode->i_sb);
1091 mark_inode_dirty(inode);
1092 atomic_inc(&inode->i_count);
1093 d_instantiate(dentry, inode);
1094 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097}
1098
1099/* Anybody can rename anything with this: the permission checks are left to the
1100 * higher-level routines.
1101 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001102static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1103 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001105 struct inode *old_inode = old_dentry->d_inode;
1106 struct inode *new_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct udf_fileident_bh ofibh, nfibh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001108 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct buffer_head *dir_bh = NULL;
1110 int retval = -ENOENT;
1111 kernel_lb_addr tloc;
1112
1113 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001114 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001116 brelse(ofibh.ebh);
1117 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1120 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001121 != old_inode->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto end_rename;
1123
1124 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001125 if (nfi) {
1126 if (!new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001128 brelse(nfibh.ebh);
1129 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 nfi = NULL;
1131 }
1132 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001133 if (S_ISDIR(old_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 uint32_t offset = udf_ext0_offset(old_inode);
1135
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001136 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 retval = -ENOTEMPTY;
1138 if (!empty_dir(new_inode))
1139 goto end_rename;
1140 }
1141 retval = -EIO;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001142 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001144 (UDF_I_EFE(old_inode) ?
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001145 sizeof(struct extendedFileEntry) :
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001146 sizeof(struct fileEntry)),
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001147 old_inode->i_sb->s_blocksize, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001148 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1150 if (!dir_bh)
1151 goto end_rename;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001152 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 if (!dir_fi)
1155 goto end_rename;
1156 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001157 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) != old_dir->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 goto end_rename;
1159
1160 retval = -EMLINK;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001161 if (!new_inode && new_dir->i_nlink >= (256 << sizeof(new_dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 goto end_rename;
1163 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001164 if (!nfi) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001165 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!nfi)
1167 goto end_rename;
1168 }
1169
1170 /*
1171 * Like most other Unix systems, set the ctime for inodes on a
1172 * rename.
1173 */
1174 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1175 mark_inode_dirty(old_inode);
1176
1177 /*
1178 * ok, that's it
1179 */
1180 ncfi.fileVersionNum = ocfi.fileVersionNum;
1181 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1182 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1183 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1184
1185 /* The old fid may have moved - find it again */
1186 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1187 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1188
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001189 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001191 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1194 mark_inode_dirty(old_dir);
1195
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001196 if (dir_fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1198 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001199 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001200 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 mark_inode_dirty(old_inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001202 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 mark_buffer_dirty_inode(dir_bh, old_inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001204 }
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001205 inode_dec_link_count(old_dir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001206 if (new_inode) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001207 inode_dec_link_count(new_inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001208 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001209 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 mark_inode_dirty(new_dir);
1211 }
1212 }
1213
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001214 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001216 brelse(ofibh.ebh);
1217 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
1220 retval = 0;
1221
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001222end_rename:
Jan Kara3bf25cb2007-05-08 00:35:16 -07001223 brelse(dir_bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001224 if (nfi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001226 brelse(nfibh.ebh);
1227 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return retval;
1232}
1233
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001234const struct inode_operations udf_dir_inode_operations = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001235 .lookup = udf_lookup,
1236 .create = udf_create,
1237 .link = udf_link,
1238 .unlink = udf_unlink,
1239 .symlink = udf_symlink,
1240 .mkdir = udf_mkdir,
1241 .rmdir = udf_rmdir,
1242 .mknod = udf_mknod,
1243 .rename = udf_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244};