blob: a126950d79e647632ea364c63ceae8f6f6c761ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * namei.c
3 *
4 * PURPOSE
5 * Inode name handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 Dobriyane8edc6e2007-05-21 01:22:52 +040033#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070035static inline int udf_match(int len1, const char *name1, int len2,
36 const char *name2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 if (len1 != len2)
39 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return !memcmp(name1, name2, len1);
42}
43
44int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070045 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
Marcin Slusarz4b111112008-02-08 04:20:36 -080046 uint8_t *impuse, uint8_t *fileident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
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 Gorcunov28de7942007-07-21 04:37:18 -070056 sizeof(struct fileIdentDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 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 Gorcunovcb00ea32007-07-19 01:47:43 -070064 if (impuse) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070065 if (adinicb || (offset + liu < 0)) {
66 memcpy((uint8_t *)sfi->impUse, impuse, liu);
67 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 memcpy(fibh->ebh->b_data + offset, impuse, liu);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070069 } else {
70 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
Marcin Slusarz4b111112008-02-08 04:20:36 -080071 memcpy(fibh->ebh->b_data, impuse - offset,
72 liu + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74 }
75
76 offset += liu;
77
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070078 if (fileident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070079 if (adinicb || (offset + lfi < 0)) {
80 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
81 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070083 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -080084 memcpy((uint8_t *)sfi->fileIdent + liu, fileident,
85 -offset);
86 memcpy(fibh->ebh->b_data, fileident - offset,
87 lfi + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89 }
90
91 offset += lfi;
92
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070093 if (adinicb || (offset + padlen < 0)) {
94 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
95 } else if (offset >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 memset(fibh->ebh->b_data + offset, 0x00, padlen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070097 } else {
98 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 memset(fibh->ebh->b_data, 0x00, padlen + offset);
100 }
101
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700102 crc = udf_crc((uint8_t *)cfi + sizeof(tag),
103 sizeof(struct fileIdentDesc) - sizeof(tag), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700105 if (fibh->sbh == fibh->ebh) {
106 crc = udf_crc((uint8_t *)sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800107 crclen + sizeof(tag) -
108 sizeof(struct fileIdentDesc), crc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700109 } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800110 crc = udf_crc(fibh->ebh->b_data +
111 sizeof(struct fileIdentDesc) +
112 fibh->soffset,
113 crclen + sizeof(tag) -
114 sizeof(struct fileIdentDesc),
115 crc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700116 } else {
117 crc = udf_crc((uint8_t *)sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800118 -fibh->soffset - sizeof(struct fileIdentDesc),
119 crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 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 Gorcunov28de7942007-07-21 04:37:18 -0700126 for (i = 0; i < 16; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (i != 4)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700128 checksum += ((uint8_t *)&cfi->descTag)[i];
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 cfi->descTag.tagChecksum = checksum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700132 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800133 memcpy((uint8_t *)sfi, (uint8_t *)cfi,
134 sizeof(struct fileIdentDesc));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700135 } else {
136 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
137 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700138 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700141 if (adinicb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700143 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (fibh->sbh != fibh->ebh)
145 mark_buffer_dirty_inode(fibh->ebh, inode);
146 mark_buffer_dirty_inode(fibh->sbh, inode);
147 }
148 return 0;
149}
150
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700151static struct fileIdentDesc *udf_find_entry(struct inode *dir,
152 struct dentry *dentry,
153 struct udf_fileident_bh *fibh,
154 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700156 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 loff_t f_pos;
158 int block, flen;
159 char fname[UDF_NAME_LEN];
160 char *nameptr;
161 uint8_t lfi;
162 uint16_t liu;
KAMBAROV, ZAURec471dc2005-06-28 20:45:10 -0700163 loff_t size;
Jan Karaff116fc2007-05-08 00:35:14 -0700164 kernel_lb_addr eloc;
165 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700166 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700167 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
KAMBAROV, ZAURec471dc2005-06-28 20:45:10 -0700169 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 f_pos = (udf_ext0_offset(dir) >> 2);
171
Marcin Slusarz4b111112008-02-08 04:20:36 -0800172 fibh->soffset = fibh->eoffset =
173 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
174 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 fibh->sbh = fibh->ebh = NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800176 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
177 &epos, &eloc, &elen, &offset) ==
178 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700180 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700182 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700184 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800185 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 offset = 0;
187
Marcin Slusarz4b111112008-02-08 04:20:36 -0800188 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
189 if (!fibh->sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700190 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return NULL;
192 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700193 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700194 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return NULL;
196 }
197
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700198 while ((f_pos < size)) {
199 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
200 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700201 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700203 brelse(fibh->ebh);
204 brelse(fibh->sbh);
205 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return NULL;
207 }
208
209 liu = le16_to_cpu(cfi->lengthOfImpUse);
210 lfi = cfi->lengthFileIdent;
211
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700212 if (fibh->sbh == fibh->ebh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 nameptr = fi->fileIdent + liu;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700214 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int poffset; /* Unpaded ending offset */
216
Marcin Slusarz4b111112008-02-08 04:20:36 -0800217 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
218 liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Marcin Slusarz4b111112008-02-08 04:20:36 -0800220 if (poffset >= lfi)
221 nameptr = (uint8_t *)(fibh->ebh->b_data +
222 poffset - lfi);
223 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 nameptr = fname;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800225 memcpy(nameptr, fi->fileIdent + liu,
226 lfi - poffset);
227 memcpy(nameptr + lfi - poffset,
228 fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 }
231
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
233 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 continue;
235 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700236
237 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
238 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 continue;
240 }
241
242 if (!lfi)
243 continue;
244
Marcin Slusarz4b111112008-02-08 04:20:36 -0800245 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
246 if (flen && udf_match(flen, fname, dentry->d_name.len,
247 dentry->d_name.name)) {
248 brelse(epos.bh);
249 return fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700254 brelse(fibh->ebh);
255 brelse(fibh->sbh);
256 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return NULL;
259}
260
261/*
262 * udf_lookup
263 *
264 * PURPOSE
265 * Look-up the inode for a given name.
266 *
267 * DESCRIPTION
268 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
269 * available for a directory. The filesystem is useless if this routine is
270 * not available for at least the filesystem's root directory.
271 *
272 * This routine is passed an incomplete dentry - it must be completed by
273 * calling d_add(dentry, inode). If the name does not exist, then the
274 * specified inode must be set to null. An error should only be returned
275 * when the lookup fails for a reason other than the name not existing.
276 * Note that the directory inode semaphore is held during the call.
277 *
278 * Refer to lookup_dentry() in fs/namei.c
279 * lookup_dentry() -> lookup() -> real_lookup() -> .
280 *
281 * PRE-CONDITIONS
282 * dir Pointer to inode of parent directory.
283 * dentry Pointer to dentry to complete.
284 * nd Pointer to lookup nameidata
285 *
286 * POST-CONDITIONS
287 * <return> Zero on success.
288 *
289 * HISTORY
290 * July 1, 1997 - Andrew E. Mileski
291 * Written, tested, and released.
292 */
293
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
295 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct inode *inode = NULL;
Jayachandran Cdb9a3692006-02-03 03:04:50 -0800298 struct fileIdentDesc cfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct udf_fileident_bh fibh;
300
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700301 if (dentry->d_name.len > UDF_NAME_LEN - 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return ERR_PTR(-ENAMETOOLONG);
303
304 lock_kernel();
305#ifdef UDF_RECOVERY
306 /* temporary shorthand for specifying files by inode number */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700307 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700308 kernel_lb_addr lb = {
309 .logicalBlockNum = 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800310 .partitionReferenceNum =
311 simple_strtoul(dentry->d_name.name + 3,
312 NULL, 0),
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700313 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 inode = udf_iget(dir->i_sb, lb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700315 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unlock_kernel();
317 return ERR_PTR(-EACCES);
318 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800319 } else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700320#endif /* UDF_RECOVERY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700322 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700324 brelse(fibh.ebh);
325 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unlock_kernel();
330 return ERR_PTR(-EACCES);
331 }
332 }
333 unlock_kernel();
334 d_add(dentry, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return NULL;
337}
338
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339static struct fileIdentDesc *udf_add_entry(struct inode *dir,
340 struct dentry *dentry,
341 struct udf_fileident_bh *fibh,
342 struct fileIdentDesc *cfi, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800344 struct super_block *sb = dir->i_sb;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700345 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
347 int namelen;
348 loff_t f_pos;
349 int flen;
350 char *nameptr;
351 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
352 int nfidlen;
353 uint8_t lfi;
354 uint16_t liu;
355 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700356 kernel_lb_addr eloc;
357 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700358 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700359 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700361 if (dentry) {
362 if (!dentry->d_name.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *err = -EINVAL;
364 return NULL;
365 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800366 namelen = udf_put_filename(sb, dentry->d_name.name, name,
367 dentry->d_name.len);
368 if (!namelen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *err = -ENAMETOOLONG;
370 return NULL;
371 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700372 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 namelen = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
377
378 f_pos = (udf_ext0_offset(dir) >> 2);
379
Marcin Slusarz4b111112008-02-08 04:20:36 -0800380 fibh->soffset = fibh->eoffset =
381 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
382 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 fibh->sbh = fibh->ebh = NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800384 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
385 &epos, &eloc, &elen, &offset) ==
386 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700388 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700390 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700392 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800393 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 offset = 0;
395
Marcin Slusarz4b111112008-02-08 04:20:36 -0800396 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
397 if (!fibh->sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700398 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 *err = -EIO;
400 return NULL;
401 }
402
403 block = UDF_I_LOCATION(dir).logicalBlockNum;
404
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700405 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
407 fibh->sbh = fibh->ebh = NULL;
408 fibh->soffset = fibh->eoffset = sb->s_blocksize;
409 goto add;
410 }
411
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700412 while ((f_pos < size)) {
413 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
414 &elen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700416 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700418 brelse(fibh->ebh);
419 brelse(fibh->sbh);
420 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *err = -EIO;
422 return NULL;
423 }
424
425 liu = le16_to_cpu(cfi->lengthOfImpUse);
426 lfi = cfi->lengthFileIdent;
427
Marcin Slusarz4b111112008-02-08 04:20:36 -0800428 if (fibh->sbh == fibh->ebh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 nameptr = fi->fileIdent + liu;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800430 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int poffset; /* Unpaded ending offset */
432
Marcin Slusarz4b111112008-02-08 04:20:36 -0800433 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
434 liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Marcin Slusarz4b111112008-02-08 04:20:36 -0800436 if (poffset >= lfi)
437 nameptr = (char *)(fibh->ebh->b_data +
438 poffset - lfi);
439 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 nameptr = fname;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800441 memcpy(nameptr, fi->fileIdent + liu,
442 lfi - poffset);
443 memcpy(nameptr + lfi - poffset,
444 fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 }
447
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700448 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800449 if (((sizeof(struct fileIdentDesc) +
450 liu + lfi + 3) & ~3) == nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700451 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 cfi->descTag.tagSerialNum = cpu_to_le16(1);
453 cfi->fileVersionNum = cpu_to_le16(1);
454 cfi->fileCharacteristics = 0;
455 cfi->lengthFileIdent = namelen;
456 cfi->lengthOfImpUse = cpu_to_le16(0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800457 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
458 name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return fi;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800460 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *err = -EIO;
462 return NULL;
463 }
464 }
465 }
466
467 if (!lfi || !dentry)
468 continue;
469
Marcin Slusarz4b111112008-02-08 04:20:36 -0800470 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
471 if (flen && udf_match(flen, fname, dentry->d_name.len,
472 dentry->d_name.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700474 brelse(fibh->ebh);
475 brelse(fibh->sbh);
476 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 *err = -EEXIST;
478 return NULL;
479 }
480 }
481
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700482add:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 f_pos += nfidlen;
484
485 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700486 sb->s_blocksize - fibh->eoffset < nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700487 brelse(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700488 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 fibh->soffset -= udf_ext0_offset(dir);
490 fibh->eoffset -= udf_ext0_offset(dir);
491 f_pos -= (udf_ext0_offset(dir) >> 2);
492 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700493 brelse(fibh->ebh);
494 brelse(fibh->sbh);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800495 fibh->sbh = fibh->ebh =
496 udf_expand_dir_adinicb(dir, &block, err);
497 if (!fibh->sbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return NULL;
Jan Karaff116fc2007-05-08 00:35:14 -0700499 epos.block = UDF_I_LOCATION(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 eloc.logicalBlockNum = block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800501 eloc.partitionReferenceNum =
502 UDF_I_LOCATION(dir).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 elen = dir->i_sb->s_blocksize;
Jan Karaff116fc2007-05-08 00:35:14 -0700504 epos.offset = udf_file_entry_alloc_offset(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700506 epos.offset += sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700508 epos.offset += sizeof(long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700511 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 fibh->soffset = fibh->eoffset;
513 fibh->eoffset += nfidlen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700514 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700515 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 fibh->sbh = fibh->ebh;
517 }
518
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700519 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 block = UDF_I_LOCATION(dir).logicalBlockNum;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800521 fi = (struct fileIdentDesc *)
522 (UDF_I_DATA(dir) + fibh->soffset -
523 udf_ext0_offset(dir) +
524 UDF_I_LENEATTR(dir));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700525 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800526 block = eloc.logicalBlockNum +
527 ((elen - 1) >>
528 dir->i_sb->s_blocksize_bits);
529 fi = (struct fileIdentDesc *)
530 (fibh->sbh->b_data + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700532 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 fibh->soffset = fibh->eoffset - sb->s_blocksize;
534 fibh->eoffset += nfidlen - sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700535 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700536 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 fibh->sbh = fibh->ebh;
538 }
539
540 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700541 dir->i_sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800542 fibh->ebh = udf_bread(dir,
543 f_pos >> (dir->i_sb->s_blocksize_bits - 2),
544 1, err);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700545 if (!fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700546 brelse(epos.bh);
547 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return NULL;
549 }
550
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700551 if (!fibh->soffset) {
Jan Karaff116fc2007-05-08 00:35:14 -0700552 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700555 dir->i_sb->s_blocksize_bits);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800556 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700557 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Jan Kara3bf25cb2007-05-08 00:35:16 -0700559 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 fibh->sbh = fibh->ebh;
561 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700562 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 fi = (struct fileIdentDesc *)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800564 (fibh->sbh->b_data + sb->s_blocksize +
565 fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 }
568
569 memset(cfi, 0, sizeof(struct fileIdentDesc));
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800570 if (UDF_SB(sb)->s_udfrev >= 0x0200)
Marcin Slusarz4b111112008-02-08 04:20:36 -0800571 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
572 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 else
Marcin Slusarz4b111112008-02-08 04:20:36 -0800574 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
575 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 cfi->fileVersionNum = cpu_to_le16(1);
577 cfi->lengthFileIdent = namelen;
578 cfi->lengthOfImpUse = cpu_to_le16(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700579 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700580 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 dir->i_size += nfidlen;
582 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
583 UDF_I_LENALLOC(dir) += nfidlen;
584 mark_inode_dirty(dir);
585 return fi;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700586 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700587 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700589 brelse(fibh->ebh);
590 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *err = -EIO;
592 return NULL;
593 }
594}
595
596static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700597 struct udf_fileident_bh *fibh,
598 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
603 memset(&(cfi->icb), 0x00, sizeof(long_ad));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
606}
607
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700608static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
609 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 struct udf_fileident_bh fibh;
612 struct inode *inode;
613 struct fileIdentDesc cfi, *fi;
614 int err;
615
616 lock_kernel();
617 inode = udf_new_inode(dir, mode, &err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700618 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 unlock_kernel();
620 return err;
621 }
622
623 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
624 inode->i_data.a_ops = &udf_adinicb_aops;
625 else
626 inode->i_data.a_ops = &udf_aops;
627 inode->i_op = &udf_file_inode_operations;
628 inode->i_fop = &udf_file_operations;
629 inode->i_mode = mode;
630 mark_inode_dirty(inode);
631
Marcin Slusarz4b111112008-02-08 04:20:36 -0800632 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
633 if (!fi) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700634 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 mark_inode_dirty(inode);
636 iput(inode);
637 unlock_kernel();
638 return err;
639 }
640 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
641 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700642 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
643 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800645 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700648 brelse(fibh.ebh);
649 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 unlock_kernel();
651 d_instantiate(dentry, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654}
655
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700656static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
657 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700659 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct udf_fileident_bh fibh;
661 struct fileIdentDesc cfi, *fi;
662 int err;
663
664 if (!old_valid_dev(rdev))
665 return -EINVAL;
666
667 lock_kernel();
668 err = -EIO;
669 inode = udf_new_inode(dir, mode, &err);
670 if (!inode)
671 goto out;
672
673 inode->i_uid = current->fsuid;
674 init_special_inode(inode, mode, rdev);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800675 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
676 if (!fi) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700677 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 mark_inode_dirty(inode);
679 iput(inode);
680 unlock_kernel();
681 return err;
682 }
683 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
684 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700685 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
686 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800688 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 mark_inode_dirty(inode);
691
692 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700693 brelse(fibh.ebh);
694 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 d_instantiate(dentry, inode);
696 err = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700697
698out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 unlock_kernel();
700 return err;
701}
702
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700703static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700705 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct udf_fileident_bh fibh;
707 struct fileIdentDesc cfi, *fi;
708 int err;
709
710 lock_kernel();
711 err = -EMLINK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto out;
714
715 err = -EIO;
716 inode = udf_new_inode(dir, S_IFDIR, &err);
717 if (!inode)
718 goto out;
719
720 inode->i_op = &udf_dir_inode_operations;
721 inode->i_fop = &udf_dir_operations;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800722 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
723 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 inode->i_nlink--;
725 mark_inode_dirty(inode);
726 iput(inode);
727 goto out;
728 }
729 inode->i_nlink = 2;
730 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
731 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700732 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
733 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800734 cfi.fileCharacteristics =
735 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700737 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 inode->i_mode = S_IFDIR | mode;
739 if (dir->i_mode & S_ISGID)
740 inode->i_mode |= S_ISGID;
741 mark_inode_dirty(inode);
742
Marcin Slusarz4b111112008-02-08 04:20:36 -0800743 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
744 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 inode->i_nlink = 0;
746 mark_inode_dirty(inode);
747 iput(inode);
748 goto out;
749 }
750 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
751 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700752 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
753 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
755 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Dave Hansend8c76e62006-09-30 23:29:04 -0700756 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 mark_inode_dirty(dir);
758 d_instantiate(dentry, inode);
759 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700760 brelse(fibh.ebh);
761 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 err = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700763
764out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 unlock_kernel();
766 return err;
767}
768
769static int empty_dir(struct inode *dir)
770{
771 struct fileIdentDesc *fi, cfi;
772 struct udf_fileident_bh fibh;
773 loff_t f_pos;
774 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
775 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700776 kernel_lb_addr eloc;
777 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700778 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700779 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 f_pos = (udf_ext0_offset(dir) >> 2);
782
Marcin Slusarz4b111112008-02-08 04:20:36 -0800783 fibh.soffset = fibh.eoffset =
784 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Marcin Slusarz4b111112008-02-08 04:20:36 -0800786 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 fibh.sbh = fibh.ebh = NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800788 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
789 &epos, &eloc, &elen, &offset) ==
790 (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700794 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700796 epos.offset -= sizeof(long_ad);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800797 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 offset = 0;
799
Marcin Slusarz4b111112008-02-08 04:20:36 -0800800 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
801 if (!fibh.sbh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700802 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700805 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700806 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808 }
809
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700810 while ((f_pos < size)) {
811 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
812 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700813 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700815 brelse(fibh.ebh);
816 brelse(fibh.sbh);
817 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return 0;
819 }
820
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700821 if (cfi.lengthFileIdent &&
822 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700824 brelse(fibh.ebh);
825 brelse(fibh.sbh);
826 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
828 }
829 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700832 brelse(fibh.ebh);
833 brelse(fibh.sbh);
834 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 1;
837}
838
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700839static int udf_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700842 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct udf_fileident_bh fibh;
844 struct fileIdentDesc *fi, cfi;
845 kernel_lb_addr tloc;
846
847 retval = -ENOENT;
848 lock_kernel();
849 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
850 if (!fi)
851 goto out;
852
853 retval = -EIO;
854 tloc = lelb_to_cpu(cfi.icb.extLocation);
855 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
856 goto end_rmdir;
857 retval = -ENOTEMPTY;
858 if (!empty_dir(inode))
859 goto end_rmdir;
860 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
861 if (retval)
862 goto end_rmdir;
863 if (inode->i_nlink != 2)
864 udf_warning(inode->i_sb, "udf_rmdir",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700865 "empty directory has nlink != 2 (%d)",
866 inode->i_nlink);
Dave Hansence71ec32006-09-30 23:29:06 -0700867 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 inode->i_size = 0;
Stephen Mollettc007c062007-05-08 00:31:31 -0700869 inode_dec_link_count(dir);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800870 inode->i_ctime = dir->i_ctime = dir->i_mtime =
871 current_fs_time(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 mark_inode_dirty(dir);
873
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700874end_rmdir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700876 brelse(fibh.ebh);
877 brelse(fibh.sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700878
879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unlock_kernel();
881 return retval;
882}
883
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700884static int udf_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700887 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct udf_fileident_bh fibh;
889 struct fileIdentDesc *fi;
890 struct fileIdentDesc cfi;
891 kernel_lb_addr tloc;
892
893 retval = -ENOENT;
894 lock_kernel();
895 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
896 if (!fi)
897 goto out;
898
899 retval = -EIO;
900 tloc = lelb_to_cpu(cfi.icb.extLocation);
901 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
902 goto end_unlink;
903
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700904 if (!inode->i_nlink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 udf_debug("Deleting nonexistent file (%lu), %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700906 inode->i_ino, inode->i_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 inode->i_nlink = 1;
908 }
909 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
910 if (retval)
911 goto end_unlink;
912 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
913 mark_inode_dirty(dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700914 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 inode->i_ctime = dir->i_ctime;
916 retval = 0;
917
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700918end_unlink:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700920 brelse(fibh.ebh);
921 brelse(fibh.sbh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700922
923out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 unlock_kernel();
925 return retval;
926}
927
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700928static int udf_symlink(struct inode *dir, struct dentry *dentry,
929 const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700931 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct pathComponent *pc;
933 char *compstart;
934 struct udf_fileident_bh fibh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700935 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int eoffset, elen = 0;
937 struct fileIdentDesc *fi;
938 struct fileIdentDesc cfi;
939 char *ea;
940 int err;
941 int block;
942 char name[UDF_NAME_LEN];
943 int namelen;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800944 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 lock_kernel();
Marcin Slusarz4b111112008-02-08 04:20:36 -0800947 inode = udf_new_inode(dir, S_IFLNK, &err);
948 if (!inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto out;
950
951 inode->i_mode = S_IFLNK | S_IRWXUGO;
952 inode->i_data.a_ops = &udf_symlink_aops;
953 inode->i_op = &page_symlink_inode_operations;
954
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700955 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB) {
Jan Karaff116fc2007-05-08 00:35:14 -0700956 kernel_lb_addr eloc;
957 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800960 UDF_I_LOCATION(inode).partitionReferenceNum,
961 UDF_I_LOCATION(inode).logicalBlockNum, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!block)
963 goto out_no_entry;
Jan Karaff116fc2007-05-08 00:35:14 -0700964 epos.block = UDF_I_LOCATION(inode);
965 epos.offset = udf_file_entry_alloc_offset(inode);
966 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 eloc.logicalBlockNum = block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800968 eloc.partitionReferenceNum =
969 UDF_I_LOCATION(inode).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 elen = inode->i_sb->s_blocksize;
971 UDF_I_LENEXTENTS(inode) = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700972 udf_add_aext(inode, &epos, eloc, elen, 0);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700973 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 block = udf_get_pblock(inode->i_sb, block,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800976 UDF_I_LOCATION(inode).partitionReferenceNum,
977 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700978 epos.bh = udf_tread(inode->i_sb, block);
979 lock_buffer(epos.bh);
980 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
981 set_buffer_uptodate(epos.bh);
982 unlock_buffer(epos.bh);
983 mark_buffer_dirty_inode(epos.bh, inode);
984 ea = epos.bh->b_data + udf_ext0_offset(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700985 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
990 pc = (struct pathComponent *)ea;
991
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700992 if (*symname == '/') {
993 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 symname++;
995 } while (*symname == '/');
996
997 pc->componentType = 1;
998 pc->lengthComponentIdent = 0;
999 pc->componentFileVersionNum = 0;
1000 pc += sizeof(struct pathComponent);
1001 elen += sizeof(struct pathComponent);
1002 }
1003
1004 err = -ENAMETOOLONG;
1005
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001006 while (*symname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (elen + sizeof(struct pathComponent) > eoffset)
1008 goto out_no_entry;
1009
1010 pc = (struct pathComponent *)(ea + elen);
1011
1012 compstart = (char *)symname;
1013
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001014 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 symname++;
1016 } while (*symname && *symname != '/');
1017
1018 pc->componentType = 5;
1019 pc->lengthComponentIdent = 0;
1020 pc->componentFileVersionNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001021 if (compstart[0] == '.') {
1022 if ((symname - compstart) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 pc->componentType = 4;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001024 else if ((symname - compstart) == 2 &&
1025 compstart[1] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 pc->componentType = 3;
1027 }
1028
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001029 if (pc->componentType == 5) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001030 namelen = udf_put_filename(inode->i_sb, compstart, name,
1031 symname - compstart);
1032 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 goto out_no_entry;
1034
Marcin Slusarz4b111112008-02-08 04:20:36 -08001035 if (elen + sizeof(struct pathComponent) + namelen >
1036 eoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto out_no_entry;
1038 else
1039 pc->lengthComponentIdent = namelen;
1040
1041 memcpy(pc->componentIdent, name, namelen);
1042 }
1043
1044 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1045
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001046 if (*symname) {
1047 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 symname++;
1049 } while (*symname == '/');
1050 }
1051 }
1052
Jan Kara3bf25cb2007-05-08 00:35:16 -07001053 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 inode->i_size = elen;
1055 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1056 UDF_I_LENALLOC(inode) = inode->i_size;
1057 mark_inode_dirty(inode);
1058
Marcin Slusarz4b111112008-02-08 04:20:36 -08001059 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1060 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto out_no_entry;
1062 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1063 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001064 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1065 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001066 struct logicalVolIntegrityDesc *lvid =
1067 (struct logicalVolIntegrityDesc *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct logicalVolHeaderDesc *lvhd;
1069 uint64_t uniqueID;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001070 lvhd = (struct logicalVolHeaderDesc *)
1071 lvid->logicalVolContentsUse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001073 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1074 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1076 uniqueID += 16;
1077 lvhd->uniqueID = cpu_to_le64(uniqueID);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001078 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001081 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001084 brelse(fibh.ebh);
1085 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 d_instantiate(dentry, inode);
1087 err = 0;
1088
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001089out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 unlock_kernel();
1091 return err;
1092
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001093out_no_entry:
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001094 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 iput(inode);
1096 goto out;
1097}
1098
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001099static int udf_link(struct dentry *old_dentry, struct inode *dir,
1100 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct inode *inode = old_dentry->d_inode;
1103 struct udf_fileident_bh fibh;
1104 struct fileIdentDesc cfi, *fi;
1105 int err;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001106 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001109 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 unlock_kernel();
1111 return -EMLINK;
1112 }
1113
Marcin Slusarz4b111112008-02-08 04:20:36 -08001114 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1115 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 unlock_kernel();
1117 return err;
1118 }
1119 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1120 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001121 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1122 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001123 struct logicalVolIntegrityDesc *lvid =
1124 (struct logicalVolIntegrityDesc *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct logicalVolHeaderDesc *lvhd;
1126 uint64_t uniqueID;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001127 lvhd = (struct logicalVolHeaderDesc *)
1128 (lvid->logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001130 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1131 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1133 uniqueID += 16;
1134 lvhd->uniqueID = cpu_to_le64(uniqueID);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001135 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001138 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 mark_inode_dirty(dir);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001142 brelse(fibh.ebh);
1143 brelse(fibh.sbh);
Dave Hansend8c76e62006-09-30 23:29:04 -07001144 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 inode->i_ctime = current_fs_time(inode->i_sb);
1146 mark_inode_dirty(inode);
1147 atomic_inc(&inode->i_count);
1148 d_instantiate(dentry, inode);
1149 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152}
1153
1154/* Anybody can rename anything with this: the permission checks are left to the
1155 * higher-level routines.
1156 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001157static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1158 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001160 struct inode *old_inode = old_dentry->d_inode;
1161 struct inode *new_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct udf_fileident_bh ofibh, nfibh;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001163 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1164 struct fileIdentDesc ocfi, ncfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct buffer_head *dir_bh = NULL;
1166 int retval = -ENOENT;
1167 kernel_lb_addr tloc;
1168
1169 lock_kernel();
Marcin Slusarz4b111112008-02-08 04:20:36 -08001170 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1171 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001173 brelse(ofibh.ebh);
1174 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1177 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001178 != old_inode->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto end_rename;
1180
1181 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001182 if (nfi) {
1183 if (!new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001185 brelse(nfibh.ebh);
1186 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 nfi = NULL;
1188 }
1189 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001190 if (S_ISDIR(old_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 uint32_t offset = udf_ext0_offset(old_inode);
1192
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001193 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 retval = -ENOTEMPTY;
1195 if (!empty_dir(new_inode))
1196 goto end_rename;
1197 }
1198 retval = -EIO;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001199 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001200 dir_fi = udf_get_fileident(
1201 UDF_I_DATA(old_inode) -
1202 (UDF_I_EFE(old_inode) ?
1203 sizeof(struct extendedFileEntry) :
1204 sizeof(struct fileEntry)),
1205 old_inode->i_sb->s_blocksize, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001206 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1208 if (!dir_bh)
1209 goto end_rename;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001210 dir_fi = udf_get_fileident(dir_bh->b_data,
1211 old_inode->i_sb->s_blocksize, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213 if (!dir_fi)
1214 goto end_rename;
1215 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001216 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1217 old_dir->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto end_rename;
1219
1220 retval = -EMLINK;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001221 if (!new_inode &&
1222 new_dir->i_nlink >=
1223 (256 << sizeof(new_dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto end_rename;
1225 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001226 if (!nfi) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001227 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1228 &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!nfi)
1230 goto end_rename;
1231 }
1232
1233 /*
1234 * Like most other Unix systems, set the ctime for inodes on a
1235 * rename.
1236 */
1237 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1238 mark_inode_dirty(old_inode);
1239
1240 /*
1241 * ok, that's it
1242 */
1243 ncfi.fileVersionNum = ocfi.fileVersionNum;
1244 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1245 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1246 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1247
1248 /* The old fid may have moved - find it again */
1249 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1250 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1251
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001252 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001254 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1257 mark_inode_dirty(old_dir);
1258
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001259 if (dir_fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
Marcin Slusarz4b111112008-02-08 04:20:36 -08001261 udf_update_tag((char *)dir_fi,
1262 (sizeof(struct fileIdentDesc) +
1263 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1264 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 mark_inode_dirty(old_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001266 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 mark_buffer_dirty_inode(dir_bh, old_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001268
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001269 inode_dec_link_count(old_dir);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001270 if (new_inode)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001271 inode_dec_link_count(new_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001272 else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001273 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 mark_inode_dirty(new_dir);
1275 }
1276 }
1277
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001278 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001280 brelse(ofibh.ebh);
1281 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
1284 retval = 0;
1285
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001286end_rename:
Jan Kara3bf25cb2007-05-08 00:35:16 -07001287 brelse(dir_bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001288 if (nfi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001290 brelse(nfibh.ebh);
1291 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return retval;
1296}
1297
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001298const struct inode_operations udf_dir_inode_operations = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001299 .lookup = udf_lookup,
1300 .create = udf_create,
1301 .link = udf_link,
1302 .unlink = udf_unlink,
1303 .symlink = udf_symlink,
1304 .mkdir = udf_mkdir,
1305 .rmdir = udf_rmdir,
1306 .mknod = udf_mknod,
1307 .rename = udf_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308};