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