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