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