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