blob: 334d363a0903c764cf79daa479a27fe894d86569 [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;
40 return !memcmp(name1, name2, len1);
41}
42
43int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070044 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
45 uint8_t * impuse, uint8_t * fileident)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
48 uint16_t crc;
49 uint8_t checksum = 0;
50 int i;
51 int offset;
52 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
53 uint8_t lfi = cfi->lengthFileIdent;
54 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070055 sizeof(struct fileIdentDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int adinicb = 0;
57
58 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
59 adinicb = 1;
60
61 offset = fibh->soffset + sizeof(struct fileIdentDesc);
62
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063 if (impuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (adinicb || (offset + liu < 0))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070065 memcpy((uint8_t *) sfi->impUse, impuse, liu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 else if (offset >= 0)
67 memcpy(fibh->ebh->b_data + offset, impuse, liu);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070068 else {
69 memcpy((uint8_t *) sfi->impUse, impuse, -offset);
70 memcpy(fibh->ebh->b_data, impuse - offset,
71 liu + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73 }
74
75 offset += liu;
76
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070077 if (fileident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (adinicb || (offset + lfi < 0))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070079 memcpy((uint8_t *) sfi->fileIdent + liu, fileident,
80 lfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 else if (offset >= 0)
82 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070083 else {
84 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
93 if (adinicb || (offset + padlen < 0))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070094 memset((uint8_t *) sfi->padding + liu + lfi, 0x00, padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 else if (offset >= 0)
96 memset(fibh->ebh->b_data + offset, 0x00, padlen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -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 Gorcunovcb00ea32007-07-19 01:47:43 -0700102 crc =
103 udf_crc((uint8_t *) cfi + sizeof(tag),
104 sizeof(struct fileIdentDesc) - sizeof(tag), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (fibh->sbh == fibh->ebh)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107 crc = udf_crc((uint8_t *) sfi->impUse,
108 crclen + sizeof(tag) -
109 sizeof(struct fileIdentDesc), crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 else if (sizeof(struct fileIdentDesc) >= -fibh->soffset)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700111 crc =
112 udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) +
113 fibh->soffset,
114 crclen + sizeof(tag) - sizeof(struct fileIdentDesc),
115 crc);
116 else {
117 crc = udf_crc((uint8_t *) sfi->impUse,
118 -fibh->soffset - sizeof(struct fileIdentDesc),
119 crc);
Linus 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 Gorcunovcb00ea32007-07-19 01:47:43 -0700126 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (i != 4)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128 checksum += ((uint8_t *) & cfi->descTag)[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 cfi->descTag.tagChecksum = checksum;
131 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700132 memcpy((uint8_t *) sfi, (uint8_t *) cfi,
133 sizeof(struct fileIdentDesc));
134 else {
135 memcpy((uint8_t *) sfi, (uint8_t *) cfi, -fibh->soffset);
136 memcpy(fibh->ebh->b_data, (uint8_t *) cfi - fibh->soffset,
137 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
140 if (adinicb)
141 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700142 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (fibh->sbh != fibh->ebh)
144 mark_buffer_dirty_inode(fibh->ebh, inode);
145 mark_buffer_dirty_inode(fibh->sbh, inode);
146 }
147 return 0;
148}
149
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150static struct fileIdentDesc *udf_find_entry(struct inode *dir,
151 struct dentry *dentry,
152 struct udf_fileident_bh *fibh,
153 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 loff_t f_pos;
157 int block, flen;
158 char fname[UDF_NAME_LEN];
159 char *nameptr;
160 uint8_t lfi;
161 uint16_t liu;
KAMBAROV, ZAURec471dc2005-06-28 20:45:10 -0700162 loff_t size;
Jan Karaff116fc2007-05-08 00:35:14 -0700163 kernel_lb_addr eloc;
164 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700165 sector_t offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700166 struct extent_position epos = { NULL, 0, {0, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
KAMBAROV, ZAURec471dc2005-06-28 20:45:10 -0700168 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 f_pos = (udf_ext0_offset(dir) >> 2);
170
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700171 fibh->soffset = fibh->eoffset =
172 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
174 fibh->sbh = fibh->ebh = NULL;
175 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700176 &epos, &eloc, &elen,
177 &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700179 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700181 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700183 epos.offset -= sizeof(long_ad);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700184 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 offset = 0;
186
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700187 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700188 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return NULL;
190 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700191 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700192 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return NULL;
194 }
195
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700196 while ((f_pos < size)) {
197 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
198 &elen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700200 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700202 brelse(fibh->ebh);
203 brelse(fibh->sbh);
204 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return NULL;
206 }
207
208 liu = le16_to_cpu(cfi->lengthOfImpUse);
209 lfi = cfi->lengthFileIdent;
210
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700211 if (fibh->sbh == fibh->ebh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 nameptr = fi->fileIdent + liu;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700213 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int poffset; /* Unpaded ending offset */
215
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700216 poffset =
217 fibh->soffset + sizeof(struct fileIdentDesc) + liu +
218 lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (poffset >= lfi)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700221 nameptr =
222 (uint8_t *) (fibh->ebh->b_data + poffset -
223 lfi);
224 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 nameptr = fname;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700226 memcpy(nameptr, fi->fileIdent + liu,
227 lfi - poffset);
228 memcpy(nameptr + lfi - poffset,
229 fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 }
232
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700233 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
234 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 continue;
236 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700237
238 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
239 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 continue;
241 }
242
243 if (!lfi)
244 continue;
245
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700246 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi))) {
247 if (udf_match
248 (flen, fname, dentry->d_name.len,
249 dentry->d_name.name)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700250 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return fi;
252 }
253 }
254 }
255 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700256 brelse(fibh->ebh);
257 brelse(fibh->sbh);
258 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return NULL;
260}
261
262/*
263 * udf_lookup
264 *
265 * PURPOSE
266 * Look-up the inode for a given name.
267 *
268 * DESCRIPTION
269 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
270 * available for a directory. The filesystem is useless if this routine is
271 * not available for at least the filesystem's root directory.
272 *
273 * This routine is passed an incomplete dentry - it must be completed by
274 * calling d_add(dentry, inode). If the name does not exist, then the
275 * specified inode must be set to null. An error should only be returned
276 * when the lookup fails for a reason other than the name not existing.
277 * Note that the directory inode semaphore is held during the call.
278 *
279 * Refer to lookup_dentry() in fs/namei.c
280 * lookup_dentry() -> lookup() -> real_lookup() -> .
281 *
282 * PRE-CONDITIONS
283 * dir Pointer to inode of parent directory.
284 * dentry Pointer to dentry to complete.
285 * nd Pointer to lookup nameidata
286 *
287 * POST-CONDITIONS
288 * <return> Zero on success.
289 *
290 * HISTORY
291 * July 1, 1997 - Andrew E. Mileski
292 * Written, tested, and released.
293 */
294
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700295static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
296 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct inode *inode = NULL;
Jayachandran Cdb9a3692006-02-03 03:04:50 -0800299 struct fileIdentDesc cfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct udf_fileident_bh fibh;
301
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700302 if (dentry->d_name.len > UDF_NAME_LEN - 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return ERR_PTR(-ENAMETOOLONG);
304
305 lock_kernel();
306#ifdef UDF_RECOVERY
307 /* temporary shorthand for specifying files by inode number */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700308 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
309 kernel_lb_addr lb =
310 { 0, simple_strtoul(dentry->d_name.name + 3, NULL, 0) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 inode = udf_iget(dir->i_sb, lb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700312 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unlock_kernel();
314 return ERR_PTR(-EACCES);
315 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700316 } else
317#endif /* UDF_RECOVERY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700319 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700321 brelse(fibh.ebh);
322 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700325 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unlock_kernel();
327 return ERR_PTR(-EACCES);
328 }
329 }
330 unlock_kernel();
331 d_add(dentry, inode);
332 return NULL;
333}
334
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700335static struct fileIdentDesc *udf_add_entry(struct inode *dir,
336 struct dentry *dentry,
337 struct udf_fileident_bh *fibh,
338 struct fileIdentDesc *cfi, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct super_block *sb;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700341 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
343 int namelen;
344 loff_t f_pos;
345 int flen;
346 char *nameptr;
347 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
348 int nfidlen;
349 uint8_t lfi;
350 uint16_t liu;
351 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700352 kernel_lb_addr eloc;
353 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700354 sector_t offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700355 struct extent_position epos = { NULL, 0, {0, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 sb = dir->i_sb;
358
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700359 if (dentry) {
360 if (!dentry->d_name.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *err = -EINVAL;
362 return NULL;
363 }
364
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700365 if (!
366 (namelen =
367 udf_put_filename(sb, dentry->d_name.name, name,
368 dentry->d_name.len))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *err = -ENAMETOOLONG;
370 return NULL;
371 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700372 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 namelen = 0;
374
375 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
376
377 f_pos = (udf_ext0_offset(dir) >> 2);
378
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700379 fibh->soffset = fibh->eoffset =
380 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
382 fibh->sbh = fibh->ebh = NULL;
383 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700384 &epos, &eloc, &elen,
385 &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700387 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700389 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700391 epos.offset -= sizeof(long_ad);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700392 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 offset = 0;
394
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700395 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700396 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *err = -EIO;
398 return NULL;
399 }
400
401 block = UDF_I_LOCATION(dir).logicalBlockNum;
402
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700403 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
405 fibh->sbh = fibh->ebh = NULL;
406 fibh->soffset = fibh->eoffset = sb->s_blocksize;
407 goto add;
408 }
409
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700410 while ((f_pos < size)) {
411 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
412 &elen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700414 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700416 brelse(fibh->ebh);
417 brelse(fibh->sbh);
418 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 *err = -EIO;
420 return NULL;
421 }
422
423 liu = le16_to_cpu(cfi->lengthOfImpUse);
424 lfi = cfi->lengthFileIdent;
425
426 if (fibh->sbh == fibh->ebh)
427 nameptr = fi->fileIdent + liu;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700428 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int poffset; /* Unpaded ending offset */
430
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700431 poffset =
432 fibh->soffset + sizeof(struct fileIdentDesc) + liu +
433 lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (poffset >= lfi)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700436 nameptr =
437 (char *)(fibh->ebh->b_data + poffset - lfi);
438 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 nameptr = fname;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700440 memcpy(nameptr, fi->fileIdent + liu,
441 lfi - poffset);
442 memcpy(nameptr + lfi - poffset,
443 fibh->ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 }
446
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700447 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
448 if (((sizeof(struct fileIdentDesc) + liu + lfi +
449 3) & ~3) == nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700450 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 cfi->descTag.tagSerialNum = cpu_to_le16(1);
452 cfi->fileVersionNum = cpu_to_le16(1);
453 cfi->fileCharacteristics = 0;
454 cfi->lengthFileIdent = namelen;
455 cfi->lengthOfImpUse = cpu_to_le16(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700456 if (!udf_write_fi
457 (dir, cfi, fi, fibh, NULL, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return fi;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700459 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *err = -EIO;
461 return NULL;
462 }
463 }
464 }
465
466 if (!lfi || !dentry)
467 continue;
468
469 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700470 udf_match(flen, fname, dentry->d_name.len,
471 dentry->d_name.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700473 brelse(fibh->ebh);
474 brelse(fibh->sbh);
475 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *err = -EEXIST;
477 return NULL;
478 }
479 }
480
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700481 add:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 f_pos += nfidlen;
483
484 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700485 sb->s_blocksize - fibh->eoffset < nfidlen) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700486 brelse(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700487 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 fibh->soffset -= udf_ext0_offset(dir);
489 fibh->eoffset -= udf_ext0_offset(dir);
490 f_pos -= (udf_ext0_offset(dir) >> 2);
491 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700492 brelse(fibh->ebh);
493 brelse(fibh->sbh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700494 if (!
495 (fibh->sbh = fibh->ebh =
496 udf_expand_dir_adinicb(dir, &block, err)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return NULL;
Jan Karaff116fc2007-05-08 00:35:14 -0700498 epos.block = UDF_I_LOCATION(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 eloc.logicalBlockNum = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700500 eloc.partitionReferenceNum =
501 UDF_I_LOCATION(dir).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 elen = dir->i_sb->s_blocksize;
Jan Karaff116fc2007-05-08 00:35:14 -0700503 epos.offset = udf_file_entry_alloc_offset(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700505 epos.offset += sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700507 epos.offset += sizeof(long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700510 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 fibh->soffset = fibh->eoffset;
512 fibh->eoffset += nfidlen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700513 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700514 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 fibh->sbh = fibh->ebh;
516 }
517
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700518 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 block = UDF_I_LOCATION(dir).logicalBlockNum;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700520 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) +
521 fibh->soffset -
522 udf_ext0_offset(dir) +
523 UDF_I_LENEATTR(dir));
524 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700526 dir->i_sb->
527 s_blocksize_bits);
528 fi = (struct fileIdentDesc *)(fibh->sbh->b_data +
529 fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 fibh->soffset = fibh->eoffset - sb->s_blocksize;
533 fibh->eoffset += nfidlen - sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700534 if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700535 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 fibh->sbh = fibh->ebh;
537 }
538
539 block = eloc.logicalBlockNum + ((elen - 1) >>
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700540 dir->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700542 if (!
543 (fibh->ebh =
544 udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
545 1, err))) {
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 Gorcunovcb00ea32007-07-19 01:47:43 -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 Gorcunovcb00ea32007-07-19 01:47:43 -0700555 dir->i_sb->
556 s_blocksize_bits);
557 } else
558 block++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jan Kara3bf25cb2007-05-08 00:35:16 -0700560 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 fibh->sbh = fibh->ebh;
562 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700563 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 fi = (struct fileIdentDesc *)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700565 (fibh->sbh->b_data + sb->s_blocksize +
566 fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 }
569
570 memset(cfi, 0, sizeof(struct fileIdentDesc));
571 if (UDF_SB_UDFREV(sb) >= 0x0200)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700572 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
573 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700575 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
576 sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 cfi->fileVersionNum = cpu_to_le16(1);
578 cfi->lengthFileIdent = namelen;
579 cfi->lengthOfImpUse = cpu_to_le16(0);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700580 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700581 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 dir->i_size += nfidlen;
583 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
584 UDF_I_LENALLOC(dir) += nfidlen;
585 mark_inode_dirty(dir);
586 return fi;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700587 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700588 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (fibh->sbh != fibh->ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700590 brelse(fibh->ebh);
591 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *err = -EIO;
593 return NULL;
594 }
595}
596
597static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700598 struct udf_fileident_bh *fibh,
599 struct fileIdentDesc *cfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
602 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
603 memset(&(cfi->icb), 0x00, sizeof(long_ad));
604 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
605}
606
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700607static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
608 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct udf_fileident_bh fibh;
611 struct inode *inode;
612 struct fileIdentDesc cfi, *fi;
613 int err;
614
615 lock_kernel();
616 inode = udf_new_inode(dir, mode, &err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700617 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unlock_kernel();
619 return err;
620 }
621
622 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
623 inode->i_data.a_ops = &udf_adinicb_aops;
624 else
625 inode->i_data.a_ops = &udf_aops;
626 inode->i_op = &udf_file_inode_operations;
627 inode->i_fop = &udf_file_operations;
628 inode->i_mode = mode;
629 mark_inode_dirty(inode);
630
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700631 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
632 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 mark_inode_dirty(inode);
634 iput(inode);
635 unlock_kernel();
636 return err;
637 }
638 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
639 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700640 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
641 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700643 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 mark_inode_dirty(dir);
645 }
646 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700647 brelse(fibh.ebh);
648 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 unlock_kernel();
650 d_instantiate(dentry, inode);
651 return 0;
652}
653
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700654static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
655 dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700657 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct udf_fileident_bh fibh;
659 struct fileIdentDesc cfi, *fi;
660 int err;
661
662 if (!old_valid_dev(rdev))
663 return -EINVAL;
664
665 lock_kernel();
666 err = -EIO;
667 inode = udf_new_inode(dir, mode, &err);
668 if (!inode)
669 goto out;
670
671 inode->i_uid = current->fsuid;
672 init_special_inode(inode, mode, rdev);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700673 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
674 inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 mark_inode_dirty(inode);
676 iput(inode);
677 unlock_kernel();
678 return err;
679 }
680 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
681 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700682 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
683 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700685 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 mark_inode_dirty(dir);
687 }
688 mark_inode_dirty(inode);
689
690 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700691 brelse(fibh.ebh);
692 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 d_instantiate(dentry, inode);
694 err = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700695 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 unlock_kernel();
697 return err;
698}
699
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700700static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700702 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct udf_fileident_bh fibh;
704 struct fileIdentDesc cfi, *fi;
705 int err;
706
707 lock_kernel();
708 err = -EMLINK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700709 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto out;
711
712 err = -EIO;
713 inode = udf_new_inode(dir, S_IFDIR, &err);
714 if (!inode)
715 goto out;
716
717 inode->i_op = &udf_dir_inode_operations;
718 inode->i_fop = &udf_dir_operations;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700719 if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 inode->i_nlink--;
721 mark_inode_dirty(inode);
722 iput(inode);
723 goto out;
724 }
725 inode->i_nlink = 2;
726 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
727 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700728 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
729 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
730 cfi.fileCharacteristics =
731 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700733 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 inode->i_mode = S_IFDIR | mode;
735 if (dir->i_mode & S_ISGID)
736 inode->i_mode |= S_ISGID;
737 mark_inode_dirty(inode);
738
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 inode->i_nlink = 0;
741 mark_inode_dirty(inode);
742 iput(inode);
743 goto out;
744 }
745 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
746 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700747 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
748 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
750 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Dave Hansend8c76e62006-09-30 23:29:04 -0700751 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 mark_inode_dirty(dir);
753 d_instantiate(dentry, inode);
754 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700755 brelse(fibh.ebh);
756 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 err = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700758 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 unlock_kernel();
760 return err;
761}
762
763static int empty_dir(struct inode *dir)
764{
765 struct fileIdentDesc *fi, cfi;
766 struct udf_fileident_bh fibh;
767 loff_t f_pos;
768 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
769 int block;
Jan Karaff116fc2007-05-08 00:35:14 -0700770 kernel_lb_addr eloc;
771 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -0700772 sector_t offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700773 struct extent_position epos = { NULL, 0, {0, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 f_pos = (udf_ext0_offset(dir) >> 2);
776
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700777 fibh.soffset = fibh.eoffset =
778 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
781 fibh.sbh = fibh.ebh = NULL;
782 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700783 &epos, &eloc, &elen,
784 &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700786 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -0700788 epos.offset -= sizeof(short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -0700790 epos.offset -= sizeof(long_ad);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 offset = 0;
793
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700794 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700795 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
797 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700798 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700799 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
801 }
802
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700803 while ((f_pos < size)) {
804 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
805 &elen, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700807 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700809 brelse(fibh.ebh);
810 brelse(fibh.sbh);
811 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return 0;
813 }
814
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700815 if (cfi.lengthFileIdent
816 && (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700818 brelse(fibh.ebh);
819 brelse(fibh.sbh);
820 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822 }
823 }
824 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700825 brelse(fibh.ebh);
826 brelse(fibh.sbh);
827 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 1;
829}
830
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700831static int udf_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700834 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct udf_fileident_bh fibh;
836 struct fileIdentDesc *fi, cfi;
837 kernel_lb_addr tloc;
838
839 retval = -ENOENT;
840 lock_kernel();
841 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
842 if (!fi)
843 goto out;
844
845 retval = -EIO;
846 tloc = lelb_to_cpu(cfi.icb.extLocation);
847 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
848 goto end_rmdir;
849 retval = -ENOTEMPTY;
850 if (!empty_dir(inode))
851 goto end_rmdir;
852 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
853 if (retval)
854 goto end_rmdir;
855 if (inode->i_nlink != 2)
856 udf_warning(inode->i_sb, "udf_rmdir",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700857 "empty directory has nlink != 2 (%d)",
858 inode->i_nlink);
Dave Hansence71ec32006-09-30 23:29:06 -0700859 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 inode->i_size = 0;
Stephen Mollettc007c062007-05-08 00:31:31 -0700861 inode_dec_link_count(dir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700862 inode->i_ctime = dir->i_ctime = dir->i_mtime =
863 current_fs_time(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 mark_inode_dirty(dir);
865
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700866 end_rmdir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700868 brelse(fibh.ebh);
869 brelse(fibh.sbh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700870 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 unlock_kernel();
872 return retval;
873}
874
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700875static int udf_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 int retval;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700878 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct udf_fileident_bh fibh;
880 struct fileIdentDesc *fi;
881 struct fileIdentDesc cfi;
882 kernel_lb_addr tloc;
883
884 retval = -ENOENT;
885 lock_kernel();
886 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
887 if (!fi)
888 goto out;
889
890 retval = -EIO;
891 tloc = lelb_to_cpu(cfi.icb.extLocation);
892 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
893 goto end_unlink;
894
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700895 if (!inode->i_nlink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 udf_debug("Deleting nonexistent file (%lu), %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700897 inode->i_ino, inode->i_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 inode->i_nlink = 1;
899 }
900 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
901 if (retval)
902 goto end_unlink;
903 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
904 mark_inode_dirty(dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700905 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 inode->i_ctime = dir->i_ctime;
907 retval = 0;
908
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700909 end_unlink:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700911 brelse(fibh.ebh);
912 brelse(fibh.sbh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700913 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 unlock_kernel();
915 return retval;
916}
917
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700918static int udf_symlink(struct inode *dir, struct dentry *dentry,
919 const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700921 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct pathComponent *pc;
923 char *compstart;
924 struct udf_fileident_bh fibh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700925 struct extent_position epos = { NULL, 0, {0, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int eoffset, elen = 0;
927 struct fileIdentDesc *fi;
928 struct fileIdentDesc cfi;
929 char *ea;
930 int err;
931 int block;
932 char name[UDF_NAME_LEN];
933 int namelen;
934
935 lock_kernel();
936 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
937 goto out;
938
939 inode->i_mode = S_IFLNK | S_IRWXUGO;
940 inode->i_data.a_ops = &udf_symlink_aops;
941 inode->i_op = &page_symlink_inode_operations;
942
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700943 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB) {
Jan Karaff116fc2007-05-08 00:35:14 -0700944 kernel_lb_addr eloc;
945 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 block = udf_new_block(inode->i_sb, inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700948 UDF_I_LOCATION(inode).
949 partitionReferenceNum,
950 UDF_I_LOCATION(inode).logicalBlockNum,
951 &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!block)
953 goto out_no_entry;
Jan Karaff116fc2007-05-08 00:35:14 -0700954 epos.block = UDF_I_LOCATION(inode);
955 epos.offset = udf_file_entry_alloc_offset(inode);
956 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 eloc.logicalBlockNum = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700958 eloc.partitionReferenceNum =
959 UDF_I_LOCATION(inode).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 elen = inode->i_sb->s_blocksize;
961 UDF_I_LENEXTENTS(inode) = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700962 udf_add_aext(inode, &epos, eloc, elen, 0);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700963 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 block = udf_get_pblock(inode->i_sb, block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700966 UDF_I_LOCATION(inode).
967 partitionReferenceNum, 0);
Jan Karaff116fc2007-05-08 00:35:14 -0700968 epos.bh = udf_tread(inode->i_sb, block);
969 lock_buffer(epos.bh);
970 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
971 set_buffer_uptodate(epos.bh);
972 unlock_buffer(epos.bh);
973 mark_buffer_dirty_inode(epos.bh, inode);
974 ea = epos.bh->b_data + udf_ext0_offset(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700975 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
977
978 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
979 pc = (struct pathComponent *)ea;
980
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700981 if (*symname == '/') {
982 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 symname++;
984 } while (*symname == '/');
985
986 pc->componentType = 1;
987 pc->lengthComponentIdent = 0;
988 pc->componentFileVersionNum = 0;
989 pc += sizeof(struct pathComponent);
990 elen += sizeof(struct pathComponent);
991 }
992
993 err = -ENAMETOOLONG;
994
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700995 while (*symname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (elen + sizeof(struct pathComponent) > eoffset)
997 goto out_no_entry;
998
999 pc = (struct pathComponent *)(ea + elen);
1000
1001 compstart = (char *)symname;
1002
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001003 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 symname++;
1005 } while (*symname && *symname != '/');
1006
1007 pc->componentType = 5;
1008 pc->lengthComponentIdent = 0;
1009 pc->componentFileVersionNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001010 if (compstart[0] == '.') {
1011 if ((symname - compstart) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 pc->componentType = 4;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001013 else if ((symname - compstart) == 2
1014 && compstart[1] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 pc->componentType = 3;
1016 }
1017
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001018 if (pc->componentType == 5) {
1019 if (!
1020 (namelen =
1021 udf_put_filename(inode->i_sb, compstart, name,
1022 symname - compstart)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 goto out_no_entry;
1024
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001025 if (elen + sizeof(struct pathComponent) + namelen >
1026 eoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 goto out_no_entry;
1028 else
1029 pc->lengthComponentIdent = namelen;
1030
1031 memcpy(pc->componentIdent, name, namelen);
1032 }
1033
1034 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1035
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001036 if (*symname) {
1037 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 symname++;
1039 } while (*symname == '/');
1040 }
1041 }
1042
Jan Kara3bf25cb2007-05-08 00:35:16 -07001043 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 inode->i_size = elen;
1045 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1046 UDF_I_LENALLOC(inode) = inode->i_size;
1047 mark_inode_dirty(inode);
1048
1049 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1050 goto out_no_entry;
1051 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1052 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001053 if (UDF_SB_LVIDBH(inode->i_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct logicalVolHeaderDesc *lvhd;
1055 uint64_t uniqueID;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001056 lvhd =
1057 (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->
1058 logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001060 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1061 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1063 uniqueID += 16;
1064 lvhd->uniqueID = cpu_to_le64(uniqueID);
1065 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1066 }
1067 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001068 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 mark_inode_dirty(dir);
1070 }
1071 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001072 brelse(fibh.ebh);
1073 brelse(fibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 d_instantiate(dentry, inode);
1075 err = 0;
1076
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001077 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 unlock_kernel();
1079 return err;
1080
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001081 out_no_entry:
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001082 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 iput(inode);
1084 goto out;
1085}
1086
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001087static int udf_link(struct dentry *old_dentry, struct inode *dir,
1088 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 struct inode *inode = old_dentry->d_inode;
1091 struct udf_fileident_bh fibh;
1092 struct fileIdentDesc cfi, *fi;
1093 int err;
1094
1095 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001096 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 unlock_kernel();
1098 return -EMLINK;
1099 }
1100
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001101 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 unlock_kernel();
1103 return err;
1104 }
1105 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1106 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001107 if (UDF_SB_LVIDBH(inode->i_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct logicalVolHeaderDesc *lvhd;
1109 uint64_t uniqueID;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001110 lvhd =
1111 (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->
1112 logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 uniqueID = le64_to_cpu(lvhd->uniqueID);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001114 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1115 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1117 uniqueID += 16;
1118 lvhd->uniqueID = cpu_to_le64(uniqueID);
1119 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1120 }
1121 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001122 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 mark_inode_dirty(dir);
1124 }
1125 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001126 brelse(fibh.ebh);
1127 brelse(fibh.sbh);
Dave Hansend8c76e62006-09-30 23:29:04 -07001128 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 inode->i_ctime = current_fs_time(inode->i_sb);
1130 mark_inode_dirty(inode);
1131 atomic_inc(&inode->i_count);
1132 d_instantiate(dentry, inode);
1133 unlock_kernel();
1134 return 0;
1135}
1136
1137/* Anybody can rename anything with this: the permission checks are left to the
1138 * higher-level routines.
1139 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001140static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1141 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001143 struct inode *old_inode = old_dentry->d_inode;
1144 struct inode *new_inode = new_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 struct udf_fileident_bh ofibh, nfibh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001146 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi =
1147 NULL, ocfi, ncfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct buffer_head *dir_bh = NULL;
1149 int retval = -ENOENT;
1150 kernel_lb_addr tloc;
1151
1152 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001153 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001155 brelse(ofibh.ebh);
1156 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1159 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001160 != old_inode->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 goto end_rename;
1162
1163 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001164 if (nfi) {
1165 if (!new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001167 brelse(nfibh.ebh);
1168 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 nfi = NULL;
1170 }
1171 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001172 if (S_ISDIR(old_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 uint32_t offset = udf_ext0_offset(old_inode);
1174
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001175 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 retval = -ENOTEMPTY;
1177 if (!empty_dir(new_inode))
1178 goto end_rename;
1179 }
1180 retval = -EIO;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001181 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001183 (UDF_I_EFE(old_inode) ?
1184 sizeof(struct
1185 extendedFileEntry) :
1186 sizeof(struct fileEntry)),
1187 old_inode->i_sb->s_blocksize,
1188 &offset);
1189 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1191 if (!dir_bh)
1192 goto end_rename;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001193 dir_fi =
1194 udf_get_fileident(dir_bh->b_data,
1195 old_inode->i_sb->s_blocksize,
1196 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198 if (!dir_fi)
1199 goto end_rename;
1200 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1201 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001202 != old_dir->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto end_rename;
1204
1205 retval = -EMLINK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001206 if (!new_inode
1207 && new_dir->i_nlink >=
1208 (256 << sizeof(new_dir->i_nlink)) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 goto end_rename;
1210 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001211 if (!nfi) {
1212 nfi =
1213 udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!nfi)
1215 goto end_rename;
1216 }
1217
1218 /*
1219 * Like most other Unix systems, set the ctime for inodes on a
1220 * rename.
1221 */
1222 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1223 mark_inode_dirty(old_inode);
1224
1225 /*
1226 * ok, that's it
1227 */
1228 ncfi.fileVersionNum = ocfi.fileVersionNum;
1229 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1230 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1231 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1232
1233 /* The old fid may have moved - find it again */
1234 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1235 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1236
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001237 if (new_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001239 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1242 mark_inode_dirty(old_dir);
1243
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001244 if (dir_fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1246 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001247 le16_to_cpu(dir_fi->
1248 lengthOfImpUse) +
1249 3) & ~3);
1250 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 mark_inode_dirty(old_inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001252 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 mark_buffer_dirty_inode(dir_bh, old_inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001254 inode_dec_link_count(old_dir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001255 if (new_inode) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001256 inode_dec_link_count(new_inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001257 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001258 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 mark_inode_dirty(new_dir);
1260 }
1261 }
1262
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001263 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (ofibh.sbh != ofibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001265 brelse(ofibh.ebh);
1266 brelse(ofibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
1269 retval = 0;
1270
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001271 end_rename:
Jan Kara3bf25cb2007-05-08 00:35:16 -07001272 brelse(dir_bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001273 if (nfi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (nfibh.sbh != nfibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001275 brelse(nfibh.ebh);
1276 brelse(nfibh.sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 unlock_kernel();
1279 return retval;
1280}
1281
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001282const struct inode_operations udf_dir_inode_operations = {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001283 .lookup = udf_lookup,
1284 .create = udf_create,
1285 .link = udf_link,
1286 .unlink = udf_unlink,
1287 .symlink = udf_symlink,
1288 .mkdir = udf_mkdir,
1289 .rmdir = udf_rmdir,
1290 .mknod = udf_mknod,
1291 .rename = udf_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292};