| 1 | |
| 2 | |
| 3 | * dir.c
|
| 4 | *
|
| 5 | * PURPOSE
|
| 6 | * Directory handling routines for the OSTA-UDF(tm) filesystem.
|
| 7 | *
|
| 8 | * COPYRIGHT
|
| 9 | * (C) 1998-2004 Ben Fennema
|
| 10 | *
|
| 11 | * HISTORY
|
| 12 | *
|
| 13 | * 10/05/98 dgb Split directory operations into its own file
|
| 14 | * Implemented directory reads via do_udf_readdir
|
| 15 | * 10/06/98 Made directory operations work!
|
| 16 | * 11/17/98 Rewrote directory to support ICBTAG_FLAG_AD_LONG
|
| 17 | * 11/25/98 blf Rewrote directory handling (readdir+lookup) to support reading
|
| 18 | * across blocks.
|
| 19 | * 12/12/98 Split out the lookup code to namei.c. bulk of directory
|
| 20 | * code now in directory.c:udf_fileident_read.
|
| 21 |
|
| 22 |
|
| 23 | #include "udfdecl.h"
|
| 24 |
|
| 25 | #include <linux/string.h>
|
| 26 | #include <linux/errno.h>
|
| 27 | #include <linux/filelock.h>
|
| 28 | #include <linux/mm.h>
|
| 29 | #include <linux/slab.h>
|
| 30 | #include <linux/bio.h>
|
| 31 | #include <linux/iversion.h>
|
| 32 |
|
| 33 | #include "udf_i.h"
|
| 34 | #include "udf_sb.h"
|
| 35 |
|
| 36 | static int udf_readdir(struct file *file, struct dir_context *ctx)
|
| 37 | {
|
| 38 | struct inode *dir = file_inode(file);
|
| 39 | loff_t nf_pos, emit_pos = 0;
|
| 40 | int flen;
|
| 41 | unsigned char *fname = NULL;
|
| 42 | int ret = 0;
|
| 43 | struct super_block *sb = dir->i_sb;
|
| 44 | bool pos_valid = false;
|
| 45 | struct udf_fileident_iter iter;
|
| 46 |
|
| 47 | if (ctx->pos == 0) {
|
| 48 | if (!dir_emit_dot(file, ctx))
|
| 49 | return 0;
|
| 50 | ctx->pos = 1;
|
| 51 | }
|
| 52 | nf_pos = (ctx->pos - 1) << 2;
|
| 53 | if (nf_pos >= dir->i_size)
|
| 54 | goto out;
|
| 55 |
|
| 56 | |
| 57 | * Something changed since last readdir (either lseek was called or dir
|
| 58 | * changed)? We need to verify the position correctly points at the
|
| 59 | * beginning of some dir entry so that the directory parsing code does
|
| 60 | * not get confused. Since UDF does not have any reliable way of
|
| 61 | * identifying beginning of dir entry (names are under user control),
|
| 62 | * we need to scan the directory from the beginning.
|
| 63 |
|
| 64 | if (!inode_eq_iversion(dir, *(u64 *)file->private_data)) {
|
| 65 | emit_pos = nf_pos;
|
| 66 | nf_pos = 0;
|
| 67 | } else {
|
| 68 | pos_valid = true;
|
| 69 | }
|
| 70 |
|
| 71 | fname = kmalloc(UDF_NAME_LEN, GFP_KERNEL);
|
| 72 | if (!fname) {
|
| 73 | ret = -ENOMEM;
|
| 74 | goto out;
|
| 75 | }
|
| 76 |
|
| 77 | for (ret = udf_fiiter_init(&iter, dir, nf_pos);
|
| 78 | !ret && iter.pos < dir->i_size;
|
| 79 | ret = udf_fiiter_advance(&iter)) {
|
| 80 | struct kernel_lb_addr tloc;
|
| 81 | udf_pblk_t iblock;
|
| 82 |
|
| 83 |
|
| 84 | if (iter.pos < emit_pos)
|
| 85 | continue;
|
| 86 |
|
| 87 |
|
| 88 | pos_valid = true;
|
| 89 | ctx->pos = (iter.pos >> 2) + 1;
|
| 90 |
|
| 91 | if (iter.fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
|
| 92 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
|
| 93 | continue;
|
| 94 | }
|
| 95 |
|
| 96 | if (iter.fi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) {
|
| 97 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
|
| 98 | continue;
|
| 99 | }
|
| 100 |
|
| 101 | if (iter.fi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
|
| 102 | if (!dir_emit_dotdot(file, ctx))
|
| 103 | goto out_iter;
|
| 104 | continue;
|
| 105 | }
|
| 106 |
|
| 107 | flen = udf_get_filename(sb, iter.name,
|
| 108 | iter.fi.lengthFileIdent, fname, UDF_NAME_LEN);
|
| 109 | if (flen < 0)
|
| 110 | continue;
|
| 111 |
|
| 112 | tloc = lelb_to_cpu(iter.fi.icb.extLocation);
|
| 113 | iblock = udf_get_lb_pblock(sb, &tloc, 0);
|
| 114 | if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
|
| 115 | goto out_iter;
|
| 116 | }
|
| 117 |
|
| 118 | if (!ret) {
|
| 119 | ctx->pos = (iter.pos >> 2) + 1;
|
| 120 | pos_valid = true;
|
| 121 | }
|
| 122 | out_iter:
|
| 123 | udf_fiiter_release(&iter);
|
| 124 | out:
|
| 125 | if (pos_valid)
|
| 126 | *(u64 *)file->private_data = inode_query_iversion(dir);
|
| 127 | kfree(fname);
|
| 128 |
|
| 129 | return ret;
|
| 130 | }
|
| 131 |
|
| 132 | static int udf_dir_open(struct inode *inode, struct file *file)
|
| 133 | {
|
| 134 | file->private_data = kzalloc(sizeof(u64), GFP_KERNEL);
|
| 135 | if (!file->private_data)
|
| 136 | return -ENOMEM;
|
| 137 | return 0;
|
| 138 | }
|
| 139 |
|
| 140 | static int udf_dir_release(struct inode *inode, struct file *file)
|
| 141 | {
|
| 142 | kfree(file->private_data);
|
| 143 | return 0;
|
| 144 | }
|
| 145 |
|
| 146 | static loff_t udf_dir_llseek(struct file *file, loff_t offset, int whence)
|
| 147 | {
|
| 148 | return generic_llseek_cookie(file, offset, whence,
|
| 149 | (u64 *)file->private_data);
|
| 150 | }
|
| 151 |
|
| 152 |
|
| 153 | const struct file_operations udf_dir_operations = {
|
| 154 | .open = udf_dir_open,
|
| 155 | .release = udf_dir_release,
|
| 156 | .llseek = udf_dir_llseek,
|
| 157 | .read = generic_read_dir,
|
| 158 | .iterate_shared = udf_readdir,
|
| 159 | .unlocked_ioctl = udf_ioctl,
|
| 160 | .fsync = simple_fsync,
|
| 161 | .setlease = generic_setlease,
|
| 162 | };
|