Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * dir.c
0003  *
0004  * PURPOSE
0005  *  Directory handling routines for the OSTA-UDF(tm) filesystem.
0006  *
0007  * COPYRIGHT
0008  *  This file is distributed under the terms of the GNU General Public
0009  *  License (GPL). Copies of the GPL can be obtained from:
0010  *      ftp://prep.ai.mit.edu/pub/gnu/GPL
0011  *  Each contributing author retains all rights to their own work.
0012  *
0013  *  (C) 1998-2004 Ben Fennema
0014  *
0015  * HISTORY
0016  *
0017  *  10/05/98 dgb  Split directory operations into its own file
0018  *                Implemented directory reads via do_udf_readdir
0019  *  10/06/98      Made directory operations work!
0020  *  11/17/98      Rewrote directory to support ICBTAG_FLAG_AD_LONG
0021  *  11/25/98 blf  Rewrote directory handling (readdir+lookup) to support reading
0022  *                across blocks.
0023  *  12/12/98      Split out the lookup code to namei.c. bulk of directory
0024  *                code now in directory.c:udf_fileident_read.
0025  */
0026 
0027 #include "udfdecl.h"
0028 
0029 #include <linux/string.h>
0030 #include <linux/errno.h>
0031 #include <linux/mm.h>
0032 #include <linux/slab.h>
0033 #include <linux/bio.h>
0034 #include <linux/iversion.h>
0035 
0036 #include "udf_i.h"
0037 #include "udf_sb.h"
0038 
0039 static int udf_readdir(struct file *file, struct dir_context *ctx)
0040 {
0041     struct inode *dir = file_inode(file);
0042     struct udf_inode_info *iinfo = UDF_I(dir);
0043     struct udf_fileident_bh fibh = { .sbh = NULL, .ebh = NULL};
0044     struct fileIdentDesc *fi = NULL;
0045     struct fileIdentDesc cfi;
0046     udf_pblk_t block, iblock;
0047     loff_t nf_pos, emit_pos = 0;
0048     int flen;
0049     unsigned char *fname = NULL, *copy_name = NULL;
0050     unsigned char *nameptr;
0051     uint16_t liu;
0052     uint8_t lfi;
0053     loff_t size = udf_ext0_offset(dir) + dir->i_size;
0054     struct buffer_head *tmp, *bha[16];
0055     struct kernel_lb_addr eloc;
0056     uint32_t elen;
0057     sector_t offset;
0058     int i, num, ret = 0;
0059     struct extent_position epos = { NULL, 0, {0, 0} };
0060     struct super_block *sb = dir->i_sb;
0061     bool pos_valid = false;
0062 
0063     if (ctx->pos == 0) {
0064         if (!dir_emit_dot(file, ctx))
0065             return 0;
0066         ctx->pos = 1;
0067     }
0068     nf_pos = (ctx->pos - 1) << 2;
0069     if (nf_pos >= size)
0070         goto out;
0071 
0072     /*
0073      * Something changed since last readdir (either lseek was called or dir
0074      * changed)?  We need to verify the position correctly points at the
0075      * beginning of some dir entry so that the directory parsing code does
0076      * not get confused. Since UDF does not have any reliable way of
0077      * identifying beginning of dir entry (names are under user control),
0078      * we need to scan the directory from the beginning.
0079      */
0080     if (!inode_eq_iversion(dir, file->f_version)) {
0081         emit_pos = nf_pos;
0082         nf_pos = 0;
0083     } else {
0084         pos_valid = true;
0085     }
0086 
0087     fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
0088     if (!fname) {
0089         ret = -ENOMEM;
0090         goto out;
0091     }
0092 
0093     if (nf_pos == 0)
0094         nf_pos = udf_ext0_offset(dir);
0095 
0096     fibh.soffset = fibh.eoffset = nf_pos & (sb->s_blocksize - 1);
0097     if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
0098         if (inode_bmap(dir, nf_pos >> sb->s_blocksize_bits,
0099             &epos, &eloc, &elen, &offset)
0100             != (EXT_RECORDED_ALLOCATED >> 30)) {
0101             ret = -ENOENT;
0102             goto out;
0103         }
0104         block = udf_get_lb_pblock(sb, &eloc, offset);
0105         if ((++offset << sb->s_blocksize_bits) < elen) {
0106             if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
0107                 epos.offset -= sizeof(struct short_ad);
0108             else if (iinfo->i_alloc_type ==
0109                     ICBTAG_FLAG_AD_LONG)
0110                 epos.offset -= sizeof(struct long_ad);
0111         } else {
0112             offset = 0;
0113         }
0114 
0115         if (!(fibh.sbh = fibh.ebh = udf_tread(sb, block))) {
0116             ret = -EIO;
0117             goto out;
0118         }
0119 
0120         if (!(offset & ((16 >> (sb->s_blocksize_bits - 9)) - 1))) {
0121             i = 16 >> (sb->s_blocksize_bits - 9);
0122             if (i + offset > (elen >> sb->s_blocksize_bits))
0123                 i = (elen >> sb->s_blocksize_bits) - offset;
0124             for (num = 0; i > 0; i--) {
0125                 block = udf_get_lb_pblock(sb, &eloc, offset + i);
0126                 tmp = udf_tgetblk(sb, block);
0127                 if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
0128                     bha[num++] = tmp;
0129                 else
0130                     brelse(tmp);
0131             }
0132             if (num) {
0133                 ll_rw_block(REQ_OP_READ | REQ_RAHEAD, num, bha);
0134                 for (i = 0; i < num; i++)
0135                     brelse(bha[i]);
0136             }
0137         }
0138     }
0139 
0140     while (nf_pos < size) {
0141         struct kernel_lb_addr tloc;
0142         loff_t cur_pos = nf_pos;
0143 
0144         /* Update file position only if we got past the current one */
0145         if (nf_pos >= emit_pos) {
0146             ctx->pos = (nf_pos >> 2) + 1;
0147             pos_valid = true;
0148         }
0149 
0150         fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
0151                     &elen, &offset);
0152         if (!fi)
0153             goto out;
0154         /* Still not at offset where user asked us to read from? */
0155         if (cur_pos < emit_pos)
0156             continue;
0157 
0158         liu = le16_to_cpu(cfi.lengthOfImpUse);
0159         lfi = cfi.lengthFileIdent;
0160 
0161         if (fibh.sbh == fibh.ebh) {
0162             nameptr = udf_get_fi_ident(fi);
0163         } else {
0164             int poffset;    /* Unpaded ending offset */
0165 
0166             poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
0167 
0168             if (poffset >= lfi) {
0169                 nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
0170             } else {
0171                 if (!copy_name) {
0172                     copy_name = kmalloc(UDF_NAME_LEN,
0173                                 GFP_NOFS);
0174                     if (!copy_name) {
0175                         ret = -ENOMEM;
0176                         goto out;
0177                     }
0178                 }
0179                 nameptr = copy_name;
0180                 memcpy(nameptr, udf_get_fi_ident(fi),
0181                        lfi - poffset);
0182                 memcpy(nameptr + lfi - poffset,
0183                        fibh.ebh->b_data, poffset);
0184             }
0185         }
0186 
0187         if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
0188             if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
0189                 continue;
0190         }
0191 
0192         if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
0193             if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
0194                 continue;
0195         }
0196 
0197         if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
0198             if (!dir_emit_dotdot(file, ctx))
0199                 goto out;
0200             continue;
0201         }
0202 
0203         flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
0204         if (flen < 0)
0205             continue;
0206 
0207         tloc = lelb_to_cpu(cfi.icb.extLocation);
0208         iblock = udf_get_lb_pblock(sb, &tloc, 0);
0209         if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
0210             goto out;
0211     } /* end while */
0212 
0213     ctx->pos = (nf_pos >> 2) + 1;
0214     pos_valid = true;
0215 
0216 out:
0217     if (pos_valid)
0218         file->f_version = inode_query_iversion(dir);
0219     if (fibh.sbh != fibh.ebh)
0220         brelse(fibh.ebh);
0221     brelse(fibh.sbh);
0222     brelse(epos.bh);
0223     kfree(fname);
0224     kfree(copy_name);
0225 
0226     return ret;
0227 }
0228 
0229 /* readdir and lookup functions */
0230 const struct file_operations udf_dir_operations = {
0231     .llseek         = generic_file_llseek,
0232     .read           = generic_read_dir,
0233     .iterate_shared     = udf_readdir,
0234     .unlocked_ioctl     = udf_ioctl,
0235     .fsync          = generic_file_fsync,
0236 };