udf: fix sparse warning in namei.c
[linux-2.6/zen-sources.git] / fs / udf / namei.c
blob2b34c8ca6c83ed2e6aa01f955664ffccf7eb1ec0
1 /*
2 * namei.c
4 * PURPOSE
5 * Inode name handling routines for the OSTA-UDF(tm) filesystem.
7 * 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.
13 * (C) 1998-2004 Ben Fennema
14 * (C) 1999-2000 Stelias Computing Inc
16 * HISTORY
18 * 12/12/98 blf Created. Split out the lookup code from dir.c
19 * 04/19/99 blf link, mknod, symlink support
22 #include "udfdecl.h"
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>
33 #include <linux/sched.h>
34 #include <linux/crc-itu-t.h>
36 static inline int udf_match(int len1, const char *name1, int len2,
37 const char *name2)
39 if (len1 != len2)
40 return 0;
42 return !memcmp(name1, name2, len1);
45 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
46 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
47 uint8_t *impuse, uint8_t *fileident)
49 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
50 uint16_t crc;
51 int offset;
52 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
53 uint8_t lfi = cfi->lengthFileIdent;
54 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
55 sizeof(struct fileIdentDesc);
56 int adinicb = 0;
58 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
59 adinicb = 1;
61 offset = fibh->soffset + sizeof(struct fileIdentDesc);
63 if (impuse) {
64 if (adinicb || (offset + liu < 0)) {
65 memcpy((uint8_t *)sfi->impUse, impuse, liu);
66 } else if (offset >= 0) {
67 memcpy(fibh->ebh->b_data + offset, impuse, liu);
68 } else {
69 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
70 memcpy(fibh->ebh->b_data, impuse - offset,
71 liu + offset);
75 offset += liu;
77 if (fileident) {
78 if (adinicb || (offset + lfi < 0)) {
79 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
80 } else if (offset >= 0) {
81 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
82 } else {
83 memcpy((uint8_t *)sfi->fileIdent + liu, fileident,
84 -offset);
85 memcpy(fibh->ebh->b_data, fileident - offset,
86 lfi + offset);
90 offset += lfi;
92 if (adinicb || (offset + padlen < 0)) {
93 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
94 } else if (offset >= 0) {
95 memset(fibh->ebh->b_data + offset, 0x00, padlen);
96 } else {
97 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
98 memset(fibh->ebh->b_data, 0x00, padlen + offset);
101 crc = crc_itu_t(0, (uint8_t *)cfi + sizeof(tag),
102 sizeof(struct fileIdentDesc) - sizeof(tag));
104 if (fibh->sbh == fibh->ebh) {
105 crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
106 crclen + sizeof(tag) -
107 sizeof(struct fileIdentDesc));
108 } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
109 crc = crc_itu_t(crc, fibh->ebh->b_data +
110 sizeof(struct fileIdentDesc) +
111 fibh->soffset,
112 crclen + sizeof(tag) -
113 sizeof(struct fileIdentDesc));
114 } else {
115 crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
116 -fibh->soffset - sizeof(struct fileIdentDesc));
117 crc = crc_itu_t(crc, fibh->ebh->b_data, fibh->eoffset);
120 cfi->descTag.descCRC = cpu_to_le16(crc);
121 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
122 cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag);
124 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
125 memcpy((uint8_t *)sfi, (uint8_t *)cfi,
126 sizeof(struct fileIdentDesc));
127 } else {
128 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
129 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
130 sizeof(struct fileIdentDesc) + fibh->soffset);
133 if (adinicb) {
134 mark_inode_dirty(inode);
135 } else {
136 if (fibh->sbh != fibh->ebh)
137 mark_buffer_dirty_inode(fibh->ebh, inode);
138 mark_buffer_dirty_inode(fibh->sbh, inode);
140 return 0;
143 static struct fileIdentDesc *udf_find_entry(struct inode *dir,
144 struct dentry *dentry,
145 struct udf_fileident_bh *fibh,
146 struct fileIdentDesc *cfi)
148 struct fileIdentDesc *fi = NULL;
149 loff_t f_pos;
150 int block, flen;
151 char *fname = NULL;
152 char *nameptr;
153 uint8_t lfi;
154 uint16_t liu;
155 loff_t size;
156 kernel_lb_addr eloc;
157 uint32_t elen;
158 sector_t offset;
159 struct extent_position epos = {};
160 struct udf_inode_info *dinfo = UDF_I(dir);
162 size = udf_ext0_offset(dir) + dir->i_size;
163 f_pos = udf_ext0_offset(dir);
165 fibh->sbh = fibh->ebh = NULL;
166 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
167 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
168 if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos,
169 &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30))
170 goto out_err;
171 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
172 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
173 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
174 epos.offset -= sizeof(short_ad);
175 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
176 epos.offset -= sizeof(long_ad);
177 } else
178 offset = 0;
180 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
181 if (!fibh->sbh)
182 goto out_err;
185 fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
186 if (!fname)
187 goto out_err;
189 while (f_pos < size) {
190 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
191 &elen, &offset);
192 if (!fi)
193 goto out_err;
195 liu = le16_to_cpu(cfi->lengthOfImpUse);
196 lfi = cfi->lengthFileIdent;
198 if (fibh->sbh == fibh->ebh) {
199 nameptr = fi->fileIdent + liu;
200 } else {
201 int poffset; /* Unpaded ending offset */
203 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
204 liu + lfi;
206 if (poffset >= lfi)
207 nameptr = (uint8_t *)(fibh->ebh->b_data +
208 poffset - lfi);
209 else {
210 nameptr = fname;
211 memcpy(nameptr, fi->fileIdent + liu,
212 lfi - poffset);
213 memcpy(nameptr + lfi - poffset,
214 fibh->ebh->b_data, poffset);
218 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
219 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
220 continue;
223 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
224 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
225 continue;
228 if (!lfi)
229 continue;
231 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
232 if (flen && udf_match(flen, fname, dentry->d_name.len,
233 dentry->d_name.name))
234 goto out_ok;
237 out_err:
238 fi = NULL;
239 if (fibh->sbh != fibh->ebh)
240 brelse(fibh->ebh);
241 brelse(fibh->sbh);
242 out_ok:
243 brelse(epos.bh);
244 kfree(fname);
246 return fi;
249 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
250 struct nameidata *nd)
252 struct inode *inode = NULL;
253 struct fileIdentDesc cfi;
254 struct udf_fileident_bh fibh;
256 if (dentry->d_name.len > UDF_NAME_LEN - 2)
257 return ERR_PTR(-ENAMETOOLONG);
259 lock_kernel();
260 #ifdef UDF_RECOVERY
261 /* temporary shorthand for specifying files by inode number */
262 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
263 kernel_lb_addr lb = {
264 .logicalBlockNum = 0,
265 .partitionReferenceNum =
266 simple_strtoul(dentry->d_name.name + 3,
267 NULL, 0),
269 inode = udf_iget(dir->i_sb, lb);
270 if (!inode) {
271 unlock_kernel();
272 return ERR_PTR(-EACCES);
274 } else
275 #endif /* UDF_RECOVERY */
277 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
278 if (fibh.sbh != fibh.ebh)
279 brelse(fibh.ebh);
280 brelse(fibh.sbh);
282 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
283 if (!inode) {
284 unlock_kernel();
285 return ERR_PTR(-EACCES);
288 unlock_kernel();
289 d_add(dentry, inode);
291 return NULL;
294 static struct fileIdentDesc *udf_add_entry(struct inode *dir,
295 struct dentry *dentry,
296 struct udf_fileident_bh *fibh,
297 struct fileIdentDesc *cfi, int *err)
299 struct super_block *sb = dir->i_sb;
300 struct fileIdentDesc *fi = NULL;
301 char *name = NULL;
302 int namelen;
303 loff_t f_pos;
304 loff_t size = udf_ext0_offset(dir) + dir->i_size;
305 int nfidlen;
306 uint8_t lfi;
307 uint16_t liu;
308 int block;
309 kernel_lb_addr eloc;
310 uint32_t elen;
311 sector_t offset;
312 struct extent_position epos = {};
313 struct udf_inode_info *dinfo;
315 fibh->sbh = fibh->ebh = NULL;
316 name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
317 if (!name) {
318 *err = -ENOMEM;
319 goto out_err;
322 if (dentry) {
323 if (!dentry->d_name.len) {
324 *err = -EINVAL;
325 goto out_err;
327 namelen = udf_put_filename(sb, dentry->d_name.name, name,
328 dentry->d_name.len);
329 if (!namelen) {
330 *err = -ENAMETOOLONG;
331 goto out_err;
333 } else {
334 namelen = 0;
337 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
339 f_pos = udf_ext0_offset(dir);
341 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
342 dinfo = UDF_I(dir);
343 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
344 if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos,
345 &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) {
346 block = udf_get_lb_pblock(dir->i_sb,
347 dinfo->i_location, 0);
348 fibh->soffset = fibh->eoffset = sb->s_blocksize;
349 goto add;
351 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
352 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
353 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
354 epos.offset -= sizeof(short_ad);
355 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
356 epos.offset -= sizeof(long_ad);
357 } else
358 offset = 0;
360 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
361 if (!fibh->sbh) {
362 *err = -EIO;
363 goto out_err;
366 block = dinfo->i_location.logicalBlockNum;
369 while (f_pos < size) {
370 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
371 &elen, &offset);
373 if (!fi) {
374 *err = -EIO;
375 goto out_err;
378 liu = le16_to_cpu(cfi->lengthOfImpUse);
379 lfi = cfi->lengthFileIdent;
381 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
382 if (((sizeof(struct fileIdentDesc) +
383 liu + lfi + 3) & ~3) == nfidlen) {
384 cfi->descTag.tagSerialNum = cpu_to_le16(1);
385 cfi->fileVersionNum = cpu_to_le16(1);
386 cfi->fileCharacteristics = 0;
387 cfi->lengthFileIdent = namelen;
388 cfi->lengthOfImpUse = cpu_to_le16(0);
389 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
390 name))
391 goto out_ok;
392 else {
393 *err = -EIO;
394 goto out_err;
400 add:
401 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
402 elen = (elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);
403 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
404 epos.offset -= sizeof(short_ad);
405 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
406 epos.offset -= sizeof(long_ad);
407 udf_write_aext(dir, &epos, eloc, elen, 1);
409 f_pos += nfidlen;
411 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
412 sb->s_blocksize - fibh->eoffset < nfidlen) {
413 brelse(epos.bh);
414 epos.bh = NULL;
415 fibh->soffset -= udf_ext0_offset(dir);
416 fibh->eoffset -= udf_ext0_offset(dir);
417 f_pos -= udf_ext0_offset(dir);
418 if (fibh->sbh != fibh->ebh)
419 brelse(fibh->ebh);
420 brelse(fibh->sbh);
421 fibh->sbh = fibh->ebh =
422 udf_expand_dir_adinicb(dir, &block, err);
423 if (!fibh->sbh)
424 goto out_err;
425 epos.block = dinfo->i_location;
426 epos.offset = udf_file_entry_alloc_offset(dir);
427 /* Load extent udf_expand_dir_adinicb() has created */
428 udf_current_aext(dir, &epos, &eloc, &elen, 1);
431 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
432 fibh->soffset = fibh->eoffset;
433 fibh->eoffset += nfidlen;
434 if (fibh->sbh != fibh->ebh) {
435 brelse(fibh->sbh);
436 fibh->sbh = fibh->ebh;
439 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
440 block = dinfo->i_location.logicalBlockNum;
441 fi = (struct fileIdentDesc *)
442 (dinfo->i_ext.i_data +
443 fibh->soffset -
444 udf_ext0_offset(dir) +
445 dinfo->i_lenEAttr);
446 } else {
447 block = eloc.logicalBlockNum +
448 ((elen - 1) >>
449 dir->i_sb->s_blocksize_bits);
450 fi = (struct fileIdentDesc *)
451 (fibh->sbh->b_data + fibh->soffset);
453 } else {
454 fibh->soffset = fibh->eoffset - sb->s_blocksize;
455 fibh->eoffset += nfidlen - sb->s_blocksize;
456 if (fibh->sbh != fibh->ebh) {
457 brelse(fibh->sbh);
458 fibh->sbh = fibh->ebh;
461 block = eloc.logicalBlockNum + ((elen - 1) >>
462 dir->i_sb->s_blocksize_bits);
463 fibh->ebh = udf_bread(dir,
464 f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
465 if (!fibh->ebh)
466 goto out_err;
468 if (!fibh->soffset) {
469 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
470 (EXT_RECORDED_ALLOCATED >> 30)) {
471 block = eloc.logicalBlockNum + ((elen - 1) >>
472 dir->i_sb->s_blocksize_bits);
473 } else
474 block++;
476 brelse(fibh->sbh);
477 fibh->sbh = fibh->ebh;
478 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
479 } else {
480 fi = (struct fileIdentDesc *)
481 (fibh->sbh->b_data + sb->s_blocksize +
482 fibh->soffset);
486 memset(cfi, 0, sizeof(struct fileIdentDesc));
487 if (UDF_SB(sb)->s_udfrev >= 0x0200)
488 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
489 sizeof(tag));
490 else
491 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
492 sizeof(tag));
493 cfi->fileVersionNum = cpu_to_le16(1);
494 cfi->lengthFileIdent = namelen;
495 cfi->lengthOfImpUse = cpu_to_le16(0);
496 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
497 dir->i_size += nfidlen;
498 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
499 dinfo->i_lenAlloc += nfidlen;
500 mark_inode_dirty(dir);
501 goto out_ok;
502 } else {
503 *err = -EIO;
504 goto out_err;
507 out_err:
508 fi = NULL;
509 if (fibh->sbh != fibh->ebh)
510 brelse(fibh->ebh);
511 brelse(fibh->sbh);
512 out_ok:
513 brelse(epos.bh);
514 kfree(name);
515 return fi;
518 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
519 struct udf_fileident_bh *fibh,
520 struct fileIdentDesc *cfi)
522 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
524 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
525 memset(&(cfi->icb), 0x00, sizeof(long_ad));
527 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
530 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
531 struct nameidata *nd)
533 struct udf_fileident_bh fibh;
534 struct inode *inode;
535 struct fileIdentDesc cfi, *fi;
536 int err;
537 struct udf_inode_info *iinfo;
539 lock_kernel();
540 inode = udf_new_inode(dir, mode, &err);
541 if (!inode) {
542 unlock_kernel();
543 return err;
546 iinfo = UDF_I(inode);
547 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
548 inode->i_data.a_ops = &udf_adinicb_aops;
549 else
550 inode->i_data.a_ops = &udf_aops;
551 inode->i_op = &udf_file_inode_operations;
552 inode->i_fop = &udf_file_operations;
553 inode->i_mode = mode;
554 mark_inode_dirty(inode);
556 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
557 if (!fi) {
558 inode->i_nlink--;
559 mark_inode_dirty(inode);
560 iput(inode);
561 unlock_kernel();
562 return err;
564 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
565 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
566 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
567 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
568 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
569 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
570 mark_inode_dirty(dir);
571 if (fibh.sbh != fibh.ebh)
572 brelse(fibh.ebh);
573 brelse(fibh.sbh);
574 unlock_kernel();
575 d_instantiate(dentry, inode);
577 return 0;
580 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
581 dev_t rdev)
583 struct inode *inode;
584 struct udf_fileident_bh fibh;
585 struct fileIdentDesc cfi, *fi;
586 int err;
587 struct udf_inode_info *iinfo;
589 if (!old_valid_dev(rdev))
590 return -EINVAL;
592 lock_kernel();
593 err = -EIO;
594 inode = udf_new_inode(dir, mode, &err);
595 if (!inode)
596 goto out;
598 iinfo = UDF_I(inode);
599 inode->i_uid = current->fsuid;
600 init_special_inode(inode, mode, rdev);
601 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
602 if (!fi) {
603 inode->i_nlink--;
604 mark_inode_dirty(inode);
605 iput(inode);
606 unlock_kernel();
607 return err;
609 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
610 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
611 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
612 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
613 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
614 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
615 mark_inode_dirty(dir);
616 mark_inode_dirty(inode);
618 if (fibh.sbh != fibh.ebh)
619 brelse(fibh.ebh);
620 brelse(fibh.sbh);
621 d_instantiate(dentry, inode);
622 err = 0;
624 out:
625 unlock_kernel();
626 return err;
629 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
631 struct inode *inode;
632 struct udf_fileident_bh fibh;
633 struct fileIdentDesc cfi, *fi;
634 int err;
635 struct udf_inode_info *dinfo = UDF_I(dir);
636 struct udf_inode_info *iinfo;
638 lock_kernel();
639 err = -EMLINK;
640 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
641 goto out;
643 err = -EIO;
644 inode = udf_new_inode(dir, S_IFDIR, &err);
645 if (!inode)
646 goto out;
648 iinfo = UDF_I(inode);
649 inode->i_op = &udf_dir_inode_operations;
650 inode->i_fop = &udf_dir_operations;
651 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
652 if (!fi) {
653 inode->i_nlink--;
654 mark_inode_dirty(inode);
655 iput(inode);
656 goto out;
658 inode->i_nlink = 2;
659 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
660 cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
661 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
662 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
663 cfi.fileCharacteristics =
664 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
665 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
666 brelse(fibh.sbh);
667 inode->i_mode = S_IFDIR | mode;
668 if (dir->i_mode & S_ISGID)
669 inode->i_mode |= S_ISGID;
670 mark_inode_dirty(inode);
672 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
673 if (!fi) {
674 inode->i_nlink = 0;
675 mark_inode_dirty(inode);
676 iput(inode);
677 goto out;
679 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
680 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
681 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
682 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
683 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
684 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
685 inc_nlink(dir);
686 mark_inode_dirty(dir);
687 d_instantiate(dentry, inode);
688 if (fibh.sbh != fibh.ebh)
689 brelse(fibh.ebh);
690 brelse(fibh.sbh);
691 err = 0;
693 out:
694 unlock_kernel();
695 return err;
698 static int empty_dir(struct inode *dir)
700 struct fileIdentDesc *fi, cfi;
701 struct udf_fileident_bh fibh;
702 loff_t f_pos;
703 loff_t size = udf_ext0_offset(dir) + dir->i_size;
704 int block;
705 kernel_lb_addr eloc;
706 uint32_t elen;
707 sector_t offset;
708 struct extent_position epos = {};
709 struct udf_inode_info *dinfo = UDF_I(dir);
711 f_pos = udf_ext0_offset(dir);
712 fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
714 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
715 fibh.sbh = fibh.ebh = NULL;
716 else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
717 &epos, &eloc, &elen, &offset) ==
718 (EXT_RECORDED_ALLOCATED >> 30)) {
719 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
720 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
721 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
722 epos.offset -= sizeof(short_ad);
723 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
724 epos.offset -= sizeof(long_ad);
725 } else
726 offset = 0;
728 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
729 if (!fibh.sbh) {
730 brelse(epos.bh);
731 return 0;
733 } else {
734 brelse(epos.bh);
735 return 0;
738 while (f_pos < size) {
739 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
740 &elen, &offset);
741 if (!fi) {
742 if (fibh.sbh != fibh.ebh)
743 brelse(fibh.ebh);
744 brelse(fibh.sbh);
745 brelse(epos.bh);
746 return 0;
749 if (cfi.lengthFileIdent &&
750 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
751 if (fibh.sbh != fibh.ebh)
752 brelse(fibh.ebh);
753 brelse(fibh.sbh);
754 brelse(epos.bh);
755 return 0;
759 if (fibh.sbh != fibh.ebh)
760 brelse(fibh.ebh);
761 brelse(fibh.sbh);
762 brelse(epos.bh);
764 return 1;
767 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
769 int retval;
770 struct inode *inode = dentry->d_inode;
771 struct udf_fileident_bh fibh;
772 struct fileIdentDesc *fi, cfi;
773 kernel_lb_addr tloc;
775 retval = -ENOENT;
776 lock_kernel();
777 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
778 if (!fi)
779 goto out;
781 retval = -EIO;
782 tloc = lelb_to_cpu(cfi.icb.extLocation);
783 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
784 goto end_rmdir;
785 retval = -ENOTEMPTY;
786 if (!empty_dir(inode))
787 goto end_rmdir;
788 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
789 if (retval)
790 goto end_rmdir;
791 if (inode->i_nlink != 2)
792 udf_warning(inode->i_sb, "udf_rmdir",
793 "empty directory has nlink != 2 (%d)",
794 inode->i_nlink);
795 clear_nlink(inode);
796 inode->i_size = 0;
797 inode_dec_link_count(dir);
798 inode->i_ctime = dir->i_ctime = dir->i_mtime =
799 current_fs_time(dir->i_sb);
800 mark_inode_dirty(dir);
802 end_rmdir:
803 if (fibh.sbh != fibh.ebh)
804 brelse(fibh.ebh);
805 brelse(fibh.sbh);
807 out:
808 unlock_kernel();
809 return retval;
812 static int udf_unlink(struct inode *dir, struct dentry *dentry)
814 int retval;
815 struct inode *inode = dentry->d_inode;
816 struct udf_fileident_bh fibh;
817 struct fileIdentDesc *fi;
818 struct fileIdentDesc cfi;
819 kernel_lb_addr tloc;
821 retval = -ENOENT;
822 lock_kernel();
823 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
824 if (!fi)
825 goto out;
827 retval = -EIO;
828 tloc = lelb_to_cpu(cfi.icb.extLocation);
829 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
830 goto end_unlink;
832 if (!inode->i_nlink) {
833 udf_debug("Deleting nonexistent file (%lu), %d\n",
834 inode->i_ino, inode->i_nlink);
835 inode->i_nlink = 1;
837 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
838 if (retval)
839 goto end_unlink;
840 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
841 mark_inode_dirty(dir);
842 inode_dec_link_count(inode);
843 inode->i_ctime = dir->i_ctime;
844 retval = 0;
846 end_unlink:
847 if (fibh.sbh != fibh.ebh)
848 brelse(fibh.ebh);
849 brelse(fibh.sbh);
851 out:
852 unlock_kernel();
853 return retval;
856 static int udf_symlink(struct inode *dir, struct dentry *dentry,
857 const char *symname)
859 struct inode *inode;
860 struct pathComponent *pc;
861 char *compstart;
862 struct udf_fileident_bh fibh;
863 struct extent_position epos = {};
864 int eoffset, elen = 0;
865 struct fileIdentDesc *fi;
866 struct fileIdentDesc cfi;
867 char *ea;
868 int err;
869 int block;
870 char *name = NULL;
871 int namelen;
872 struct buffer_head *bh;
873 struct udf_inode_info *iinfo;
875 lock_kernel();
876 inode = udf_new_inode(dir, S_IFLNK, &err);
877 if (!inode)
878 goto out;
880 name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
881 if (!name) {
882 err = -ENOMEM;
883 goto out_no_entry;
886 iinfo = UDF_I(inode);
887 inode->i_mode = S_IFLNK | S_IRWXUGO;
888 inode->i_data.a_ops = &udf_symlink_aops;
889 inode->i_op = &page_symlink_inode_operations;
891 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
892 kernel_lb_addr eloc;
893 uint32_t bsize;
895 block = udf_new_block(inode->i_sb, inode,
896 iinfo->i_location.partitionReferenceNum,
897 iinfo->i_location.logicalBlockNum, &err);
898 if (!block)
899 goto out_no_entry;
900 epos.block = iinfo->i_location;
901 epos.offset = udf_file_entry_alloc_offset(inode);
902 epos.bh = NULL;
903 eloc.logicalBlockNum = block;
904 eloc.partitionReferenceNum =
905 iinfo->i_location.partitionReferenceNum;
906 bsize = inode->i_sb->s_blocksize;
907 iinfo->i_lenExtents = bsize;
908 udf_add_aext(inode, &epos, eloc, bsize, 0);
909 brelse(epos.bh);
911 block = udf_get_pblock(inode->i_sb, block,
912 iinfo->i_location.partitionReferenceNum,
914 epos.bh = udf_tread(inode->i_sb, block);
915 lock_buffer(epos.bh);
916 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
917 set_buffer_uptodate(epos.bh);
918 unlock_buffer(epos.bh);
919 mark_buffer_dirty_inode(epos.bh, inode);
920 ea = epos.bh->b_data + udf_ext0_offset(inode);
921 } else
922 ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
924 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
925 pc = (struct pathComponent *)ea;
927 if (*symname == '/') {
928 do {
929 symname++;
930 } while (*symname == '/');
932 pc->componentType = 1;
933 pc->lengthComponentIdent = 0;
934 pc->componentFileVersionNum = 0;
935 pc += sizeof(struct pathComponent);
936 elen += sizeof(struct pathComponent);
939 err = -ENAMETOOLONG;
941 while (*symname) {
942 if (elen + sizeof(struct pathComponent) > eoffset)
943 goto out_no_entry;
945 pc = (struct pathComponent *)(ea + elen);
947 compstart = (char *)symname;
949 do {
950 symname++;
951 } while (*symname && *symname != '/');
953 pc->componentType = 5;
954 pc->lengthComponentIdent = 0;
955 pc->componentFileVersionNum = 0;
956 if (compstart[0] == '.') {
957 if ((symname - compstart) == 1)
958 pc->componentType = 4;
959 else if ((symname - compstart) == 2 &&
960 compstart[1] == '.')
961 pc->componentType = 3;
964 if (pc->componentType == 5) {
965 namelen = udf_put_filename(inode->i_sb, compstart, name,
966 symname - compstart);
967 if (!namelen)
968 goto out_no_entry;
970 if (elen + sizeof(struct pathComponent) + namelen >
971 eoffset)
972 goto out_no_entry;
973 else
974 pc->lengthComponentIdent = namelen;
976 memcpy(pc->componentIdent, name, namelen);
979 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
981 if (*symname) {
982 do {
983 symname++;
984 } while (*symname == '/');
988 brelse(epos.bh);
989 inode->i_size = elen;
990 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
991 iinfo->i_lenAlloc = inode->i_size;
992 mark_inode_dirty(inode);
994 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
995 if (!fi)
996 goto out_no_entry;
997 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
998 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
999 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1000 if (bh) {
1001 struct logicalVolIntegrityDesc *lvid =
1002 (struct logicalVolIntegrityDesc *)bh->b_data;
1003 struct logicalVolHeaderDesc *lvhd;
1004 uint64_t uniqueID;
1005 lvhd = (struct logicalVolHeaderDesc *)
1006 lvid->logicalVolContentsUse;
1007 uniqueID = le64_to_cpu(lvhd->uniqueID);
1008 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1009 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1010 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1011 uniqueID += 16;
1012 lvhd->uniqueID = cpu_to_le64(uniqueID);
1013 mark_buffer_dirty(bh);
1015 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1016 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1017 mark_inode_dirty(dir);
1018 if (fibh.sbh != fibh.ebh)
1019 brelse(fibh.ebh);
1020 brelse(fibh.sbh);
1021 d_instantiate(dentry, inode);
1022 err = 0;
1024 out:
1025 kfree(name);
1026 unlock_kernel();
1027 return err;
1029 out_no_entry:
1030 inode_dec_link_count(inode);
1031 iput(inode);
1032 goto out;
1035 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1036 struct dentry *dentry)
1038 struct inode *inode = old_dentry->d_inode;
1039 struct udf_fileident_bh fibh;
1040 struct fileIdentDesc cfi, *fi;
1041 int err;
1042 struct buffer_head *bh;
1044 lock_kernel();
1045 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1046 unlock_kernel();
1047 return -EMLINK;
1050 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1051 if (!fi) {
1052 unlock_kernel();
1053 return err;
1055 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1056 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1057 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1058 if (bh) {
1059 struct logicalVolIntegrityDesc *lvid =
1060 (struct logicalVolIntegrityDesc *)bh->b_data;
1061 struct logicalVolHeaderDesc *lvhd;
1062 uint64_t uniqueID;
1063 lvhd = (struct logicalVolHeaderDesc *)
1064 (lvid->logicalVolContentsUse);
1065 uniqueID = le64_to_cpu(lvhd->uniqueID);
1066 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1067 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1068 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1069 uniqueID += 16;
1070 lvhd->uniqueID = cpu_to_le64(uniqueID);
1071 mark_buffer_dirty(bh);
1073 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1074 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1075 mark_inode_dirty(dir);
1077 if (fibh.sbh != fibh.ebh)
1078 brelse(fibh.ebh);
1079 brelse(fibh.sbh);
1080 inc_nlink(inode);
1081 inode->i_ctime = current_fs_time(inode->i_sb);
1082 mark_inode_dirty(inode);
1083 atomic_inc(&inode->i_count);
1084 d_instantiate(dentry, inode);
1085 unlock_kernel();
1087 return 0;
1090 /* Anybody can rename anything with this: the permission checks are left to the
1091 * higher-level routines.
1093 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1094 struct inode *new_dir, struct dentry *new_dentry)
1096 struct inode *old_inode = old_dentry->d_inode;
1097 struct inode *new_inode = new_dentry->d_inode;
1098 struct udf_fileident_bh ofibh, nfibh;
1099 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1100 struct fileIdentDesc ocfi, ncfi;
1101 struct buffer_head *dir_bh = NULL;
1102 int retval = -ENOENT;
1103 kernel_lb_addr tloc;
1104 struct udf_inode_info *old_iinfo = UDF_I(old_inode);
1106 lock_kernel();
1107 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1108 if (ofi) {
1109 if (ofibh.sbh != ofibh.ebh)
1110 brelse(ofibh.ebh);
1111 brelse(ofibh.sbh);
1113 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1114 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1115 != old_inode->i_ino)
1116 goto end_rename;
1118 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1119 if (nfi) {
1120 if (!new_inode) {
1121 if (nfibh.sbh != nfibh.ebh)
1122 brelse(nfibh.ebh);
1123 brelse(nfibh.sbh);
1124 nfi = NULL;
1127 if (S_ISDIR(old_inode->i_mode)) {
1128 int offset = udf_ext0_offset(old_inode);
1130 if (new_inode) {
1131 retval = -ENOTEMPTY;
1132 if (!empty_dir(new_inode))
1133 goto end_rename;
1135 retval = -EIO;
1136 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1137 dir_fi = udf_get_fileident(
1138 old_iinfo->i_ext.i_data -
1139 (old_iinfo->i_efe ?
1140 sizeof(struct extendedFileEntry) :
1141 sizeof(struct fileEntry)),
1142 old_inode->i_sb->s_blocksize, &offset);
1143 } else {
1144 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1145 if (!dir_bh)
1146 goto end_rename;
1147 dir_fi = udf_get_fileident(dir_bh->b_data,
1148 old_inode->i_sb->s_blocksize, &offset);
1150 if (!dir_fi)
1151 goto end_rename;
1152 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1153 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1154 old_dir->i_ino)
1155 goto end_rename;
1157 retval = -EMLINK;
1158 if (!new_inode &&
1159 new_dir->i_nlink >=
1160 (256 << sizeof(new_dir->i_nlink)) - 1)
1161 goto end_rename;
1163 if (!nfi) {
1164 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1165 &retval);
1166 if (!nfi)
1167 goto end_rename;
1171 * Like most other Unix systems, set the ctime for inodes on a
1172 * rename.
1174 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1175 mark_inode_dirty(old_inode);
1178 * ok, that's it
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);
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);
1189 if (new_inode) {
1190 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1191 inode_dec_link_count(new_inode);
1193 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1194 mark_inode_dirty(old_dir);
1196 if (dir_fi) {
1197 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1198 udf_update_tag((char *)dir_fi,
1199 (sizeof(struct fileIdentDesc) +
1200 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1201 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1202 mark_inode_dirty(old_inode);
1203 else
1204 mark_buffer_dirty_inode(dir_bh, old_inode);
1206 inode_dec_link_count(old_dir);
1207 if (new_inode)
1208 inode_dec_link_count(new_inode);
1209 else {
1210 inc_nlink(new_dir);
1211 mark_inode_dirty(new_dir);
1215 if (ofi) {
1216 if (ofibh.sbh != ofibh.ebh)
1217 brelse(ofibh.ebh);
1218 brelse(ofibh.sbh);
1221 retval = 0;
1223 end_rename:
1224 brelse(dir_bh);
1225 if (nfi) {
1226 if (nfibh.sbh != nfibh.ebh)
1227 brelse(nfibh.ebh);
1228 brelse(nfibh.sbh);
1230 unlock_kernel();
1232 return retval;
1235 const struct inode_operations udf_dir_inode_operations = {
1236 .lookup = udf_lookup,
1237 .create = udf_create,
1238 .link = udf_link,
1239 .unlink = udf_unlink,
1240 .symlink = udf_symlink,
1241 .mkdir = udf_mkdir,
1242 .rmdir = udf_rmdir,
1243 .mknod = udf_mknod,
1244 .rename = udf_rename,