1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * namei.c
4 *
5 * PURPOSE
6 * Inode name handling routines for the OSTA-UDF(tm) filesystem.
7 *
8 * COPYRIGHT
9 * (C) 1998-2004 Ben Fennema
10 * (C) 1999-2000 Stelias Computing Inc
11 *
12 * HISTORY
13 *
14 * 12/12/98 blf Created. Split out the lookup code from dir.c
15 * 04/19/99 blf link, mknod, symlink support
16 */
17
18 #include "udfdecl.h"
19
20 #include "udf_i.h"
21 #include "udf_sb.h"
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include <linux/sched.h>
27 #include <linux/crc-itu-t.h>
28 #include <linux/exportfs.h>
29 #include <linux/iversion.h>
30
udf_match(int len1,const unsigned char * name1,int len2,const unsigned char * name2)31 static inline int udf_match(int len1, const unsigned char *name1, int len2,
32 const unsigned char *name2)
33 {
34 if (len1 != len2)
35 return 0;
36
37 return !memcmp(name1, name2, len1);
38 }
39
40 /**
41 * udf_fiiter_find_entry - find entry in given directory.
42 *
43 * @dir: directory inode to search in
44 * @child: qstr of the name
45 * @iter: iter to use for searching
46 *
47 * This function searches in the directory @dir for a file name @child. When
48 * found, @iter points to the position in the directory with given entry.
49 *
50 * Returns 0 on success, < 0 on error (including -ENOENT).
51 */
udf_fiiter_find_entry(struct inode * dir,const struct qstr * child,struct udf_fileident_iter * iter)52 static int udf_fiiter_find_entry(struct inode *dir, const struct qstr *child,
53 struct udf_fileident_iter *iter)
54 {
55 int flen;
56 unsigned char *fname = NULL;
57 struct super_block *sb = dir->i_sb;
58 int isdotdot = child->len == 2 &&
59 child->name[0] == '.' && child->name[1] == '.';
60 int ret;
61
62 fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
63 if (!fname)
64 return -ENOMEM;
65
66 for (ret = udf_fiiter_init(iter, dir, 0);
67 !ret && iter->pos < dir->i_size;
68 ret = udf_fiiter_advance(iter)) {
69 if (iter->fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
70 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
71 continue;
72 }
73
74 if (iter->fi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) {
75 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
76 continue;
77 }
78
79 if ((iter->fi.fileCharacteristics & FID_FILE_CHAR_PARENT) &&
80 isdotdot)
81 goto out_ok;
82
83 if (!iter->fi.lengthFileIdent)
84 continue;
85
86 flen = udf_get_filename(sb, iter->name,
87 iter->fi.lengthFileIdent, fname, UDF_NAME_LEN);
88 if (flen < 0) {
89 ret = flen;
90 goto out_err;
91 }
92
93 if (udf_match(flen, fname, child->len, child->name))
94 goto out_ok;
95 }
96 if (!ret)
97 ret = -ENOENT;
98
99 out_err:
100 udf_fiiter_release(iter);
101 out_ok:
102 kfree(fname);
103
104 return ret;
105 }
106
udf_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)107 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
108 unsigned int flags)
109 {
110 struct inode *inode = NULL;
111 struct udf_fileident_iter iter;
112 int err;
113
114 if (dentry->d_name.len > UDF_NAME_LEN)
115 return ERR_PTR(-ENAMETOOLONG);
116
117 err = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
118 if (err < 0 && err != -ENOENT)
119 return ERR_PTR(err);
120
121 if (err == 0) {
122 struct kernel_lb_addr loc;
123
124 loc = lelb_to_cpu(iter.fi.icb.extLocation);
125 udf_fiiter_release(&iter);
126
127 inode = udf_iget(dir->i_sb, &loc);
128 if (IS_ERR(inode))
129 return ERR_CAST(inode);
130 }
131
132 return d_splice_alias(inode, dentry);
133 }
134
udf_expand_dir_adinicb(struct inode * inode,udf_pblk_t * block)135 static int udf_expand_dir_adinicb(struct inode *inode, udf_pblk_t *block)
136 {
137 udf_pblk_t newblock;
138 struct buffer_head *dbh = NULL;
139 struct kernel_lb_addr eloc;
140 struct extent_position epos;
141 uint8_t alloctype;
142 struct udf_inode_info *iinfo = UDF_I(inode);
143 struct udf_fileident_iter iter;
144 uint8_t *impuse;
145 int ret;
146
147 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
148 alloctype = ICBTAG_FLAG_AD_SHORT;
149 else
150 alloctype = ICBTAG_FLAG_AD_LONG;
151
152 if (!inode->i_size) {
153 iinfo->i_alloc_type = alloctype;
154 mark_inode_dirty(inode);
155 return 0;
156 }
157
158 /* alloc block, and copy data to it */
159 *block = udf_new_block(inode->i_sb, inode,
160 iinfo->i_location.partitionReferenceNum,
161 iinfo->i_location.logicalBlockNum, &ret);
162 if (!(*block))
163 return ret;
164 newblock = udf_get_pblock(inode->i_sb, *block,
165 iinfo->i_location.partitionReferenceNum,
166 0);
167 if (newblock == 0xffffffff)
168 return -EFSCORRUPTED;
169 dbh = sb_getblk(inode->i_sb, newblock);
170 if (!dbh)
171 return -ENOMEM;
172 lock_buffer(dbh);
173 memcpy(dbh->b_data, iinfo->i_data, inode->i_size);
174 memset(dbh->b_data + inode->i_size, 0,
175 inode->i_sb->s_blocksize - inode->i_size);
176 set_buffer_uptodate(dbh);
177 unlock_buffer(dbh);
178
179 /* Drop inline data, add block instead */
180 iinfo->i_alloc_type = alloctype;
181 memset(iinfo->i_data + iinfo->i_lenEAttr, 0, iinfo->i_lenAlloc);
182 iinfo->i_lenAlloc = 0;
183 eloc.logicalBlockNum = *block;
184 eloc.partitionReferenceNum =
185 iinfo->i_location.partitionReferenceNum;
186 iinfo->i_lenExtents = inode->i_size;
187 epos.bh = NULL;
188 epos.block = iinfo->i_location;
189 epos.offset = udf_file_entry_alloc_offset(inode);
190 ret = udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
191 brelse(epos.bh);
192 if (ret < 0) {
193 brelse(dbh);
194 udf_free_blocks(inode->i_sb, inode, &eloc, 0, 1);
195 return ret;
196 }
197 mark_inode_dirty(inode);
198
199 /* Now fixup tags in moved directory entries */
200 for (ret = udf_fiiter_init(&iter, inode, 0);
201 !ret && iter.pos < inode->i_size;
202 ret = udf_fiiter_advance(&iter)) {
203 iter.fi.descTag.tagLocation = cpu_to_le32(*block);
204 if (iter.fi.lengthOfImpUse != cpu_to_le16(0))
205 impuse = dbh->b_data + iter.pos +
206 sizeof(struct fileIdentDesc);
207 else
208 impuse = NULL;
209 udf_fiiter_write_fi(&iter, impuse);
210 }
211 brelse(dbh);
212 /*
213 * We don't expect the iteration to fail as the directory has been
214 * already verified to be correct
215 */
216 WARN_ON_ONCE(ret);
217 udf_fiiter_release(&iter);
218
219 return 0;
220 }
221
udf_fiiter_add_entry(struct inode * dir,struct dentry * dentry,struct udf_fileident_iter * iter)222 static int udf_fiiter_add_entry(struct inode *dir, struct dentry *dentry,
223 struct udf_fileident_iter *iter)
224 {
225 struct udf_inode_info *dinfo = UDF_I(dir);
226 int nfidlen, namelen = 0;
227 int ret;
228 int off, blksize = 1 << dir->i_blkbits;
229 udf_pblk_t block;
230 char name[UDF_NAME_LEN_CS0];
231
232 if (dentry) {
233 if (!dentry->d_name.len)
234 return -EINVAL;
235 namelen = udf_put_filename(dir->i_sb, dentry->d_name.name,
236 dentry->d_name.len,
237 name, UDF_NAME_LEN_CS0);
238 if (!namelen)
239 return -ENAMETOOLONG;
240 }
241 nfidlen = ALIGN(sizeof(struct fileIdentDesc) + namelen, UDF_NAME_PAD);
242
243 for (ret = udf_fiiter_init(iter, dir, 0);
244 !ret && iter->pos < dir->i_size;
245 ret = udf_fiiter_advance(iter)) {
246 if (iter->fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
247 if (udf_dir_entry_len(&iter->fi) == nfidlen) {
248 iter->fi.descTag.tagSerialNum = cpu_to_le16(1);
249 iter->fi.fileVersionNum = cpu_to_le16(1);
250 iter->fi.fileCharacteristics = 0;
251 iter->fi.lengthFileIdent = namelen;
252 iter->fi.lengthOfImpUse = cpu_to_le16(0);
253 memcpy(iter->namebuf, name, namelen);
254 iter->name = iter->namebuf;
255 return 0;
256 }
257 }
258 }
259 if (ret) {
260 udf_fiiter_release(iter);
261 return ret;
262 }
263 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
264 blksize - udf_ext0_offset(dir) - iter->pos < nfidlen) {
265 udf_fiiter_release(iter);
266 ret = udf_expand_dir_adinicb(dir, &block);
267 if (ret)
268 return ret;
269 ret = udf_fiiter_init(iter, dir, dir->i_size);
270 if (ret < 0)
271 return ret;
272 }
273
274 /* Get blocknumber to use for entry tag */
275 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
276 block = dinfo->i_location.logicalBlockNum;
277 } else {
278 block = iter->eloc.logicalBlockNum +
279 ((iter->elen - 1) >> dir->i_blkbits);
280 }
281 off = iter->pos & (blksize - 1);
282 if (!off)
283 off = blksize;
284 /* Entry fits into current block? */
285 if (blksize - udf_ext0_offset(dir) - off >= nfidlen)
286 goto store_fi;
287
288 ret = udf_fiiter_append_blk(iter);
289 if (ret) {
290 udf_fiiter_release(iter);
291 return ret;
292 }
293
294 /* Entry will be completely in the new block? Update tag location... */
295 if (!(iter->pos & (blksize - 1)))
296 block = iter->eloc.logicalBlockNum +
297 ((iter->elen - 1) >> dir->i_blkbits);
298 store_fi:
299 memset(&iter->fi, 0, sizeof(struct fileIdentDesc));
300 if (UDF_SB(dir->i_sb)->s_udfrev >= 0x0200)
301 udf_new_tag((char *)(&iter->fi), TAG_IDENT_FID, 3, 1, block,
302 sizeof(struct tag));
303 else
304 udf_new_tag((char *)(&iter->fi), TAG_IDENT_FID, 2, 1, block,
305 sizeof(struct tag));
306 iter->fi.fileVersionNum = cpu_to_le16(1);
307 iter->fi.lengthFileIdent = namelen;
308 iter->fi.lengthOfImpUse = cpu_to_le16(0);
309 memcpy(iter->namebuf, name, namelen);
310 iter->name = iter->namebuf;
311
312 dir->i_size += nfidlen;
313 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
314 dinfo->i_lenAlloc += nfidlen;
315 } else {
316 /* Truncate last extent to proper size */
317 udf_fiiter_update_elen(iter, iter->elen -
318 (dinfo->i_lenExtents - dir->i_size));
319 }
320 mark_inode_dirty(dir);
321
322 return 0;
323 }
324
udf_fiiter_delete_entry(struct udf_fileident_iter * iter)325 static void udf_fiiter_delete_entry(struct udf_fileident_iter *iter)
326 {
327 iter->fi.fileCharacteristics |= FID_FILE_CHAR_DELETED;
328
329 if (UDF_QUERY_FLAG(iter->dir->i_sb, UDF_FLAG_STRICT))
330 memset(&iter->fi.icb, 0x00, sizeof(struct long_ad));
331
332 udf_fiiter_write_fi(iter, NULL);
333 }
334
udf_add_fid_counter(struct super_block * sb,bool dir,int val)335 static void udf_add_fid_counter(struct super_block *sb, bool dir, int val)
336 {
337 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
338
339 if (!lvidiu)
340 return;
341 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
342 if (dir)
343 le32_add_cpu(&lvidiu->numDirs, val);
344 else
345 le32_add_cpu(&lvidiu->numFiles, val);
346 udf_updated_lvid(sb);
347 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
348 }
349
udf_add_nondir(struct dentry * dentry,struct inode * inode)350 static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
351 {
352 struct udf_inode_info *iinfo = UDF_I(inode);
353 struct inode *dir = d_inode(dentry->d_parent);
354 struct udf_fileident_iter iter;
355 int err;
356
357 err = udf_fiiter_add_entry(dir, dentry, &iter);
358 if (err) {
359 inode_dec_link_count(inode);
360 discard_new_inode(inode);
361 return err;
362 }
363 iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
364 iter.fi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
365 *(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
366 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
367 udf_fiiter_write_fi(&iter, NULL);
368 dir->i_mtime = inode_set_ctime_current(dir);
369 mark_inode_dirty(dir);
370 udf_fiiter_release(&iter);
371 udf_add_fid_counter(dir->i_sb, false, 1);
372 d_instantiate_new(dentry, inode);
373
374 return 0;
375 }
376
udf_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)377 static int udf_create(struct mnt_idmap *idmap, struct inode *dir,
378 struct dentry *dentry, umode_t mode, bool excl)
379 {
380 struct inode *inode = udf_new_inode(dir, mode);
381
382 if (IS_ERR(inode))
383 return PTR_ERR(inode);
384
385 inode->i_data.a_ops = &udf_aops;
386 inode->i_op = &udf_file_inode_operations;
387 inode->i_fop = &udf_file_operations;
388 mark_inode_dirty(inode);
389
390 return udf_add_nondir(dentry, inode);
391 }
392
udf_tmpfile(struct mnt_idmap * idmap,struct inode * dir,struct file * file,umode_t mode)393 static int udf_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
394 struct file *file, umode_t mode)
395 {
396 struct inode *inode = udf_new_inode(dir, mode);
397
398 if (IS_ERR(inode))
399 return PTR_ERR(inode);
400
401 inode->i_data.a_ops = &udf_aops;
402 inode->i_op = &udf_file_inode_operations;
403 inode->i_fop = &udf_file_operations;
404 mark_inode_dirty(inode);
405 d_tmpfile(file, inode);
406 unlock_new_inode(inode);
407 return finish_open_simple(file, 0);
408 }
409
udf_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)410 static int udf_mknod(struct mnt_idmap *idmap, struct inode *dir,
411 struct dentry *dentry, umode_t mode, dev_t rdev)
412 {
413 struct inode *inode;
414
415 if (!old_valid_dev(rdev))
416 return -EINVAL;
417
418 inode = udf_new_inode(dir, mode);
419 if (IS_ERR(inode))
420 return PTR_ERR(inode);
421
422 init_special_inode(inode, mode, rdev);
423 return udf_add_nondir(dentry, inode);
424 }
425
udf_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)426 static int udf_mkdir(struct mnt_idmap *idmap, struct inode *dir,
427 struct dentry *dentry, umode_t mode)
428 {
429 struct inode *inode;
430 struct udf_fileident_iter iter;
431 int err;
432 struct udf_inode_info *dinfo = UDF_I(dir);
433 struct udf_inode_info *iinfo;
434
435 inode = udf_new_inode(dir, S_IFDIR | mode);
436 if (IS_ERR(inode))
437 return PTR_ERR(inode);
438
439 iinfo = UDF_I(inode);
440 inode->i_op = &udf_dir_inode_operations;
441 inode->i_fop = &udf_dir_operations;
442 err = udf_fiiter_add_entry(inode, NULL, &iter);
443 if (err) {
444 clear_nlink(inode);
445 discard_new_inode(inode);
446 return err;
447 }
448 set_nlink(inode, 2);
449 iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
450 iter.fi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
451 *(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
452 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
453 iter.fi.fileCharacteristics =
454 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
455 udf_fiiter_write_fi(&iter, NULL);
456 udf_fiiter_release(&iter);
457 mark_inode_dirty(inode);
458
459 err = udf_fiiter_add_entry(dir, dentry, &iter);
460 if (err) {
461 clear_nlink(inode);
462 discard_new_inode(inode);
463 return err;
464 }
465 iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
466 iter.fi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
467 *(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
468 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
469 iter.fi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
470 udf_fiiter_write_fi(&iter, NULL);
471 udf_fiiter_release(&iter);
472 udf_add_fid_counter(dir->i_sb, true, 1);
473 inc_nlink(dir);
474 dir->i_mtime = inode_set_ctime_current(dir);
475 mark_inode_dirty(dir);
476 d_instantiate_new(dentry, inode);
477
478 return 0;
479 }
480
empty_dir(struct inode * dir)481 static int empty_dir(struct inode *dir)
482 {
483 struct udf_fileident_iter iter;
484 int ret;
485
486 for (ret = udf_fiiter_init(&iter, dir, 0);
487 !ret && iter.pos < dir->i_size;
488 ret = udf_fiiter_advance(&iter)) {
489 if (iter.fi.lengthFileIdent &&
490 !(iter.fi.fileCharacteristics & FID_FILE_CHAR_DELETED)) {
491 udf_fiiter_release(&iter);
492 return 0;
493 }
494 }
495 udf_fiiter_release(&iter);
496
497 return 1;
498 }
499
udf_rmdir(struct inode * dir,struct dentry * dentry)500 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
501 {
502 int ret;
503 struct inode *inode = d_inode(dentry);
504 struct udf_fileident_iter iter;
505 struct kernel_lb_addr tloc;
506
507 ret = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
508 if (ret)
509 goto out;
510
511 ret = -EFSCORRUPTED;
512 tloc = lelb_to_cpu(iter.fi.icb.extLocation);
513 if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
514 goto end_rmdir;
515 ret = -ENOTEMPTY;
516 if (!empty_dir(inode))
517 goto end_rmdir;
518 udf_fiiter_delete_entry(&iter);
519 if (inode->i_nlink != 2)
520 udf_warn(inode->i_sb, "empty directory has nlink != 2 (%u)\n",
521 inode->i_nlink);
522 clear_nlink(inode);
523 inode->i_size = 0;
524 if (dir->i_nlink >= 3)
525 inode_dec_link_count(dir);
526 else
527 udf_warn(inode->i_sb, "parent dir link count too low (%u)\n",
528 dir->i_nlink);
529 udf_add_fid_counter(dir->i_sb, true, -1);
530 dir->i_mtime = inode_set_ctime_to_ts(dir,
531 inode_set_ctime_current(inode));
532 mark_inode_dirty(dir);
533 ret = 0;
534 end_rmdir:
535 udf_fiiter_release(&iter);
536 out:
537 return ret;
538 }
539
udf_unlink(struct inode * dir,struct dentry * dentry)540 static int udf_unlink(struct inode *dir, struct dentry *dentry)
541 {
542 int ret;
543 struct inode *inode = d_inode(dentry);
544 struct udf_fileident_iter iter;
545 struct kernel_lb_addr tloc;
546
547 ret = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
548 if (ret)
549 goto out;
550
551 ret = -EFSCORRUPTED;
552 tloc = lelb_to_cpu(iter.fi.icb.extLocation);
553 if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
554 goto end_unlink;
555
556 if (!inode->i_nlink) {
557 udf_debug("Deleting nonexistent file (%lu), %u\n",
558 inode->i_ino, inode->i_nlink);
559 set_nlink(inode, 1);
560 }
561 udf_fiiter_delete_entry(&iter);
562 dir->i_mtime = inode_set_ctime_current(dir);
563 mark_inode_dirty(dir);
564 inode_dec_link_count(inode);
565 udf_add_fid_counter(dir->i_sb, false, -1);
566 inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
567 ret = 0;
568 end_unlink:
569 udf_fiiter_release(&iter);
570 out:
571 return ret;
572 }
573
udf_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)574 static int udf_symlink(struct mnt_idmap *idmap, struct inode *dir,
575 struct dentry *dentry, const char *symname)
576 {
577 struct inode *inode = udf_new_inode(dir, S_IFLNK | 0777);
578 struct pathComponent *pc;
579 const char *compstart;
580 struct extent_position epos = {};
581 int eoffset, elen = 0;
582 uint8_t *ea;
583 int err;
584 udf_pblk_t block;
585 unsigned char *name = NULL;
586 int namelen;
587 struct udf_inode_info *iinfo;
588 struct super_block *sb = dir->i_sb;
589
590 if (IS_ERR(inode))
591 return PTR_ERR(inode);
592
593 iinfo = UDF_I(inode);
594 down_write(&iinfo->i_data_sem);
595 name = kmalloc(UDF_NAME_LEN_CS0, GFP_NOFS);
596 if (!name) {
597 err = -ENOMEM;
598 goto out_no_entry;
599 }
600
601 inode->i_data.a_ops = &udf_symlink_aops;
602 inode->i_op = &udf_symlink_inode_operations;
603 inode_nohighmem(inode);
604
605 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
606 struct kernel_lb_addr eloc;
607 uint32_t bsize;
608
609 block = udf_new_block(sb, inode,
610 iinfo->i_location.partitionReferenceNum,
611 iinfo->i_location.logicalBlockNum, &err);
612 if (!block)
613 goto out_no_entry;
614 epos.block = iinfo->i_location;
615 epos.offset = udf_file_entry_alloc_offset(inode);
616 epos.bh = NULL;
617 eloc.logicalBlockNum = block;
618 eloc.partitionReferenceNum =
619 iinfo->i_location.partitionReferenceNum;
620 bsize = sb->s_blocksize;
621 iinfo->i_lenExtents = bsize;
622 err = udf_add_aext(inode, &epos, &eloc, bsize, 0);
623 brelse(epos.bh);
624 if (err < 0) {
625 udf_free_blocks(sb, inode, &eloc, 0, 1);
626 goto out_no_entry;
627 }
628
629 block = udf_get_pblock(sb, block,
630 iinfo->i_location.partitionReferenceNum,
631 0);
632 epos.bh = sb_getblk(sb, block);
633 if (unlikely(!epos.bh)) {
634 err = -ENOMEM;
635 udf_free_blocks(sb, inode, &eloc, 0, 1);
636 goto out_no_entry;
637 }
638 lock_buffer(epos.bh);
639 memset(epos.bh->b_data, 0x00, bsize);
640 set_buffer_uptodate(epos.bh);
641 unlock_buffer(epos.bh);
642 mark_buffer_dirty_inode(epos.bh, inode);
643 ea = epos.bh->b_data + udf_ext0_offset(inode);
644 } else
645 ea = iinfo->i_data + iinfo->i_lenEAttr;
646
647 eoffset = sb->s_blocksize - udf_ext0_offset(inode);
648 pc = (struct pathComponent *)ea;
649
650 if (*symname == '/') {
651 do {
652 symname++;
653 } while (*symname == '/');
654
655 pc->componentType = 1;
656 pc->lengthComponentIdent = 0;
657 pc->componentFileVersionNum = 0;
658 elen += sizeof(struct pathComponent);
659 }
660
661 err = -ENAMETOOLONG;
662
663 while (*symname) {
664 if (elen + sizeof(struct pathComponent) > eoffset)
665 goto out_no_entry;
666
667 pc = (struct pathComponent *)(ea + elen);
668
669 compstart = symname;
670
671 do {
672 symname++;
673 } while (*symname && *symname != '/');
674
675 pc->componentType = 5;
676 pc->lengthComponentIdent = 0;
677 pc->componentFileVersionNum = 0;
678 if (compstart[0] == '.') {
679 if ((symname - compstart) == 1)
680 pc->componentType = 4;
681 else if ((symname - compstart) == 2 &&
682 compstart[1] == '.')
683 pc->componentType = 3;
684 }
685
686 if (pc->componentType == 5) {
687 namelen = udf_put_filename(sb, compstart,
688 symname - compstart,
689 name, UDF_NAME_LEN_CS0);
690 if (!namelen)
691 goto out_no_entry;
692
693 if (elen + sizeof(struct pathComponent) + namelen >
694 eoffset)
695 goto out_no_entry;
696 else
697 pc->lengthComponentIdent = namelen;
698
699 memcpy(pc->componentIdent, name, namelen);
700 }
701
702 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
703
704 if (*symname) {
705 do {
706 symname++;
707 } while (*symname == '/');
708 }
709 }
710
711 brelse(epos.bh);
712 inode->i_size = elen;
713 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
714 iinfo->i_lenAlloc = inode->i_size;
715 else
716 udf_truncate_tail_extent(inode);
717 mark_inode_dirty(inode);
718 up_write(&iinfo->i_data_sem);
719
720 err = udf_add_nondir(dentry, inode);
721 out:
722 kfree(name);
723 return err;
724
725 out_no_entry:
726 up_write(&iinfo->i_data_sem);
727 inode_dec_link_count(inode);
728 discard_new_inode(inode);
729 goto out;
730 }
731
udf_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)732 static int udf_link(struct dentry *old_dentry, struct inode *dir,
733 struct dentry *dentry)
734 {
735 struct inode *inode = d_inode(old_dentry);
736 struct udf_fileident_iter iter;
737 int err;
738
739 err = udf_fiiter_add_entry(dir, dentry, &iter);
740 if (err)
741 return err;
742 iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
743 iter.fi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
744 if (UDF_SB(inode->i_sb)->s_lvid_bh) {
745 *(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
746 cpu_to_le32(lvid_get_unique_id(inode->i_sb));
747 }
748 udf_fiiter_write_fi(&iter, NULL);
749 udf_fiiter_release(&iter);
750
751 inc_nlink(inode);
752 udf_add_fid_counter(dir->i_sb, false, 1);
753 inode_set_ctime_current(inode);
754 mark_inode_dirty(inode);
755 dir->i_mtime = inode_set_ctime_current(dir);
756 mark_inode_dirty(dir);
757 ihold(inode);
758 d_instantiate(dentry, inode);
759
760 return 0;
761 }
762
763 /* Anybody can rename anything with this: the permission checks are left to the
764 * higher-level routines.
765 */
udf_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)766 static int udf_rename(struct mnt_idmap *idmap, struct inode *old_dir,
767 struct dentry *old_dentry, struct inode *new_dir,
768 struct dentry *new_dentry, unsigned int flags)
769 {
770 struct inode *old_inode = d_inode(old_dentry);
771 struct inode *new_inode = d_inode(new_dentry);
772 struct udf_fileident_iter oiter, niter, diriter;
773 bool has_diriter = false, is_dir = false;
774 int retval;
775 struct kernel_lb_addr tloc;
776
777 if (flags & ~RENAME_NOREPLACE)
778 return -EINVAL;
779
780 retval = udf_fiiter_find_entry(old_dir, &old_dentry->d_name, &oiter);
781 if (retval)
782 return retval;
783
784 tloc = lelb_to_cpu(oiter.fi.icb.extLocation);
785 if (udf_get_lb_pblock(old_dir->i_sb, &tloc, 0) != old_inode->i_ino) {
786 retval = -ENOENT;
787 goto out_oiter;
788 }
789
790 if (S_ISDIR(old_inode->i_mode)) {
791 if (new_inode) {
792 retval = -ENOTEMPTY;
793 if (!empty_dir(new_inode))
794 goto out_oiter;
795 retval = -EFSCORRUPTED;
796 if (new_inode->i_nlink != 2)
797 goto out_oiter;
798 }
799 retval = -EFSCORRUPTED;
800 if (old_dir->i_nlink < 3)
801 goto out_oiter;
802 is_dir = true;
803 } else if (new_inode) {
804 retval = -EFSCORRUPTED;
805 if (new_inode->i_nlink < 1)
806 goto out_oiter;
807 }
808 if (is_dir && old_dir != new_dir) {
809 retval = udf_fiiter_find_entry(old_inode, &dotdot_name,
810 &diriter);
811 if (retval == -ENOENT) {
812 udf_err(old_inode->i_sb,
813 "directory (ino %lu) has no '..' entry\n",
814 old_inode->i_ino);
815 retval = -EFSCORRUPTED;
816 }
817 if (retval)
818 goto out_oiter;
819 has_diriter = true;
820 tloc = lelb_to_cpu(diriter.fi.icb.extLocation);
821 if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
822 old_dir->i_ino) {
823 retval = -EFSCORRUPTED;
824 udf_err(old_inode->i_sb,
825 "directory (ino %lu) has parent entry pointing to another inode (%lu != %u)\n",
826 old_inode->i_ino, old_dir->i_ino,
827 udf_get_lb_pblock(old_inode->i_sb, &tloc, 0));
828 goto out_oiter;
829 }
830 }
831
832 retval = udf_fiiter_find_entry(new_dir, &new_dentry->d_name, &niter);
833 if (retval && retval != -ENOENT)
834 goto out_oiter;
835 /* Entry found but not passed by VFS? */
836 if (!retval && !new_inode) {
837 retval = -EFSCORRUPTED;
838 udf_fiiter_release(&niter);
839 goto out_oiter;
840 }
841 /* Entry not found? Need to add one... */
842 if (retval) {
843 udf_fiiter_release(&niter);
844 retval = udf_fiiter_add_entry(new_dir, new_dentry, &niter);
845 if (retval)
846 goto out_oiter;
847 }
848
849 /*
850 * Like most other Unix systems, set the ctime for inodes on a
851 * rename.
852 */
853 inode_set_ctime_current(old_inode);
854 mark_inode_dirty(old_inode);
855
856 /*
857 * ok, that's it
858 */
859 niter.fi.fileVersionNum = oiter.fi.fileVersionNum;
860 niter.fi.fileCharacteristics = oiter.fi.fileCharacteristics;
861 memcpy(&(niter.fi.icb), &(oiter.fi.icb), sizeof(oiter.fi.icb));
862 udf_fiiter_write_fi(&niter, NULL);
863 udf_fiiter_release(&niter);
864
865 /*
866 * The old entry may have moved due to new entry allocation. Find it
867 * again.
868 */
869 udf_fiiter_release(&oiter);
870 retval = udf_fiiter_find_entry(old_dir, &old_dentry->d_name, &oiter);
871 if (retval) {
872 udf_err(old_dir->i_sb,
873 "failed to find renamed entry again in directory (ino %lu)\n",
874 old_dir->i_ino);
875 } else {
876 udf_fiiter_delete_entry(&oiter);
877 udf_fiiter_release(&oiter);
878 }
879
880 if (new_inode) {
881 inode_set_ctime_current(new_inode);
882 inode_dec_link_count(new_inode);
883 udf_add_fid_counter(old_dir->i_sb, S_ISDIR(new_inode->i_mode),
884 -1);
885 }
886 old_dir->i_mtime = inode_set_ctime_current(old_dir);
887 new_dir->i_mtime = inode_set_ctime_current(new_dir);
888 mark_inode_dirty(old_dir);
889 mark_inode_dirty(new_dir);
890
891 if (has_diriter) {
892 diriter.fi.icb.extLocation =
893 cpu_to_lelb(UDF_I(new_dir)->i_location);
894 udf_fiiter_write_fi(&diriter, NULL);
895 udf_fiiter_release(&diriter);
896 }
897
898 if (is_dir) {
899 inode_dec_link_count(old_dir);
900 if (new_inode)
901 inode_dec_link_count(new_inode);
902 else {
903 inc_nlink(new_dir);
904 mark_inode_dirty(new_dir);
905 }
906 }
907 return 0;
908 out_oiter:
909 if (has_diriter)
910 udf_fiiter_release(&diriter);
911 udf_fiiter_release(&oiter);
912
913 return retval;
914 }
915
udf_get_parent(struct dentry * child)916 static struct dentry *udf_get_parent(struct dentry *child)
917 {
918 struct kernel_lb_addr tloc;
919 struct inode *inode = NULL;
920 struct udf_fileident_iter iter;
921 int err;
922
923 err = udf_fiiter_find_entry(d_inode(child), &dotdot_name, &iter);
924 if (err)
925 return ERR_PTR(err);
926
927 tloc = lelb_to_cpu(iter.fi.icb.extLocation);
928 udf_fiiter_release(&iter);
929 inode = udf_iget(child->d_sb, &tloc);
930 if (IS_ERR(inode))
931 return ERR_CAST(inode);
932
933 return d_obtain_alias(inode);
934 }
935
936
udf_nfs_get_inode(struct super_block * sb,u32 block,u16 partref,__u32 generation)937 static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
938 u16 partref, __u32 generation)
939 {
940 struct inode *inode;
941 struct kernel_lb_addr loc;
942
943 if (block == 0)
944 return ERR_PTR(-ESTALE);
945
946 loc.logicalBlockNum = block;
947 loc.partitionReferenceNum = partref;
948 inode = udf_iget(sb, &loc);
949
950 if (IS_ERR(inode))
951 return ERR_CAST(inode);
952
953 if (generation && inode->i_generation != generation) {
954 iput(inode);
955 return ERR_PTR(-ESTALE);
956 }
957 return d_obtain_alias(inode);
958 }
959
udf_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)960 static struct dentry *udf_fh_to_dentry(struct super_block *sb,
961 struct fid *fid, int fh_len, int fh_type)
962 {
963 if (fh_len < 3 ||
964 (fh_type != FILEID_UDF_WITH_PARENT &&
965 fh_type != FILEID_UDF_WITHOUT_PARENT))
966 return NULL;
967
968 return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
969 fid->udf.generation);
970 }
971
udf_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)972 static struct dentry *udf_fh_to_parent(struct super_block *sb,
973 struct fid *fid, int fh_len, int fh_type)
974 {
975 if (fh_len < 5 || fh_type != FILEID_UDF_WITH_PARENT)
976 return NULL;
977
978 return udf_nfs_get_inode(sb, fid->udf.parent_block,
979 fid->udf.parent_partref,
980 fid->udf.parent_generation);
981 }
udf_encode_fh(struct inode * inode,__u32 * fh,int * lenp,struct inode * parent)982 static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
983 struct inode *parent)
984 {
985 int len = *lenp;
986 struct kernel_lb_addr location = UDF_I(inode)->i_location;
987 struct fid *fid = (struct fid *)fh;
988 int type = FILEID_UDF_WITHOUT_PARENT;
989
990 if (parent && (len < 5)) {
991 *lenp = 5;
992 return FILEID_INVALID;
993 } else if (len < 3) {
994 *lenp = 3;
995 return FILEID_INVALID;
996 }
997
998 *lenp = 3;
999 fid->udf.block = location.logicalBlockNum;
1000 fid->udf.partref = location.partitionReferenceNum;
1001 fid->udf.parent_partref = 0;
1002 fid->udf.generation = inode->i_generation;
1003
1004 if (parent) {
1005 location = UDF_I(parent)->i_location;
1006 fid->udf.parent_block = location.logicalBlockNum;
1007 fid->udf.parent_partref = location.partitionReferenceNum;
1008 fid->udf.parent_generation = inode->i_generation;
1009 *lenp = 5;
1010 type = FILEID_UDF_WITH_PARENT;
1011 }
1012
1013 return type;
1014 }
1015
1016 const struct export_operations udf_export_ops = {
1017 .encode_fh = udf_encode_fh,
1018 .fh_to_dentry = udf_fh_to_dentry,
1019 .fh_to_parent = udf_fh_to_parent,
1020 .get_parent = udf_get_parent,
1021 };
1022
1023 const struct inode_operations udf_dir_inode_operations = {
1024 .lookup = udf_lookup,
1025 .create = udf_create,
1026 .link = udf_link,
1027 .unlink = udf_unlink,
1028 .symlink = udf_symlink,
1029 .mkdir = udf_mkdir,
1030 .rmdir = udf_rmdir,
1031 .mknod = udf_mknod,
1032 .rename = udf_rename,
1033 .tmpfile = udf_tmpfile,
1034 };
1035