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