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