ext4: Fix spelling of CONTIG_FS_EXT3 to CONFIG_FS_EXT3
[linux-2.6.git] / fs / udf / namei.c
1 /*
2  * namei.c
3  *
4  * PURPOSE
5  *      Inode name handling routines for the OSTA-UDF(tm) filesystem.
6  *
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.
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>
33 #include <linux/sched.h>
34 #include <linux/crc-itu-t.h>
35 #include <linux/exportfs.h>
36
37 static inline int udf_match(int len1, const unsigned char *name1, int len2,
38                             const unsigned char *name2)
39 {
40         if (len1 != len2)
41                 return 0;
42
43         return !memcmp(name1, name2, len1);
44 }
45
46 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
47                  struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
48                  uint8_t *impuse, uint8_t *fileident)
49 {
50         uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(struct tag);
51         uint16_t crc;
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 -
56                 sizeof(struct fileIdentDesc);
57         int adinicb = 0;
58
59         if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
60                 adinicb = 1;
61
62         offset = fibh->soffset + sizeof(struct fileIdentDesc);
63
64         if (impuse) {
65                 if (adinicb || (offset + liu < 0)) {
66                         memcpy((uint8_t *)sfi->impUse, impuse, liu);
67                 } else if (offset >= 0) {
68                         memcpy(fibh->ebh->b_data + offset, impuse, liu);
69                 } else {
70                         memcpy((uint8_t *)sfi->impUse, impuse, -offset);
71                         memcpy(fibh->ebh->b_data, impuse - offset,
72                                 liu + offset);
73                 }
74         }
75
76         offset += liu;
77
78         if (fileident) {
79                 if (adinicb || (offset + lfi < 0)) {
80                         memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
81                 } else if (offset >= 0) {
82                         memcpy(fibh->ebh->b_data + offset, fileident, lfi);
83                 } else {
84                         memcpy((uint8_t *)sfi->fileIdent + liu, fileident,
85                                 -offset);
86                         memcpy(fibh->ebh->b_data, fileident - offset,
87                                 lfi + offset);
88                 }
89         }
90
91         offset += lfi;
92
93         if (adinicb || (offset + padlen < 0)) {
94                 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
95         } else if (offset >= 0) {
96                 memset(fibh->ebh->b_data + offset, 0x00, padlen);
97         } else {
98                 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
99                 memset(fibh->ebh->b_data, 0x00, padlen + offset);
100         }
101
102         crc = crc_itu_t(0, (uint8_t *)cfi + sizeof(struct tag),
103                       sizeof(struct fileIdentDesc) - sizeof(struct tag));
104
105         if (fibh->sbh == fibh->ebh) {
106                 crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
107                               crclen + sizeof(struct tag) -
108                               sizeof(struct fileIdentDesc));
109         } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
110                 crc = crc_itu_t(crc, fibh->ebh->b_data +
111                                         sizeof(struct fileIdentDesc) +
112                                         fibh->soffset,
113                               crclen + sizeof(struct tag) -
114                                         sizeof(struct fileIdentDesc));
115         } else {
116                 crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
117                               -fibh->soffset - sizeof(struct fileIdentDesc));
118                 crc = crc_itu_t(crc, fibh->ebh->b_data, fibh->eoffset);
119         }
120
121         cfi->descTag.descCRC = cpu_to_le16(crc);
122         cfi->descTag.descCRCLength = cpu_to_le16(crclen);
123         cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag);
124
125         if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
126                 memcpy((uint8_t *)sfi, (uint8_t *)cfi,
127                         sizeof(struct fileIdentDesc));
128         } else {
129                 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
130                 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
131                        sizeof(struct fileIdentDesc) + fibh->soffset);
132         }
133
134         if (adinicb) {
135                 mark_inode_dirty(inode);
136         } else {
137                 if (fibh->sbh != fibh->ebh)
138                         mark_buffer_dirty_inode(fibh->ebh, inode);
139                 mark_buffer_dirty_inode(fibh->sbh, inode);
140         }
141         return 0;
142 }
143
144 static struct fileIdentDesc *udf_find_entry(struct inode *dir,
145                                             const struct qstr *child,
146                                             struct udf_fileident_bh *fibh,
147                                             struct fileIdentDesc *cfi)
148 {
149         struct fileIdentDesc *fi = NULL;
150         loff_t f_pos;
151         int block, flen;
152         unsigned char *fname = NULL;
153         unsigned char *nameptr;
154         uint8_t lfi;
155         uint16_t liu;
156         loff_t size;
157         struct kernel_lb_addr eloc;
158         uint32_t elen;
159         sector_t offset;
160         struct extent_position epos = {};
161         struct udf_inode_info *dinfo = UDF_I(dir);
162         int isdotdot = child->len == 2 &&
163                 child->name[0] == '.' && child->name[1] == '.';
164
165         size = udf_ext0_offset(dir) + dir->i_size;
166         f_pos = udf_ext0_offset(dir);
167
168         fibh->sbh = fibh->ebh = NULL;
169         fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
170         if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
171                 if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos,
172                     &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30))
173                         goto out_err;
174                 block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
175                 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
176                         if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
177                                 epos.offset -= sizeof(struct short_ad);
178                         else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
179                                 epos.offset -= sizeof(struct long_ad);
180                 } else
181                         offset = 0;
182
183                 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
184                 if (!fibh->sbh)
185                         goto out_err;
186         }
187
188         fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
189         if (!fname)
190                 goto out_err;
191
192         while (f_pos < size) {
193                 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
194                                         &elen, &offset);
195                 if (!fi)
196                         goto out_err;
197
198                 liu = le16_to_cpu(cfi->lengthOfImpUse);
199                 lfi = cfi->lengthFileIdent;
200
201                 if (fibh->sbh == fibh->ebh) {
202                         nameptr = fi->fileIdent + liu;
203                 } else {
204                         int poffset;    /* Unpaded ending offset */
205
206                         poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
207                                         liu + lfi;
208
209                         if (poffset >= lfi)
210                                 nameptr = (uint8_t *)(fibh->ebh->b_data +
211                                                       poffset - lfi);
212                         else {
213                                 nameptr = fname;
214                                 memcpy(nameptr, fi->fileIdent + liu,
215                                         lfi - poffset);
216                                 memcpy(nameptr + lfi - poffset,
217                                         fibh->ebh->b_data, poffset);
218                         }
219                 }
220
221                 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
222                         if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
223                                 continue;
224                 }
225
226                 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
227                         if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
228                                 continue;
229                 }
230
231                 if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
232                     isdotdot) {
233                         brelse(epos.bh);
234                         return fi;
235                 }
236
237                 if (!lfi)
238                         continue;
239
240                 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
241                 if (flen && udf_match(flen, fname, child->len, child->name))
242                         goto out_ok;
243         }
244
245 out_err:
246         fi = NULL;
247         if (fibh->sbh != fibh->ebh)
248                 brelse(fibh->ebh);
249         brelse(fibh->sbh);
250 out_ok:
251         brelse(epos.bh);
252         kfree(fname);
253
254         return fi;
255 }
256
257 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
258                                  struct nameidata *nd)
259 {
260         struct inode *inode = NULL;
261         struct fileIdentDesc cfi;
262         struct udf_fileident_bh fibh;
263
264         if (dentry->d_name.len > UDF_NAME_LEN - 2)
265                 return ERR_PTR(-ENAMETOOLONG);
266
267         lock_kernel();
268 #ifdef UDF_RECOVERY
269         /* temporary shorthand for specifying files by inode number */
270         if (!strncmp(dentry->d_name.name, ".B=", 3)) {
271                 struct kernel_lb_addr lb = {
272                         .logicalBlockNum = 0,
273                         .partitionReferenceNum =
274                                 simple_strtoul(dentry->d_name.name + 3,
275                                                 NULL, 0),
276                 };
277                 inode = udf_iget(dir->i_sb, lb);
278                 if (!inode) {
279                         unlock_kernel();
280                         return ERR_PTR(-EACCES);
281                 }
282         } else
283 #endif /* UDF_RECOVERY */
284
285         if (udf_find_entry(dir, &dentry->d_name, &fibh, &cfi)) {
286                 struct kernel_lb_addr loc;
287
288                 if (fibh.sbh != fibh.ebh)
289                         brelse(fibh.ebh);
290                 brelse(fibh.sbh);
291
292                 loc = lelb_to_cpu(cfi.icb.extLocation);
293                 inode = udf_iget(dir->i_sb, &loc);
294                 if (!inode) {
295                         unlock_kernel();
296                         return ERR_PTR(-EACCES);
297                 }
298         }
299         unlock_kernel();
300
301         return d_splice_alias(inode, dentry);
302 }
303
304 static struct fileIdentDesc *udf_add_entry(struct inode *dir,
305                                            struct dentry *dentry,
306                                            struct udf_fileident_bh *fibh,
307                                            struct fileIdentDesc *cfi, int *err)
308 {
309         struct super_block *sb = dir->i_sb;
310         struct fileIdentDesc *fi = NULL;
311         unsigned char *name = NULL;
312         int namelen;
313         loff_t f_pos;
314         loff_t size = udf_ext0_offset(dir) + dir->i_size;
315         int nfidlen;
316         uint8_t lfi;
317         uint16_t liu;
318         int block;
319         struct kernel_lb_addr eloc;
320         uint32_t elen = 0;
321         sector_t offset;
322         struct extent_position epos = {};
323         struct udf_inode_info *dinfo;
324
325         fibh->sbh = fibh->ebh = NULL;
326         name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
327         if (!name) {
328                 *err = -ENOMEM;
329                 goto out_err;
330         }
331
332         if (dentry) {
333                 if (!dentry->d_name.len) {
334                         *err = -EINVAL;
335                         goto out_err;
336                 }
337                 namelen = udf_put_filename(sb, dentry->d_name.name, name,
338                                                  dentry->d_name.len);
339                 if (!namelen) {
340                         *err = -ENAMETOOLONG;
341                         goto out_err;
342                 }
343         } else {
344                 namelen = 0;
345         }
346
347         nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
348
349         f_pos = udf_ext0_offset(dir);
350
351         fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
352         dinfo = UDF_I(dir);
353         if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
354                 if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos,
355                     &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) {
356                         block = udf_get_lb_pblock(dir->i_sb,
357                                         &dinfo->i_location, 0);
358                         fibh->soffset = fibh->eoffset = sb->s_blocksize;
359                         goto add;
360                 }
361                 block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
362                 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
363                         if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
364                                 epos.offset -= sizeof(struct short_ad);
365                         else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
366                                 epos.offset -= sizeof(struct long_ad);
367                 } else
368                         offset = 0;
369
370                 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
371                 if (!fibh->sbh) {
372                         *err = -EIO;
373                         goto out_err;
374                 }
375
376                 block = dinfo->i_location.logicalBlockNum;
377         }
378
379         while (f_pos < size) {
380                 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
381                                         &elen, &offset);
382
383                 if (!fi) {
384                         *err = -EIO;
385                         goto out_err;
386                 }
387
388                 liu = le16_to_cpu(cfi->lengthOfImpUse);
389                 lfi = cfi->lengthFileIdent;
390
391                 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
392                         if (((sizeof(struct fileIdentDesc) +
393                                         liu + lfi + 3) & ~3) == nfidlen) {
394                                 cfi->descTag.tagSerialNum = cpu_to_le16(1);
395                                 cfi->fileVersionNum = cpu_to_le16(1);
396                                 cfi->fileCharacteristics = 0;
397                                 cfi->lengthFileIdent = namelen;
398                                 cfi->lengthOfImpUse = cpu_to_le16(0);
399                                 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
400                                                   name))
401                                         goto out_ok;
402                                 else {
403                                         *err = -EIO;
404                                         goto out_err;
405                                 }
406                         }
407                 }
408         }
409
410 add:
411         f_pos += nfidlen;
412
413         if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
414             sb->s_blocksize - fibh->eoffset < nfidlen) {
415                 brelse(epos.bh);
416                 epos.bh = NULL;
417                 fibh->soffset -= udf_ext0_offset(dir);
418                 fibh->eoffset -= udf_ext0_offset(dir);
419                 f_pos -= udf_ext0_offset(dir);
420                 if (fibh->sbh != fibh->ebh)
421                         brelse(fibh->ebh);
422                 brelse(fibh->sbh);
423                 fibh->sbh = fibh->ebh =
424                                 udf_expand_dir_adinicb(dir, &block, err);
425                 if (!fibh->sbh)
426                         goto out_err;
427                 epos.block = dinfo->i_location;
428                 epos.offset = udf_file_entry_alloc_offset(dir);
429                 /* Load extent udf_expand_dir_adinicb() has created */
430                 udf_current_aext(dir, &epos, &eloc, &elen, 1);
431         }
432
433         /* Entry fits into current block? */
434         if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
435                 fibh->soffset = fibh->eoffset;
436                 fibh->eoffset += nfidlen;
437                 if (fibh->sbh != fibh->ebh) {
438                         brelse(fibh->sbh);
439                         fibh->sbh = fibh->ebh;
440                 }
441
442                 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
443                         block = dinfo->i_location.logicalBlockNum;
444                         fi = (struct fileIdentDesc *)
445                                         (dinfo->i_ext.i_data +
446                                          fibh->soffset -
447                                          udf_ext0_offset(dir) +
448                                          dinfo->i_lenEAttr);
449                 } else {
450                         block = eloc.logicalBlockNum +
451                                         ((elen - 1) >>
452                                                 dir->i_sb->s_blocksize_bits);
453                         fi = (struct fileIdentDesc *)
454                                 (fibh->sbh->b_data + fibh->soffset);
455                 }
456         } else {
457                 /* Round up last extent in the file */
458                 elen = (elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);
459                 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
460                         epos.offset -= sizeof(struct short_ad);
461                 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
462                         epos.offset -= sizeof(struct long_ad);
463                 udf_write_aext(dir, &epos, &eloc, elen, 1);
464                 dinfo->i_lenExtents = (dinfo->i_lenExtents + sb->s_blocksize
465                                         - 1) & ~(sb->s_blocksize - 1);
466
467                 fibh->soffset = fibh->eoffset - sb->s_blocksize;
468                 fibh->eoffset += nfidlen - sb->s_blocksize;
469                 if (fibh->sbh != fibh->ebh) {
470                         brelse(fibh->sbh);
471                         fibh->sbh = fibh->ebh;
472                 }
473
474                 block = eloc.logicalBlockNum + ((elen - 1) >>
475                                                 dir->i_sb->s_blocksize_bits);
476                 fibh->ebh = udf_bread(dir,
477                                 f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
478                 if (!fibh->ebh)
479                         goto out_err;
480
481                 if (!fibh->soffset) {
482                         if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
483                             (EXT_RECORDED_ALLOCATED >> 30)) {
484                                 block = eloc.logicalBlockNum + ((elen - 1) >>
485                                         dir->i_sb->s_blocksize_bits);
486                         } else
487                                 block++;
488
489                         brelse(fibh->sbh);
490                         fibh->sbh = fibh->ebh;
491                         fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
492                 } else {
493                         fi = (struct fileIdentDesc *)
494                                 (fibh->sbh->b_data + sb->s_blocksize +
495                                         fibh->soffset);
496                 }
497         }
498
499         memset(cfi, 0, sizeof(struct fileIdentDesc));
500         if (UDF_SB(sb)->s_udfrev >= 0x0200)
501                 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
502                             sizeof(struct tag));
503         else
504                 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
505                             sizeof(struct tag));
506         cfi->fileVersionNum = cpu_to_le16(1);
507         cfi->lengthFileIdent = namelen;
508         cfi->lengthOfImpUse = cpu_to_le16(0);
509         if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
510                 dir->i_size += nfidlen;
511                 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
512                         dinfo->i_lenAlloc += nfidlen;
513                 else {
514                         /* Find the last extent and truncate it to proper size */
515                         while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
516                                 (EXT_RECORDED_ALLOCATED >> 30))
517                                 ;
518                         elen -= dinfo->i_lenExtents - dir->i_size;
519                         if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
520                                 epos.offset -= sizeof(struct short_ad);
521                         else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
522                                 epos.offset -= sizeof(struct long_ad);
523                         udf_write_aext(dir, &epos, &eloc, elen, 1);
524                         dinfo->i_lenExtents = dir->i_size;
525                 }
526
527                 mark_inode_dirty(dir);
528                 goto out_ok;
529         } else {
530                 *err = -EIO;
531                 goto out_err;
532         }
533
534 out_err:
535         fi = NULL;
536         if (fibh->sbh != fibh->ebh)
537                 brelse(fibh->ebh);
538         brelse(fibh->sbh);
539 out_ok:
540         brelse(epos.bh);
541         kfree(name);
542         return fi;
543 }
544
545 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
546                             struct udf_fileident_bh *fibh,
547                             struct fileIdentDesc *cfi)
548 {
549         cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
550
551         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
552                 memset(&(cfi->icb), 0x00, sizeof(struct long_ad));
553
554         return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
555 }
556
557 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
558                       struct nameidata *nd)
559 {
560         struct udf_fileident_bh fibh;
561         struct inode *inode;
562         struct fileIdentDesc cfi, *fi;
563         int err;
564         struct udf_inode_info *iinfo;
565
566         dquot_initialize(dir);
567
568         lock_kernel();
569         inode = udf_new_inode(dir, mode, &err);
570         if (!inode) {
571                 unlock_kernel();
572                 return err;
573         }
574
575         iinfo = UDF_I(inode);
576         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
577                 inode->i_data.a_ops = &udf_adinicb_aops;
578         else
579                 inode->i_data.a_ops = &udf_aops;
580         inode->i_op = &udf_file_inode_operations;
581         inode->i_fop = &udf_file_operations;
582         inode->i_mode = mode;
583         mark_inode_dirty(inode);
584
585         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
586         if (!fi) {
587                 inode->i_nlink--;
588                 mark_inode_dirty(inode);
589                 iput(inode);
590                 unlock_kernel();
591                 return err;
592         }
593         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
594         cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
595         *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
596                 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
597         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
598         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
599                 mark_inode_dirty(dir);
600         if (fibh.sbh != fibh.ebh)
601                 brelse(fibh.ebh);
602         brelse(fibh.sbh);
603         unlock_kernel();
604         d_instantiate(dentry, inode);
605
606         return 0;
607 }
608
609 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
610                      dev_t rdev)
611 {
612         struct inode *inode;
613         struct udf_fileident_bh fibh;
614         struct fileIdentDesc cfi, *fi;
615         int err;
616         struct udf_inode_info *iinfo;
617
618         if (!old_valid_dev(rdev))
619                 return -EINVAL;
620
621         dquot_initialize(dir);
622
623         lock_kernel();
624         err = -EIO;
625         inode = udf_new_inode(dir, mode, &err);
626         if (!inode)
627                 goto out;
628
629         iinfo = UDF_I(inode);
630         inode->i_uid = current_fsuid();
631         init_special_inode(inode, mode, rdev);
632         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
633         if (!fi) {
634                 inode->i_nlink--;
635                 mark_inode_dirty(inode);
636                 iput(inode);
637                 unlock_kernel();
638                 return err;
639         }
640         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
641         cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
642         *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
643                 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
644         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
645         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
646                 mark_inode_dirty(dir);
647         mark_inode_dirty(inode);
648
649         if (fibh.sbh != fibh.ebh)
650                 brelse(fibh.ebh);
651         brelse(fibh.sbh);
652         d_instantiate(dentry, inode);
653         err = 0;
654
655 out:
656         unlock_kernel();
657         return err;
658 }
659
660 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
661 {
662         struct inode *inode;
663         struct udf_fileident_bh fibh;
664         struct fileIdentDesc cfi, *fi;
665         int err;
666         struct udf_inode_info *dinfo = UDF_I(dir);
667         struct udf_inode_info *iinfo;
668
669         dquot_initialize(dir);
670
671         lock_kernel();
672         err = -EMLINK;
673         if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
674                 goto out;
675
676         err = -EIO;
677         inode = udf_new_inode(dir, S_IFDIR, &err);
678         if (!inode)
679                 goto out;
680
681         iinfo = UDF_I(inode);
682         inode->i_op = &udf_dir_inode_operations;
683         inode->i_fop = &udf_dir_operations;
684         fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
685         if (!fi) {
686                 inode->i_nlink--;
687                 mark_inode_dirty(inode);
688                 iput(inode);
689                 goto out;
690         }
691         inode->i_nlink = 2;
692         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
693         cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
694         *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
695                 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
696         cfi.fileCharacteristics =
697                         FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
698         udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
699         brelse(fibh.sbh);
700         inode->i_mode = S_IFDIR | mode;
701         if (dir->i_mode & S_ISGID)
702                 inode->i_mode |= S_ISGID;
703         mark_inode_dirty(inode);
704
705         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
706         if (!fi) {
707                 inode->i_nlink = 0;
708                 mark_inode_dirty(inode);
709                 iput(inode);
710                 goto out;
711         }
712         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
713         cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
714         *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
715                 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
716         cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
717         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
718         inc_nlink(dir);
719         mark_inode_dirty(dir);
720         d_instantiate(dentry, inode);
721         if (fibh.sbh != fibh.ebh)
722                 brelse(fibh.ebh);
723         brelse(fibh.sbh);
724         err = 0;
725
726 out:
727         unlock_kernel();
728         return err;
729 }
730
731 static int empty_dir(struct inode *dir)
732 {
733         struct fileIdentDesc *fi, cfi;
734         struct udf_fileident_bh fibh;
735         loff_t f_pos;
736         loff_t size = udf_ext0_offset(dir) + dir->i_size;
737         int block;
738         struct kernel_lb_addr eloc;
739         uint32_t elen;
740         sector_t offset;
741         struct extent_position epos = {};
742         struct udf_inode_info *dinfo = UDF_I(dir);
743
744         f_pos = udf_ext0_offset(dir);
745         fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
746
747         if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
748                 fibh.sbh = fibh.ebh = NULL;
749         else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
750                               &epos, &eloc, &elen, &offset) ==
751                                         (EXT_RECORDED_ALLOCATED >> 30)) {
752                 block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
753                 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
754                         if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
755                                 epos.offset -= sizeof(struct short_ad);
756                         else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
757                                 epos.offset -= sizeof(struct long_ad);
758                 } else
759                         offset = 0;
760
761                 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
762                 if (!fibh.sbh) {
763                         brelse(epos.bh);
764                         return 0;
765                 }
766         } else {
767                 brelse(epos.bh);
768                 return 0;
769         }
770
771         while (f_pos < size) {
772                 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
773                                         &elen, &offset);
774                 if (!fi) {
775                         if (fibh.sbh != fibh.ebh)
776                                 brelse(fibh.ebh);
777                         brelse(fibh.sbh);
778                         brelse(epos.bh);
779                         return 0;
780                 }
781
782                 if (cfi.lengthFileIdent &&
783                     (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
784                         if (fibh.sbh != fibh.ebh)
785                                 brelse(fibh.ebh);
786                         brelse(fibh.sbh);
787                         brelse(epos.bh);
788                         return 0;
789                 }
790         }
791
792         if (fibh.sbh != fibh.ebh)
793                 brelse(fibh.ebh);
794         brelse(fibh.sbh);
795         brelse(epos.bh);
796
797         return 1;
798 }
799
800 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
801 {
802         int retval;
803         struct inode *inode = dentry->d_inode;
804         struct udf_fileident_bh fibh;
805         struct fileIdentDesc *fi, cfi;
806         struct kernel_lb_addr tloc;
807
808         dquot_initialize(dir);
809
810         retval = -ENOENT;
811         lock_kernel();
812         fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
813         if (!fi)
814                 goto out;
815
816         retval = -EIO;
817         tloc = lelb_to_cpu(cfi.icb.extLocation);
818         if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
819                 goto end_rmdir;
820         retval = -ENOTEMPTY;
821         if (!empty_dir(inode))
822                 goto end_rmdir;
823         retval = udf_delete_entry(dir, fi, &fibh, &cfi);
824         if (retval)
825                 goto end_rmdir;
826         if (inode->i_nlink != 2)
827                 udf_warning(inode->i_sb, "udf_rmdir",
828                             "empty directory has nlink != 2 (%d)",
829                             inode->i_nlink);
830         clear_nlink(inode);
831         inode->i_size = 0;
832         inode_dec_link_count(dir);
833         inode->i_ctime = dir->i_ctime = dir->i_mtime =
834                                                 current_fs_time(dir->i_sb);
835         mark_inode_dirty(dir);
836
837 end_rmdir:
838         if (fibh.sbh != fibh.ebh)
839                 brelse(fibh.ebh);
840         brelse(fibh.sbh);
841
842 out:
843         unlock_kernel();
844         return retval;
845 }
846
847 static int udf_unlink(struct inode *dir, struct dentry *dentry)
848 {
849         int retval;
850         struct inode *inode = dentry->d_inode;
851         struct udf_fileident_bh fibh;
852         struct fileIdentDesc *fi;
853         struct fileIdentDesc cfi;
854         struct kernel_lb_addr tloc;
855
856         dquot_initialize(dir);
857
858         retval = -ENOENT;
859         lock_kernel();
860         fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
861         if (!fi)
862                 goto out;
863
864         retval = -EIO;
865         tloc = lelb_to_cpu(cfi.icb.extLocation);
866         if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
867                 goto end_unlink;
868
869         if (!inode->i_nlink) {
870                 udf_debug("Deleting nonexistent file (%lu), %d\n",
871                           inode->i_ino, inode->i_nlink);
872                 inode->i_nlink = 1;
873         }
874         retval = udf_delete_entry(dir, fi, &fibh, &cfi);
875         if (retval)
876                 goto end_unlink;
877         dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
878         mark_inode_dirty(dir);
879         inode_dec_link_count(inode);
880         inode->i_ctime = dir->i_ctime;
881         retval = 0;
882
883 end_unlink:
884         if (fibh.sbh != fibh.ebh)
885                 brelse(fibh.ebh);
886         brelse(fibh.sbh);
887
888 out:
889         unlock_kernel();
890         return retval;
891 }
892
893 static int udf_symlink(struct inode *dir, struct dentry *dentry,
894                        const char *symname)
895 {
896         struct inode *inode;
897         struct pathComponent *pc;
898         const char *compstart;
899         struct udf_fileident_bh fibh;
900         struct extent_position epos = {};
901         int eoffset, elen = 0;
902         struct fileIdentDesc *fi;
903         struct fileIdentDesc cfi;
904         uint8_t *ea;
905         int err;
906         int block;
907         unsigned char *name = NULL;
908         int namelen;
909         struct buffer_head *bh;
910         struct udf_inode_info *iinfo;
911
912         dquot_initialize(dir);
913
914         lock_kernel();
915         inode = udf_new_inode(dir, S_IFLNK, &err);
916         if (!inode)
917                 goto out;
918
919         name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
920         if (!name) {
921                 err = -ENOMEM;
922                 goto out_no_entry;
923         }
924
925         iinfo = UDF_I(inode);
926         inode->i_mode = S_IFLNK | S_IRWXUGO;
927         inode->i_data.a_ops = &udf_symlink_aops;
928         inode->i_op = &page_symlink_inode_operations;
929
930         if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
931                 struct kernel_lb_addr eloc;
932                 uint32_t bsize;
933
934                 block = udf_new_block(inode->i_sb, inode,
935                                 iinfo->i_location.partitionReferenceNum,
936                                 iinfo->i_location.logicalBlockNum, &err);
937                 if (!block)
938                         goto out_no_entry;
939                 epos.block = iinfo->i_location;
940                 epos.offset = udf_file_entry_alloc_offset(inode);
941                 epos.bh = NULL;
942                 eloc.logicalBlockNum = block;
943                 eloc.partitionReferenceNum =
944                                 iinfo->i_location.partitionReferenceNum;
945                 bsize = inode->i_sb->s_blocksize;
946                 iinfo->i_lenExtents = bsize;
947                 udf_add_aext(inode, &epos, &eloc, bsize, 0);
948                 brelse(epos.bh);
949
950                 block = udf_get_pblock(inode->i_sb, block,
951                                 iinfo->i_location.partitionReferenceNum,
952                                 0);
953                 epos.bh = udf_tgetblk(inode->i_sb, block);
954                 lock_buffer(epos.bh);
955                 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
956                 set_buffer_uptodate(epos.bh);
957                 unlock_buffer(epos.bh);
958                 mark_buffer_dirty_inode(epos.bh, inode);
959                 ea = epos.bh->b_data + udf_ext0_offset(inode);
960         } else
961                 ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
962
963         eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
964         pc = (struct pathComponent *)ea;
965
966         if (*symname == '/') {
967                 do {
968                         symname++;
969                 } while (*symname == '/');
970
971                 pc->componentType = 1;
972                 pc->lengthComponentIdent = 0;
973                 pc->componentFileVersionNum = 0;
974                 elen += sizeof(struct pathComponent);
975         }
976
977         err = -ENAMETOOLONG;
978
979         while (*symname) {
980                 if (elen + sizeof(struct pathComponent) > eoffset)
981                         goto out_no_entry;
982
983                 pc = (struct pathComponent *)(ea + elen);
984
985                 compstart = symname;
986
987                 do {
988                         symname++;
989                 } while (*symname && *symname != '/');
990
991                 pc->componentType = 5;
992                 pc->lengthComponentIdent = 0;
993                 pc->componentFileVersionNum = 0;
994                 if (compstart[0] == '.') {
995                         if ((symname - compstart) == 1)
996                                 pc->componentType = 4;
997                         else if ((symname - compstart) == 2 &&
998                                         compstart[1] == '.')
999                                 pc->componentType = 3;
1000                 }
1001
1002                 if (pc->componentType == 5) {
1003                         namelen = udf_put_filename(inode->i_sb, compstart, name,
1004                                                    symname - compstart);
1005                         if (!namelen)
1006                                 goto out_no_entry;
1007
1008                         if (elen + sizeof(struct pathComponent) + namelen >
1009                                         eoffset)
1010                                 goto out_no_entry;
1011                         else
1012                                 pc->lengthComponentIdent = namelen;
1013
1014                         memcpy(pc->componentIdent, name, namelen);
1015                 }
1016
1017                 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1018
1019                 if (*symname) {
1020                         do {
1021                                 symname++;
1022                         } while (*symname == '/');
1023                 }
1024         }
1025
1026         brelse(epos.bh);
1027         inode->i_size = elen;
1028         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1029                 iinfo->i_lenAlloc = inode->i_size;
1030         else
1031                 udf_truncate_tail_extent(inode);
1032         mark_inode_dirty(inode);
1033
1034         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1035         if (!fi)
1036                 goto out_no_entry;
1037         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1038         cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
1039         bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1040         if (bh) {
1041                 struct logicalVolIntegrityDesc *lvid =
1042                                 (struct logicalVolIntegrityDesc *)bh->b_data;
1043                 struct logicalVolHeaderDesc *lvhd;
1044                 uint64_t uniqueID;
1045                 lvhd = (struct logicalVolHeaderDesc *)
1046                                 lvid->logicalVolContentsUse;
1047                 uniqueID = le64_to_cpu(lvhd->uniqueID);
1048                 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1049                         cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1050                 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1051                         uniqueID += 16;
1052                 lvhd->uniqueID = cpu_to_le64(uniqueID);
1053                 mark_buffer_dirty(bh);
1054         }
1055         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1056         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1057                 mark_inode_dirty(dir);
1058         if (fibh.sbh != fibh.ebh)
1059                 brelse(fibh.ebh);
1060         brelse(fibh.sbh);
1061         d_instantiate(dentry, inode);
1062         err = 0;
1063
1064 out:
1065         kfree(name);
1066         unlock_kernel();
1067         return err;
1068
1069 out_no_entry:
1070         inode_dec_link_count(inode);
1071         iput(inode);
1072         goto out;
1073 }
1074
1075 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1076                     struct dentry *dentry)
1077 {
1078         struct inode *inode = old_dentry->d_inode;
1079         struct udf_fileident_bh fibh;
1080         struct fileIdentDesc cfi, *fi;
1081         int err;
1082         struct buffer_head *bh;
1083
1084         dquot_initialize(dir);
1085
1086         lock_kernel();
1087         if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1088                 unlock_kernel();
1089                 return -EMLINK;
1090         }
1091
1092         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1093         if (!fi) {
1094                 unlock_kernel();
1095                 return err;
1096         }
1097         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1098         cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1099         bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1100         if (bh) {
1101                 struct logicalVolIntegrityDesc *lvid =
1102                                 (struct logicalVolIntegrityDesc *)bh->b_data;
1103                 struct logicalVolHeaderDesc *lvhd;
1104                 uint64_t uniqueID;
1105                 lvhd = (struct logicalVolHeaderDesc *)
1106                                 (lvid->logicalVolContentsUse);
1107                 uniqueID = le64_to_cpu(lvhd->uniqueID);
1108                 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1109                         cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1110                 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1111                         uniqueID += 16;
1112                 lvhd->uniqueID = cpu_to_le64(uniqueID);
1113                 mark_buffer_dirty(bh);
1114         }
1115         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1116         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1117                 mark_inode_dirty(dir);
1118
1119         if (fibh.sbh != fibh.ebh)
1120                 brelse(fibh.ebh);
1121         brelse(fibh.sbh);
1122         inc_nlink(inode);
1123         inode->i_ctime = current_fs_time(inode->i_sb);
1124         mark_inode_dirty(inode);
1125         atomic_inc(&inode->i_count);
1126         d_instantiate(dentry, inode);
1127         unlock_kernel();
1128
1129         return 0;
1130 }
1131
1132 /* Anybody can rename anything with this: the permission checks are left to the
1133  * higher-level routines.
1134  */
1135 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1136                       struct inode *new_dir, struct dentry *new_dentry)
1137 {
1138         struct inode *old_inode = old_dentry->d_inode;
1139         struct inode *new_inode = new_dentry->d_inode;
1140         struct udf_fileident_bh ofibh, nfibh;
1141         struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1142         struct fileIdentDesc ocfi, ncfi;
1143         struct buffer_head *dir_bh = NULL;
1144         int retval = -ENOENT;
1145         struct kernel_lb_addr tloc;
1146         struct udf_inode_info *old_iinfo = UDF_I(old_inode);
1147
1148         dquot_initialize(old_dir);
1149         dquot_initialize(new_dir);
1150
1151         lock_kernel();
1152         ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
1153         if (ofi) {
1154                 if (ofibh.sbh != ofibh.ebh)
1155                         brelse(ofibh.ebh);
1156                 brelse(ofibh.sbh);
1157         }
1158         tloc = lelb_to_cpu(ocfi.icb.extLocation);
1159         if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1160             != old_inode->i_ino)
1161                 goto end_rename;
1162
1163         nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1164         if (nfi) {
1165                 if (!new_inode) {
1166                         if (nfibh.sbh != nfibh.ebh)
1167                                 brelse(nfibh.ebh);
1168                         brelse(nfibh.sbh);
1169                         nfi = NULL;
1170                 }
1171         }
1172         if (S_ISDIR(old_inode->i_mode)) {
1173                 int offset = udf_ext0_offset(old_inode);
1174
1175                 if (new_inode) {
1176                         retval = -ENOTEMPTY;
1177                         if (!empty_dir(new_inode))
1178                                 goto end_rename;
1179                 }
1180                 retval = -EIO;
1181                 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1182                         dir_fi = udf_get_fileident(
1183                                         old_iinfo->i_ext.i_data -
1184                                           (old_iinfo->i_efe ?
1185                                            sizeof(struct extendedFileEntry) :
1186                                            sizeof(struct fileEntry)),
1187                                         old_inode->i_sb->s_blocksize, &offset);
1188                 } else {
1189                         dir_bh = udf_bread(old_inode, 0, 0, &retval);
1190                         if (!dir_bh)
1191                                 goto end_rename;
1192                         dir_fi = udf_get_fileident(dir_bh->b_data,
1193                                         old_inode->i_sb->s_blocksize, &offset);
1194                 }
1195                 if (!dir_fi)
1196                         goto end_rename;
1197                 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1198                 if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
1199                                 old_dir->i_ino)
1200                         goto end_rename;
1201
1202                 retval = -EMLINK;
1203                 if (!new_inode &&
1204                         new_dir->i_nlink >=
1205                                 (256 << sizeof(new_dir->i_nlink)) - 1)
1206                         goto end_rename;
1207         }
1208         if (!nfi) {
1209                 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1210                                     &retval);
1211                 if (!nfi)
1212                         goto end_rename;
1213         }
1214
1215         /*
1216          * Like most other Unix systems, set the ctime for inodes on a
1217          * rename.
1218          */
1219         old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1220         mark_inode_dirty(old_inode);
1221
1222         /*
1223          * ok, that's it
1224          */
1225         ncfi.fileVersionNum = ocfi.fileVersionNum;
1226         ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1227         memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(struct long_ad));
1228         udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1229
1230         /* The old fid may have moved - find it again */
1231         ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
1232         udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1233
1234         if (new_inode) {
1235                 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1236                 inode_dec_link_count(new_inode);
1237         }
1238         old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1239         mark_inode_dirty(old_dir);
1240
1241         if (dir_fi) {
1242                 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1243                 udf_update_tag((char *)dir_fi,
1244                                 (sizeof(struct fileIdentDesc) +
1245                                 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1246                 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1247                         mark_inode_dirty(old_inode);
1248                 else
1249                         mark_buffer_dirty_inode(dir_bh, old_inode);
1250
1251                 inode_dec_link_count(old_dir);
1252                 if (new_inode)
1253                         inode_dec_link_count(new_inode);
1254                 else {
1255                         inc_nlink(new_dir);
1256                         mark_inode_dirty(new_dir);
1257                 }
1258         }
1259
1260         if (ofi) {
1261                 if (ofibh.sbh != ofibh.ebh)
1262                         brelse(ofibh.ebh);
1263                 brelse(ofibh.sbh);
1264         }
1265
1266         retval = 0;
1267
1268 end_rename:
1269         brelse(dir_bh);
1270         if (nfi) {
1271                 if (nfibh.sbh != nfibh.ebh)
1272                         brelse(nfibh.ebh);
1273                 brelse(nfibh.sbh);
1274         }
1275         unlock_kernel();
1276
1277         return retval;
1278 }
1279
1280 static struct dentry *udf_get_parent(struct dentry *child)
1281 {
1282         struct kernel_lb_addr tloc;
1283         struct inode *inode = NULL;
1284         struct qstr dotdot = {.name = "..", .len = 2};
1285         struct fileIdentDesc cfi;
1286         struct udf_fileident_bh fibh;
1287
1288         lock_kernel();
1289         if (!udf_find_entry(child->d_inode, &dotdot, &fibh, &cfi))
1290                 goto out_unlock;
1291
1292         if (fibh.sbh != fibh.ebh)
1293                 brelse(fibh.ebh);
1294         brelse(fibh.sbh);
1295
1296         tloc = lelb_to_cpu(cfi.icb.extLocation);
1297         inode = udf_iget(child->d_inode->i_sb, &tloc);
1298         if (!inode)
1299                 goto out_unlock;
1300         unlock_kernel();
1301
1302         return d_obtain_alias(inode);
1303 out_unlock:
1304         unlock_kernel();
1305         return ERR_PTR(-EACCES);
1306 }
1307
1308
1309 static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
1310                                         u16 partref, __u32 generation)
1311 {
1312         struct inode *inode;
1313         struct kernel_lb_addr loc;
1314
1315         if (block == 0)
1316                 return ERR_PTR(-ESTALE);
1317
1318         loc.logicalBlockNum = block;
1319         loc.partitionReferenceNum = partref;
1320         inode = udf_iget(sb, &loc);
1321
1322         if (inode == NULL)
1323                 return ERR_PTR(-ENOMEM);
1324
1325         if (generation && inode->i_generation != generation) {
1326                 iput(inode);
1327                 return ERR_PTR(-ESTALE);
1328         }
1329         return d_obtain_alias(inode);
1330 }
1331
1332 static struct dentry *udf_fh_to_dentry(struct super_block *sb,
1333                                        struct fid *fid, int fh_len, int fh_type)
1334 {
1335         if ((fh_len != 3 && fh_len != 5) ||
1336             (fh_type != FILEID_UDF_WITH_PARENT &&
1337              fh_type != FILEID_UDF_WITHOUT_PARENT))
1338                 return NULL;
1339
1340         return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
1341                         fid->udf.generation);
1342 }
1343
1344 static struct dentry *udf_fh_to_parent(struct super_block *sb,
1345                                        struct fid *fid, int fh_len, int fh_type)
1346 {
1347         if (fh_len != 5 || fh_type != FILEID_UDF_WITH_PARENT)
1348                 return NULL;
1349
1350         return udf_nfs_get_inode(sb, fid->udf.parent_block,
1351                                  fid->udf.parent_partref,
1352                                  fid->udf.parent_generation);
1353 }
1354 static int udf_encode_fh(struct dentry *de, __u32 *fh, int *lenp,
1355                          int connectable)
1356 {
1357         int len = *lenp;
1358         struct inode *inode =  de->d_inode;
1359         struct kernel_lb_addr location = UDF_I(inode)->i_location;
1360         struct fid *fid = (struct fid *)fh;
1361         int type = FILEID_UDF_WITHOUT_PARENT;
1362
1363         if (len < 3 || (connectable && len < 5))
1364                 return 255;
1365
1366         *lenp = 3;
1367         fid->udf.block = location.logicalBlockNum;
1368         fid->udf.partref = location.partitionReferenceNum;
1369         fid->udf.generation = inode->i_generation;
1370
1371         if (connectable && !S_ISDIR(inode->i_mode)) {
1372                 spin_lock(&de->d_lock);
1373                 inode = de->d_parent->d_inode;
1374                 location = UDF_I(inode)->i_location;
1375                 fid->udf.parent_block = location.logicalBlockNum;
1376                 fid->udf.parent_partref = location.partitionReferenceNum;
1377                 fid->udf.parent_generation = inode->i_generation;
1378                 spin_unlock(&de->d_lock);
1379                 *lenp = 5;
1380                 type = FILEID_UDF_WITH_PARENT;
1381         }
1382
1383         return type;
1384 }
1385
1386 const struct export_operations udf_export_ops = {
1387         .encode_fh      = udf_encode_fh,
1388         .fh_to_dentry   = udf_fh_to_dentry,
1389         .fh_to_parent   = udf_fh_to_parent,
1390         .get_parent     = udf_get_parent,
1391 };
1392
1393 const struct inode_operations udf_dir_inode_operations = {
1394         .lookup                         = udf_lookup,
1395         .create                         = udf_create,
1396         .link                           = udf_link,
1397         .unlink                         = udf_unlink,
1398         .symlink                        = udf_symlink,
1399         .mkdir                          = udf_mkdir,
1400         .rmdir                          = udf_rmdir,
1401         .mknod                          = udf_mknod,
1402         .rename                         = udf_rename,
1403 };