udf: remove UDF_I_* macros and open code them
[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
162         size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
163         f_pos = (udf_ext0_offset(dir) >> 2);
164
165         fibh->soffset = fibh->eoffset =
166                 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
167         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
168                 fibh->sbh = fibh->ebh = NULL;
169         else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
170                               &epos, &eloc, &elen, &offset) ==
171                                         (EXT_RECORDED_ALLOCATED >> 30)) {
172                 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
173                 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
174                         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
175                                 epos.offset -= sizeof(short_ad);
176                         else if (UDF_I(dir)->i_alloc_type ==
177                                                         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
355         if (dentry) {
356                 if (!dentry->d_name.len) {
357                         *err = -EINVAL;
358                         return NULL;
359                 }
360                 namelen = udf_put_filename(sb, dentry->d_name.name, name,
361                                                  dentry->d_name.len);
362                 if (!namelen) {
363                         *err = -ENAMETOOLONG;
364                         return NULL;
365                 }
366         } else {
367                 namelen = 0;
368         }
369
370         nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
371
372         f_pos = (udf_ext0_offset(dir) >> 2);
373
374         fibh->soffset = fibh->eoffset =
375                         (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
376         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
377                 fibh->sbh = fibh->ebh = NULL;
378         else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
379                               &epos, &eloc, &elen, &offset) ==
380                                         (EXT_RECORDED_ALLOCATED >> 30)) {
381                 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
382                 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
383                         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
384                                 epos.offset -= sizeof(short_ad);
385                         else if (UDF_I(dir)->i_alloc_type ==
386                                                         ICBTAG_FLAG_AD_LONG)
387                                 epos.offset -= sizeof(long_ad);
388                 } else
389                         offset = 0;
390
391                 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
392                 if (!fibh->sbh) {
393                         brelse(epos.bh);
394                         *err = -EIO;
395                         return NULL;
396                 }
397
398                 block = UDF_I(dir)->i_location.logicalBlockNum;
399
400         } else {
401                 block = udf_get_lb_pblock(dir->i_sb, UDF_I(dir)->i_location, 0);
402                 fibh->sbh = fibh->ebh = NULL;
403                 fibh->soffset = fibh->eoffset = sb->s_blocksize;
404                 goto add;
405         }
406
407         while ((f_pos < size)) {
408                 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
409                                         &elen, &offset);
410
411                 if (!fi) {
412                         if (fibh->sbh != fibh->ebh)
413                                 brelse(fibh->ebh);
414                         brelse(fibh->sbh);
415                         brelse(epos.bh);
416                         *err = -EIO;
417                         return NULL;
418                 }
419
420                 liu = le16_to_cpu(cfi->lengthOfImpUse);
421                 lfi = cfi->lengthFileIdent;
422
423                 if (fibh->sbh == fibh->ebh)
424                         nameptr = fi->fileIdent + liu;
425                 else {
426                         int poffset;    /* Unpaded ending offset */
427
428                         poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
429                                         liu + lfi;
430
431                         if (poffset >= lfi)
432                                 nameptr = (char *)(fibh->ebh->b_data +
433                                                    poffset - lfi);
434                         else {
435                                 nameptr = fname;
436                                 memcpy(nameptr, fi->fileIdent + liu,
437                                         lfi - poffset);
438                                 memcpy(nameptr + lfi - poffset,
439                                         fibh->ebh->b_data, poffset);
440                         }
441                 }
442
443                 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
444                         if (((sizeof(struct fileIdentDesc) +
445                                         liu + lfi + 3) & ~3) == nfidlen) {
446                                 brelse(epos.bh);
447                                 cfi->descTag.tagSerialNum = cpu_to_le16(1);
448                                 cfi->fileVersionNum = cpu_to_le16(1);
449                                 cfi->fileCharacteristics = 0;
450                                 cfi->lengthFileIdent = namelen;
451                                 cfi->lengthOfImpUse = cpu_to_le16(0);
452                                 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
453                                                   name))
454                                         return fi;
455                                 else {
456                                         *err = -EIO;
457                                         return NULL;
458                                 }
459                         }
460                 }
461
462                 if (!lfi || !dentry)
463                         continue;
464
465                 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
466                 if (flen && udf_match(flen, fname, dentry->d_name.len,
467                                       dentry->d_name.name)) {
468                         if (fibh->sbh != fibh->ebh)
469                                 brelse(fibh->ebh);
470                         brelse(fibh->sbh);
471                         brelse(epos.bh);
472                         *err = -EEXIST;
473                         return NULL;
474                 }
475         }
476
477 add:
478         f_pos += nfidlen;
479
480         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
481             sb->s_blocksize - fibh->eoffset < nfidlen) {
482                 brelse(epos.bh);
483                 epos.bh = NULL;
484                 fibh->soffset -= udf_ext0_offset(dir);
485                 fibh->eoffset -= udf_ext0_offset(dir);
486                 f_pos -= (udf_ext0_offset(dir) >> 2);
487                 if (fibh->sbh != fibh->ebh)
488                         brelse(fibh->ebh);
489                 brelse(fibh->sbh);
490                 fibh->sbh = fibh->ebh =
491                                 udf_expand_dir_adinicb(dir, &block, err);
492                 if (!fibh->sbh)
493                         return NULL;
494                 epos.block = UDF_I(dir)->i_location;
495                 eloc.logicalBlockNum = block;
496                 eloc.partitionReferenceNum =
497                                 UDF_I(dir)->i_location.partitionReferenceNum;
498                 elen = dir->i_sb->s_blocksize;
499                 epos.offset = udf_file_entry_alloc_offset(dir);
500                 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
501                         epos.offset += sizeof(short_ad);
502                 else if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
503                         epos.offset += sizeof(long_ad);
504         }
505
506         if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
507                 fibh->soffset = fibh->eoffset;
508                 fibh->eoffset += nfidlen;
509                 if (fibh->sbh != fibh->ebh) {
510                         brelse(fibh->sbh);
511                         fibh->sbh = fibh->ebh;
512                 }
513
514                 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
515                         block = UDF_I(dir)->i_location.logicalBlockNum;
516                         fi = (struct fileIdentDesc *)
517                                         (UDF_I(dir)->i_ext.i_data +
518                                          fibh->soffset -
519                                          udf_ext0_offset(dir) +
520                                          UDF_I(dir)->i_lenEAttr);
521                 } else {
522                         block = eloc.logicalBlockNum +
523                                         ((elen - 1) >>
524                                                 dir->i_sb->s_blocksize_bits);
525                         fi = (struct fileIdentDesc *)
526                                 (fibh->sbh->b_data + fibh->soffset);
527                 }
528         } else {
529                 fibh->soffset = fibh->eoffset - sb->s_blocksize;
530                 fibh->eoffset += nfidlen - sb->s_blocksize;
531                 if (fibh->sbh != fibh->ebh) {
532                         brelse(fibh->sbh);
533                         fibh->sbh = fibh->ebh;
534                 }
535
536                 block = eloc.logicalBlockNum + ((elen - 1) >>
537                                                 dir->i_sb->s_blocksize_bits);
538                 fibh->ebh = udf_bread(dir,
539                                 f_pos >> (dir->i_sb->s_blocksize_bits - 2),
540                                 1, err);
541                 if (!fibh->ebh) {
542                         brelse(epos.bh);
543                         brelse(fibh->sbh);
544                         return NULL;
545                 }
546
547                 if (!fibh->soffset) {
548                         if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
549                             (EXT_RECORDED_ALLOCATED >> 30)) {
550                                 block = eloc.logicalBlockNum + ((elen - 1) >>
551                                         dir->i_sb->s_blocksize_bits);
552                         } else
553                                 block++;
554
555                         brelse(fibh->sbh);
556                         fibh->sbh = fibh->ebh;
557                         fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
558                 } else {
559                         fi = (struct fileIdentDesc *)
560                                 (fibh->sbh->b_data + sb->s_blocksize +
561                                         fibh->soffset);
562                 }
563         }
564
565         memset(cfi, 0, sizeof(struct fileIdentDesc));
566         if (UDF_SB(sb)->s_udfrev >= 0x0200)
567                 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
568                             sizeof(tag));
569         else
570                 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
571                             sizeof(tag));
572         cfi->fileVersionNum = cpu_to_le16(1);
573         cfi->lengthFileIdent = namelen;
574         cfi->lengthOfImpUse = cpu_to_le16(0);
575         if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
576                 brelse(epos.bh);
577                 dir->i_size += nfidlen;
578                 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
579                         UDF_I(dir)->i_lenAlloc += nfidlen;
580                 mark_inode_dirty(dir);
581                 return fi;
582         } else {
583                 brelse(epos.bh);
584                 if (fibh->sbh != fibh->ebh)
585                         brelse(fibh->ebh);
586                 brelse(fibh->sbh);
587                 *err = -EIO;
588                 return NULL;
589         }
590 }
591
592 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
593                             struct udf_fileident_bh *fibh,
594                             struct fileIdentDesc *cfi)
595 {
596         cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
597
598         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
599                 memset(&(cfi->icb), 0x00, sizeof(long_ad));
600
601         return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
602 }
603
604 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
605                       struct nameidata *nd)
606 {
607         struct udf_fileident_bh fibh;
608         struct inode *inode;
609         struct fileIdentDesc cfi, *fi;
610         int err;
611
612         lock_kernel();
613         inode = udf_new_inode(dir, mode, &err);
614         if (!inode) {
615                 unlock_kernel();
616                 return err;
617         }
618
619         if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
620                 inode->i_data.a_ops = &udf_adinicb_aops;
621         else
622                 inode->i_data.a_ops = &udf_aops;
623         inode->i_op = &udf_file_inode_operations;
624         inode->i_fop = &udf_file_operations;
625         inode->i_mode = mode;
626         mark_inode_dirty(inode);
627
628         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
629         if (!fi) {
630                 inode->i_nlink--;
631                 mark_inode_dirty(inode);
632                 iput(inode);
633                 unlock_kernel();
634                 return err;
635         }
636         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
637         cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
638         *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
639                 cpu_to_le32(UDF_I(inode)->i_unique & 0x00000000FFFFFFFFUL);
640         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
641         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
642                 mark_inode_dirty(dir);
643         if (fibh.sbh != fibh.ebh)
644                 brelse(fibh.ebh);
645         brelse(fibh.sbh);
646         unlock_kernel();
647         d_instantiate(dentry, inode);
648
649         return 0;
650 }
651
652 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
653                      dev_t rdev)
654 {
655         struct inode *inode;
656         struct udf_fileident_bh fibh;
657         struct fileIdentDesc cfi, *fi;
658         int err;
659
660         if (!old_valid_dev(rdev))
661                 return -EINVAL;
662
663         lock_kernel();
664         err = -EIO;
665         inode = udf_new_inode(dir, mode, &err);
666         if (!inode)
667                 goto out;
668
669         inode->i_uid = current->fsuid;
670         init_special_inode(inode, mode, rdev);
671         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
672         if (!fi) {
673                 inode->i_nlink--;
674                 mark_inode_dirty(inode);
675                 iput(inode);
676                 unlock_kernel();
677                 return err;
678         }
679         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
680         cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
681         *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
682                 cpu_to_le32(UDF_I(inode)->i_unique & 0x00000000FFFFFFFFUL);
683         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
684         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
685                 mark_inode_dirty(dir);
686         mark_inode_dirty(inode);
687
688         if (fibh.sbh != fibh.ebh)
689                 brelse(fibh.ebh);
690         brelse(fibh.sbh);
691         d_instantiate(dentry, inode);
692         err = 0;
693
694 out:
695         unlock_kernel();
696         return err;
697 }
698
699 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
700 {
701         struct inode *inode;
702         struct udf_fileident_bh fibh;
703         struct fileIdentDesc cfi, *fi;
704         int err;
705
706         lock_kernel();
707         err = -EMLINK;
708         if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
709                 goto out;
710
711         err = -EIO;
712         inode = udf_new_inode(dir, S_IFDIR, &err);
713         if (!inode)
714                 goto out;
715
716         inode->i_op = &udf_dir_inode_operations;
717         inode->i_fop = &udf_dir_operations;
718         fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
719         if (!fi) {
720                 inode->i_nlink--;
721                 mark_inode_dirty(inode);
722                 iput(inode);
723                 goto out;
724         }
725         inode->i_nlink = 2;
726         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
727         cfi.icb.extLocation = cpu_to_lelb(UDF_I(dir)->i_location);
728         *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
729                 cpu_to_le32(UDF_I(dir)->i_unique & 0x00000000FFFFFFFFUL);
730         cfi.fileCharacteristics =
731                         FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
732         udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
733         brelse(fibh.sbh);
734         inode->i_mode = S_IFDIR | mode;
735         if (dir->i_mode & S_ISGID)
736                 inode->i_mode |= S_ISGID;
737         mark_inode_dirty(inode);
738
739         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
740         if (!fi) {
741                 inode->i_nlink = 0;
742                 mark_inode_dirty(inode);
743                 iput(inode);
744                 goto out;
745         }
746         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
747         cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
748         *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
749                 cpu_to_le32(UDF_I(inode)->i_unique & 0x00000000FFFFFFFFUL);
750         cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
751         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
752         inc_nlink(dir);
753         mark_inode_dirty(dir);
754         d_instantiate(dentry, inode);
755         if (fibh.sbh != fibh.ebh)
756                 brelse(fibh.ebh);
757         brelse(fibh.sbh);
758         err = 0;
759
760 out:
761         unlock_kernel();
762         return err;
763 }
764
765 static int empty_dir(struct inode *dir)
766 {
767         struct fileIdentDesc *fi, cfi;
768         struct udf_fileident_bh fibh;
769         loff_t f_pos;
770         loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
771         int block;
772         kernel_lb_addr eloc;
773         uint32_t elen;
774         sector_t offset;
775         struct extent_position epos = {};
776
777         f_pos = (udf_ext0_offset(dir) >> 2);
778
779         fibh.soffset = fibh.eoffset =
780                         (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
781
782         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
783                 fibh.sbh = fibh.ebh = NULL;
784         else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
785                               &epos, &eloc, &elen, &offset) ==
786                                         (EXT_RECORDED_ALLOCATED >> 30)) {
787                 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
788                 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
789                         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
790                                 epos.offset -= sizeof(short_ad);
791                         else if (UDF_I(dir)->i_alloc_type ==
792                                                         ICBTAG_FLAG_AD_LONG)
793                                 epos.offset -= sizeof(long_ad);
794                 } else
795                         offset = 0;
796
797                 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
798                 if (!fibh.sbh) {
799                         brelse(epos.bh);
800                         return 0;
801                 }
802         } else {
803                 brelse(epos.bh);
804                 return 0;
805         }
806
807         while ((f_pos < size)) {
808                 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
809                                         &elen, &offset);
810                 if (!fi) {
811                         if (fibh.sbh != fibh.ebh)
812                                 brelse(fibh.ebh);
813                         brelse(fibh.sbh);
814                         brelse(epos.bh);
815                         return 0;
816                 }
817
818                 if (cfi.lengthFileIdent &&
819                     (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
820                         if (fibh.sbh != fibh.ebh)
821                                 brelse(fibh.ebh);
822                         brelse(fibh.sbh);
823                         brelse(epos.bh);
824                         return 0;
825                 }
826         }
827
828         if (fibh.sbh != fibh.ebh)
829                 brelse(fibh.ebh);
830         brelse(fibh.sbh);
831         brelse(epos.bh);
832
833         return 1;
834 }
835
836 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
837 {
838         int retval;
839         struct inode *inode = dentry->d_inode;
840         struct udf_fileident_bh fibh;
841         struct fileIdentDesc *fi, cfi;
842         kernel_lb_addr tloc;
843
844         retval = -ENOENT;
845         lock_kernel();
846         fi = udf_find_entry(dir, dentry, &fibh, &cfi);
847         if (!fi)
848                 goto out;
849
850         retval = -EIO;
851         tloc = lelb_to_cpu(cfi.icb.extLocation);
852         if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
853                 goto end_rmdir;
854         retval = -ENOTEMPTY;
855         if (!empty_dir(inode))
856                 goto end_rmdir;
857         retval = udf_delete_entry(dir, fi, &fibh, &cfi);
858         if (retval)
859                 goto end_rmdir;
860         if (inode->i_nlink != 2)
861                 udf_warning(inode->i_sb, "udf_rmdir",
862                             "empty directory has nlink != 2 (%d)",
863                             inode->i_nlink);
864         clear_nlink(inode);
865         inode->i_size = 0;
866         inode_dec_link_count(dir);
867         inode->i_ctime = dir->i_ctime = dir->i_mtime =
868                                                 current_fs_time(dir->i_sb);
869         mark_inode_dirty(dir);
870
871 end_rmdir:
872         if (fibh.sbh != fibh.ebh)
873                 brelse(fibh.ebh);
874         brelse(fibh.sbh);
875
876 out:
877         unlock_kernel();
878         return retval;
879 }
880
881 static int udf_unlink(struct inode *dir, struct dentry *dentry)
882 {
883         int retval;
884         struct inode *inode = dentry->d_inode;
885         struct udf_fileident_bh fibh;
886         struct fileIdentDesc *fi;
887         struct fileIdentDesc cfi;
888         kernel_lb_addr tloc;
889
890         retval = -ENOENT;
891         lock_kernel();
892         fi = udf_find_entry(dir, dentry, &fibh, &cfi);
893         if (!fi)
894                 goto out;
895
896         retval = -EIO;
897         tloc = lelb_to_cpu(cfi.icb.extLocation);
898         if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
899                 goto end_unlink;
900
901         if (!inode->i_nlink) {
902                 udf_debug("Deleting nonexistent file (%lu), %d\n",
903                           inode->i_ino, inode->i_nlink);
904                 inode->i_nlink = 1;
905         }
906         retval = udf_delete_entry(dir, fi, &fibh, &cfi);
907         if (retval)
908                 goto end_unlink;
909         dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
910         mark_inode_dirty(dir);
911         inode_dec_link_count(inode);
912         inode->i_ctime = dir->i_ctime;
913         retval = 0;
914
915 end_unlink:
916         if (fibh.sbh != fibh.ebh)
917                 brelse(fibh.ebh);
918         brelse(fibh.sbh);
919
920 out:
921         unlock_kernel();
922         return retval;
923 }
924
925 static int udf_symlink(struct inode *dir, struct dentry *dentry,
926                        const char *symname)
927 {
928         struct inode *inode;
929         struct pathComponent *pc;
930         char *compstart;
931         struct udf_fileident_bh fibh;
932         struct extent_position epos = {};
933         int eoffset, elen = 0;
934         struct fileIdentDesc *fi;
935         struct fileIdentDesc cfi;
936         char *ea;
937         int err;
938         int block;
939         char name[UDF_NAME_LEN];
940         int namelen;
941         struct buffer_head *bh;
942
943         lock_kernel();
944         inode = udf_new_inode(dir, S_IFLNK, &err);
945         if (!inode)
946                 goto out;
947
948         inode->i_mode = S_IFLNK | S_IRWXUGO;
949         inode->i_data.a_ops = &udf_symlink_aops;
950         inode->i_op = &page_symlink_inode_operations;
951
952         if (UDF_I(inode)->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
953                 kernel_lb_addr eloc;
954                 uint32_t elen;
955
956                 block = udf_new_block(inode->i_sb, inode,
957                                 UDF_I(inode)->i_location.partitionReferenceNum,
958                                 UDF_I(inode)->i_location.logicalBlockNum, &err);
959                 if (!block)
960                         goto out_no_entry;
961                 epos.block = UDF_I(inode)->i_location;
962                 epos.offset = udf_file_entry_alloc_offset(inode);
963                 epos.bh = NULL;
964                 eloc.logicalBlockNum = block;
965                 eloc.partitionReferenceNum =
966                                 UDF_I(inode)->i_location.partitionReferenceNum;
967                 elen = inode->i_sb->s_blocksize;
968                 UDF_I(inode)->i_lenExtents = elen;
969                 udf_add_aext(inode, &epos, eloc, elen, 0);
970                 brelse(epos.bh);
971
972                 block = udf_get_pblock(inode->i_sb, block,
973                                 UDF_I(inode)->i_location.partitionReferenceNum,
974                                 0);
975                 epos.bh = udf_tread(inode->i_sb, block);
976                 lock_buffer(epos.bh);
977                 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
978                 set_buffer_uptodate(epos.bh);
979                 unlock_buffer(epos.bh);
980                 mark_buffer_dirty_inode(epos.bh, inode);
981                 ea = epos.bh->b_data + udf_ext0_offset(inode);
982         } else {
983                 ea = UDF_I(inode)->i_ext.i_data + UDF_I(inode)->i_lenEAttr;
984         }
985
986         eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
987         pc = (struct pathComponent *)ea;
988
989         if (*symname == '/') {
990                 do {
991                         symname++;
992                 } while (*symname == '/');
993
994                 pc->componentType = 1;
995                 pc->lengthComponentIdent = 0;
996                 pc->componentFileVersionNum = 0;
997                 pc += sizeof(struct pathComponent);
998                 elen += sizeof(struct pathComponent);
999         }
1000
1001         err = -ENAMETOOLONG;
1002
1003         while (*symname) {
1004                 if (elen + sizeof(struct pathComponent) > eoffset)
1005                         goto out_no_entry;
1006
1007                 pc = (struct pathComponent *)(ea + elen);
1008
1009                 compstart = (char *)symname;
1010
1011                 do {
1012                         symname++;
1013                 } while (*symname && *symname != '/');
1014
1015                 pc->componentType = 5;
1016                 pc->lengthComponentIdent = 0;
1017                 pc->componentFileVersionNum = 0;
1018                 if (compstart[0] == '.') {
1019                         if ((symname - compstart) == 1)
1020                                 pc->componentType = 4;
1021                         else if ((symname - compstart) == 2 &&
1022                                         compstart[1] == '.')
1023                                 pc->componentType = 3;
1024                 }
1025
1026                 if (pc->componentType == 5) {
1027                         namelen = udf_put_filename(inode->i_sb, compstart, name,
1028                                                    symname - compstart);
1029                         if (!namelen)
1030                                 goto out_no_entry;
1031
1032                         if (elen + sizeof(struct pathComponent) + namelen >
1033                                         eoffset)
1034                                 goto out_no_entry;
1035                         else
1036                                 pc->lengthComponentIdent = namelen;
1037
1038                         memcpy(pc->componentIdent, name, namelen);
1039                 }
1040
1041                 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1042
1043                 if (*symname) {
1044                         do {
1045                                 symname++;
1046                         } while (*symname == '/');
1047                 }
1048         }
1049
1050         brelse(epos.bh);
1051         inode->i_size = elen;
1052         if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1053                 UDF_I(inode)->i_lenAlloc = inode->i_size;
1054         mark_inode_dirty(inode);
1055
1056         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1057         if (!fi)
1058                 goto out_no_entry;
1059         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1060         cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1061         bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1062         if (bh) {
1063                 struct logicalVolIntegrityDesc *lvid =
1064                                 (struct logicalVolIntegrityDesc *)bh->b_data;
1065                 struct logicalVolHeaderDesc *lvhd;
1066                 uint64_t uniqueID;
1067                 lvhd = (struct logicalVolHeaderDesc *)
1068                                 lvid->logicalVolContentsUse;
1069                 uniqueID = le64_to_cpu(lvhd->uniqueID);
1070                 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1071                         cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1072                 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1073                         uniqueID += 16;
1074                 lvhd->uniqueID = cpu_to_le64(uniqueID);
1075                 mark_buffer_dirty(bh);
1076         }
1077         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1078         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1079                 mark_inode_dirty(dir);
1080         if (fibh.sbh != fibh.ebh)
1081                 brelse(fibh.ebh);
1082         brelse(fibh.sbh);
1083         d_instantiate(dentry, inode);
1084         err = 0;
1085
1086 out:
1087         unlock_kernel();
1088         return err;
1089
1090 out_no_entry:
1091         inode_dec_link_count(inode);
1092         iput(inode);
1093         goto out;
1094 }
1095
1096 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1097                     struct dentry *dentry)
1098 {
1099         struct inode *inode = old_dentry->d_inode;
1100         struct udf_fileident_bh fibh;
1101         struct fileIdentDesc cfi, *fi;
1102         int err;
1103         struct buffer_head *bh;
1104
1105         lock_kernel();
1106         if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1107                 unlock_kernel();
1108                 return -EMLINK;
1109         }
1110
1111         fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1112         if (!fi) {
1113                 unlock_kernel();
1114                 return err;
1115         }
1116         cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1117         cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1118         bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1119         if (bh) {
1120                 struct logicalVolIntegrityDesc *lvid =
1121                                 (struct logicalVolIntegrityDesc *)bh->b_data;
1122                 struct logicalVolHeaderDesc *lvhd;
1123                 uint64_t uniqueID;
1124                 lvhd = (struct logicalVolHeaderDesc *)
1125                                 (lvid->logicalVolContentsUse);
1126                 uniqueID = le64_to_cpu(lvhd->uniqueID);
1127                 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1128                         cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1129                 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1130                         uniqueID += 16;
1131                 lvhd->uniqueID = cpu_to_le64(uniqueID);
1132                 mark_buffer_dirty(bh);
1133         }
1134         udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1135         if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1136                 mark_inode_dirty(dir);
1137
1138         if (fibh.sbh != fibh.ebh)
1139                 brelse(fibh.ebh);
1140         brelse(fibh.sbh);
1141         inc_nlink(inode);
1142         inode->i_ctime = current_fs_time(inode->i_sb);
1143         mark_inode_dirty(inode);
1144         atomic_inc(&inode->i_count);
1145         d_instantiate(dentry, inode);
1146         unlock_kernel();
1147
1148         return 0;
1149 }
1150
1151 /* Anybody can rename anything with this: the permission checks are left to the
1152  * higher-level routines.
1153  */
1154 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1155                       struct inode *new_dir, struct dentry *new_dentry)
1156 {
1157         struct inode *old_inode = old_dentry->d_inode;
1158         struct inode *new_inode = new_dentry->d_inode;
1159         struct udf_fileident_bh ofibh, nfibh;
1160         struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1161         struct fileIdentDesc ocfi, ncfi;
1162         struct buffer_head *dir_bh = NULL;
1163         int retval = -ENOENT;
1164         kernel_lb_addr tloc;
1165
1166         lock_kernel();
1167         ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1168         if (ofi) {
1169                 if (ofibh.sbh != ofibh.ebh)
1170                         brelse(ofibh.ebh);
1171                 brelse(ofibh.sbh);
1172         }
1173         tloc = lelb_to_cpu(ocfi.icb.extLocation);
1174         if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1175             != old_inode->i_ino)
1176                 goto end_rename;
1177
1178         nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1179         if (nfi) {
1180                 if (!new_inode) {
1181                         if (nfibh.sbh != nfibh.ebh)
1182                                 brelse(nfibh.ebh);
1183                         brelse(nfibh.sbh);
1184                         nfi = NULL;
1185                 }
1186         }
1187         if (S_ISDIR(old_inode->i_mode)) {
1188                 uint32_t offset = udf_ext0_offset(old_inode);
1189
1190                 if (new_inode) {
1191                         retval = -ENOTEMPTY;
1192                         if (!empty_dir(new_inode))
1193                                 goto end_rename;
1194                 }
1195                 retval = -EIO;
1196                 if (UDF_I(old_inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1197                         dir_fi = udf_get_fileident(
1198                                         UDF_I(old_inode)->i_ext.i_data -
1199                                           (UDF_I(old_inode)->i_efe ?
1200                                            sizeof(struct extendedFileEntry) :
1201                                            sizeof(struct fileEntry)),
1202                                         old_inode->i_sb->s_blocksize, &offset);
1203                 } else {
1204                         dir_bh = udf_bread(old_inode, 0, 0, &retval);
1205                         if (!dir_bh)
1206                                 goto end_rename;
1207                         dir_fi = udf_get_fileident(dir_bh->b_data,
1208                                         old_inode->i_sb->s_blocksize, &offset);
1209                 }
1210                 if (!dir_fi)
1211                         goto end_rename;
1212                 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1213                 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1214                                 old_dir->i_ino)
1215                         goto end_rename;
1216
1217                 retval = -EMLINK;
1218                 if (!new_inode &&
1219                         new_dir->i_nlink >=
1220                                 (256 << sizeof(new_dir->i_nlink)) - 1)
1221                         goto end_rename;
1222         }
1223         if (!nfi) {
1224                 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1225                                     &retval);
1226                 if (!nfi)
1227                         goto end_rename;
1228         }
1229
1230         /*
1231          * Like most other Unix systems, set the ctime for inodes on a
1232          * rename.
1233          */
1234         old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1235         mark_inode_dirty(old_inode);
1236
1237         /*
1238          * ok, that's it
1239          */
1240         ncfi.fileVersionNum = ocfi.fileVersionNum;
1241         ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1242         memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1243         udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1244
1245         /* The old fid may have moved - find it again */
1246         ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1247         udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1248
1249         if (new_inode) {
1250                 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1251                 inode_dec_link_count(new_inode);
1252         }
1253         old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1254         mark_inode_dirty(old_dir);
1255
1256         if (dir_fi) {
1257                 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1258                 udf_update_tag((char *)dir_fi,
1259                                 (sizeof(struct fileIdentDesc) +
1260                                 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1261                 if (UDF_I(old_inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1262                         mark_inode_dirty(old_inode);
1263                 else
1264                         mark_buffer_dirty_inode(dir_bh, old_inode);
1265
1266                 inode_dec_link_count(old_dir);
1267                 if (new_inode)
1268                         inode_dec_link_count(new_inode);
1269                 else {
1270                         inc_nlink(new_dir);
1271                         mark_inode_dirty(new_dir);
1272                 }
1273         }
1274
1275         if (ofi) {
1276                 if (ofibh.sbh != ofibh.ebh)
1277                         brelse(ofibh.ebh);
1278                 brelse(ofibh.sbh);
1279         }
1280
1281         retval = 0;
1282
1283 end_rename:
1284         brelse(dir_bh);
1285         if (nfi) {
1286                 if (nfibh.sbh != nfibh.ebh)
1287                         brelse(nfibh.ebh);
1288                 brelse(nfibh.sbh);
1289         }
1290         unlock_kernel();
1291
1292         return retval;
1293 }
1294
1295 const struct inode_operations udf_dir_inode_operations = {
1296         .lookup                         = udf_lookup,
1297         .create                         = udf_create,
1298         .link                           = udf_link,
1299         .unlink                         = udf_unlink,
1300         .symlink                        = udf_symlink,
1301         .mkdir                          = udf_mkdir,
1302         .rmdir                          = udf_rmdir,
1303         .mknod                          = udf_mknod,
1304         .rename                         = udf_rename,
1305 };