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