Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ufs/ufs_dir.c |
| 3 | * |
| 4 | * Copyright (C) 1996 |
| 5 | * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu) |
| 6 | * Laboratory for Computer Science Research Computing Facility |
| 7 | * Rutgers, The State University of New Jersey |
| 8 | * |
| 9 | * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406 |
| 10 | * |
| 11 | * 4.4BSD (FreeBSD) support added on February 1st 1998 by |
| 12 | * Niels Kristian Bech Jensen <nkbj@image.dk> partially based |
| 13 | * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>. |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 14 | * |
| 15 | * Migration to usage of "page cache" on May 2006 by |
| 16 | * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/time.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/ufs_fs.h> |
| 22 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include "swab.h" |
| 25 | #include "util.h" |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* |
| 28 | * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure. |
| 29 | * |
| 30 | * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller. |
| 31 | */ |
| 32 | static inline int ufs_match(struct super_block *sb, int len, |
| 33 | const char * const name, struct ufs_dir_entry * de) |
| 34 | { |
| 35 | if (len != ufs_get_de_namlen(sb, de)) |
| 36 | return 0; |
| 37 | if (!de->d_ino) |
| 38 | return 0; |
| 39 | return !memcmp(name, de->d_name, len); |
| 40 | } |
| 41 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 42 | static int ufs_commit_chunk(struct page *page, unsigned from, unsigned to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 44 | struct inode *dir = page->mapping->host; |
| 45 | int err = 0; |
| 46 | dir->i_version++; |
| 47 | page->mapping->a_ops->commit_write(NULL, page, from, to); |
| 48 | if (IS_DIRSYNC(dir)) |
| 49 | err = write_one_page(page, 1); |
| 50 | else |
| 51 | unlock_page(page); |
| 52 | return err; |
| 53 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 55 | static inline void ufs_put_page(struct page *page) |
| 56 | { |
| 57 | kunmap(page); |
| 58 | page_cache_release(page); |
| 59 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 61 | static inline unsigned long ufs_dir_pages(struct inode *inode) |
| 62 | { |
| 63 | return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT; |
| 64 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 66 | ino_t ufs_inode_by_name(struct inode *dir, struct dentry *dentry) |
| 67 | { |
| 68 | ino_t res = 0; |
| 69 | struct ufs_dir_entry *de; |
| 70 | struct page *page; |
| 71 | |
| 72 | de = ufs_find_entry(dir, dentry, &page); |
| 73 | if (de) { |
| 74 | res = fs32_to_cpu(dir->i_sb, de->d_ino); |
| 75 | ufs_put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 77 | return res; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /* Releases the page */ |
| 82 | void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de, |
| 83 | struct page *page, struct inode *inode) |
| 84 | { |
| 85 | unsigned from = (char *) de - (char *) page_address(page); |
| 86 | unsigned to = from + fs16_to_cpu(dir->i_sb, de->d_reclen); |
| 87 | int err; |
| 88 | |
| 89 | lock_page(page); |
| 90 | err = page->mapping->a_ops->prepare_write(NULL, page, from, to); |
| 91 | BUG_ON(err); |
| 92 | de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino); |
| 93 | ufs_set_de_type(dir->i_sb, de, inode->i_mode); |
| 94 | err = ufs_commit_chunk(page, from, to); |
| 95 | ufs_put_page(page); |
| 96 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 97 | mark_inode_dirty(dir); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | static void ufs_check_page(struct page *page) |
| 102 | { |
| 103 | struct inode *dir = page->mapping->host; |
| 104 | struct super_block *sb = dir->i_sb; |
| 105 | char *kaddr = page_address(page); |
| 106 | unsigned offs, rec_len; |
| 107 | unsigned limit = PAGE_CACHE_SIZE; |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 108 | const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 109 | struct ufs_dir_entry *p; |
| 110 | char *error; |
| 111 | |
| 112 | if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) { |
| 113 | limit = dir->i_size & ~PAGE_CACHE_MASK; |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 114 | if (limit & chunk_mask) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 115 | goto Ebadsize; |
| 116 | if (!limit) |
| 117 | goto out; |
| 118 | } |
| 119 | for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) { |
| 120 | p = (struct ufs_dir_entry *)(kaddr + offs); |
| 121 | rec_len = fs16_to_cpu(sb, p->d_reclen); |
| 122 | |
| 123 | if (rec_len < UFS_DIR_REC_LEN(1)) |
| 124 | goto Eshort; |
| 125 | if (rec_len & 3) |
| 126 | goto Ealign; |
| 127 | if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p))) |
| 128 | goto Enamelen; |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 129 | if (((offs + rec_len - 1) ^ offs) & ~chunk_mask) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 130 | goto Espan; |
| 131 | if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg * |
| 132 | UFS_SB(sb)->s_uspi->s_ncg)) |
| 133 | goto Einumber; |
| 134 | } |
| 135 | if (offs != limit) |
| 136 | goto Eend; |
| 137 | out: |
| 138 | SetPageChecked(page); |
| 139 | return; |
| 140 | |
| 141 | /* Too bad, we had an error */ |
| 142 | |
| 143 | Ebadsize: |
| 144 | ufs_error(sb, "ufs_check_page", |
| 145 | "size of directory #%lu is not a multiple of chunk size", |
| 146 | dir->i_ino |
| 147 | ); |
| 148 | goto fail; |
| 149 | Eshort: |
| 150 | error = "rec_len is smaller than minimal"; |
| 151 | goto bad_entry; |
| 152 | Ealign: |
| 153 | error = "unaligned directory entry"; |
| 154 | goto bad_entry; |
| 155 | Enamelen: |
| 156 | error = "rec_len is too small for name_len"; |
| 157 | goto bad_entry; |
| 158 | Espan: |
| 159 | error = "directory entry across blocks"; |
| 160 | goto bad_entry; |
| 161 | Einumber: |
| 162 | error = "inode out of bounds"; |
| 163 | bad_entry: |
| 164 | ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - " |
| 165 | "offset=%lu, rec_len=%d, name_len=%d", |
| 166 | dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs, |
| 167 | rec_len, ufs_get_de_namlen(sb, p)); |
| 168 | goto fail; |
| 169 | Eend: |
| 170 | p = (struct ufs_dir_entry *)(kaddr + offs); |
| 171 | ufs_error (sb, "ext2_check_page", |
| 172 | "entry in directory #%lu spans the page boundary" |
| 173 | "offset=%lu", |
| 174 | dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs); |
| 175 | fail: |
| 176 | SetPageChecked(page); |
| 177 | SetPageError(page); |
| 178 | } |
| 179 | |
| 180 | static struct page *ufs_get_page(struct inode *dir, unsigned long n) |
| 181 | { |
| 182 | struct address_space *mapping = dir->i_mapping; |
| 183 | struct page *page = read_cache_page(mapping, n, |
| 184 | (filler_t*)mapping->a_ops->readpage, NULL); |
| 185 | if (!IS_ERR(page)) { |
| 186 | wait_on_page_locked(page); |
| 187 | kmap(page); |
| 188 | if (!PageUptodate(page)) |
| 189 | goto fail; |
| 190 | if (!PageChecked(page)) |
| 191 | ufs_check_page(page); |
| 192 | if (PageError(page)) |
| 193 | goto fail; |
| 194 | } |
| 195 | return page; |
| 196 | |
| 197 | fail: |
| 198 | ufs_put_page(page); |
| 199 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 203 | * Return the offset into page `page_nr' of the last valid |
| 204 | * byte in that page, plus one. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | */ |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 206 | static unsigned |
| 207 | ufs_last_byte(struct inode *inode, unsigned long page_nr) |
| 208 | { |
| 209 | unsigned last_byte = inode->i_size; |
| 210 | |
| 211 | last_byte -= page_nr << PAGE_CACHE_SHIFT; |
| 212 | if (last_byte > PAGE_CACHE_SIZE) |
| 213 | last_byte = PAGE_CACHE_SIZE; |
| 214 | return last_byte; |
| 215 | } |
| 216 | |
| 217 | static inline struct ufs_dir_entry * |
| 218 | ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p) |
| 219 | { |
| 220 | return (struct ufs_dir_entry *)((char *)p + |
| 221 | fs16_to_cpu(sb, p->d_reclen)); |
| 222 | } |
| 223 | |
| 224 | struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p) |
| 225 | { |
| 226 | struct page *page = ufs_get_page(dir, 0); |
| 227 | struct ufs_dir_entry *de = NULL; |
| 228 | |
| 229 | if (!IS_ERR(page)) { |
| 230 | de = ufs_next_entry(dir->i_sb, |
| 231 | (struct ufs_dir_entry *)page_address(page)); |
| 232 | *p = page; |
| 233 | } |
| 234 | return de; |
| 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * ufs_find_entry() |
| 239 | * |
| 240 | * finds an entry in the specified directory with the wanted name. It |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 241 | * returns the page in which the entry was found, and the entry itself |
| 242 | * (as a parameter - res_dir). Page is returned mapped and unlocked. |
| 243 | * Entry is guaranteed to be valid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | */ |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 245 | struct ufs_dir_entry *ufs_find_entry(struct inode *dir, struct dentry *dentry, |
| 246 | struct page **res_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | struct super_block *sb = dir->i_sb; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 249 | const char *name = dentry->d_name.name; |
| 250 | int namelen = dentry->d_name.len; |
| 251 | unsigned reclen = UFS_DIR_REC_LEN(namelen); |
| 252 | unsigned long start, n; |
| 253 | unsigned long npages = ufs_dir_pages(dir); |
| 254 | struct page *page = NULL; |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 255 | struct ufs_inode_info *ui = UFS_I(dir); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 256 | struct ufs_dir_entry *de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 258 | UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 260 | if (npages == 0 || namelen > UFS_MAXNAMLEN) |
| 261 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 263 | /* OFFSET_CACHE */ |
| 264 | *res_page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 266 | start = ui->i_dir_start_lookup; |
| 267 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 268 | if (start >= npages) |
| 269 | start = 0; |
| 270 | n = start; |
| 271 | do { |
| 272 | char *kaddr; |
| 273 | page = ufs_get_page(dir, n); |
| 274 | if (!IS_ERR(page)) { |
| 275 | kaddr = page_address(page); |
| 276 | de = (struct ufs_dir_entry *) kaddr; |
| 277 | kaddr += ufs_last_byte(dir, n) - reclen; |
| 278 | while ((char *) de <= kaddr) { |
| 279 | if (de->d_reclen == 0) { |
| 280 | ufs_error(dir->i_sb, __FUNCTION__, |
| 281 | "zero-length directory entry"); |
| 282 | ufs_put_page(page); |
| 283 | goto out; |
| 284 | } |
| 285 | if (ufs_match(sb, namelen, name, de)) |
| 286 | goto found; |
| 287 | de = ufs_next_entry(sb, de); |
| 288 | } |
| 289 | ufs_put_page(page); |
| 290 | } |
| 291 | if (++n >= npages) |
| 292 | n = 0; |
| 293 | } while (n != start); |
| 294 | out: |
| 295 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 297 | found: |
| 298 | *res_page = page; |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 299 | ui->i_dir_start_lookup = n; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 300 | return de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 304 | * Parent is locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | */ |
| 306 | int ufs_add_link(struct dentry *dentry, struct inode *inode) |
| 307 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | struct inode *dir = dentry->d_parent->d_inode; |
| 309 | const char *name = dentry->d_name.name; |
| 310 | int namelen = dentry->d_name.len; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 311 | struct super_block *sb = dir->i_sb; |
| 312 | unsigned reclen = UFS_DIR_REC_LEN(namelen); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 313 | const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 314 | unsigned short rec_len, name_len; |
| 315 | struct page *page = NULL; |
| 316 | struct ufs_dir_entry *de; |
| 317 | unsigned long npages = ufs_dir_pages(dir); |
| 318 | unsigned long n; |
| 319 | char *kaddr; |
| 320 | unsigned from, to; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | int err; |
| 322 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 323 | UFSD("ENTER, name %s, namelen %u\n", name, namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 325 | /* |
| 326 | * We take care of directory expansion in the same loop. |
| 327 | * This code plays outside i_size, so it locks the page |
| 328 | * to protect that region. |
| 329 | */ |
| 330 | for (n = 0; n <= npages; n++) { |
| 331 | char *dir_end; |
| 332 | |
| 333 | page = ufs_get_page(dir, n); |
| 334 | err = PTR_ERR(page); |
| 335 | if (IS_ERR(page)) |
| 336 | goto out; |
| 337 | lock_page(page); |
| 338 | kaddr = page_address(page); |
| 339 | dir_end = kaddr + ufs_last_byte(dir, n); |
| 340 | de = (struct ufs_dir_entry *)kaddr; |
| 341 | kaddr += PAGE_CACHE_SIZE - reclen; |
| 342 | while ((char *)de <= kaddr) { |
| 343 | if ((char *)de == dir_end) { |
| 344 | /* We hit i_size */ |
| 345 | name_len = 0; |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 346 | rec_len = chunk_size; |
| 347 | de->d_reclen = cpu_to_fs16(sb, chunk_size); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 348 | de->d_ino = 0; |
| 349 | goto got_it; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 351 | if (de->d_reclen == 0) { |
| 352 | ufs_error(dir->i_sb, __FUNCTION__, |
| 353 | "zero-length directory entry"); |
| 354 | err = -EIO; |
| 355 | goto out_unlock; |
| 356 | } |
| 357 | err = -EEXIST; |
| 358 | if (ufs_match(sb, namelen, name, de)) |
| 359 | goto out_unlock; |
| 360 | name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)); |
| 361 | rec_len = fs16_to_cpu(sb, de->d_reclen); |
| 362 | if (!de->d_ino && rec_len >= reclen) |
| 363 | goto got_it; |
| 364 | if (rec_len >= name_len + reclen) |
| 365 | goto got_it; |
| 366 | de = (struct ufs_dir_entry *) ((char *) de + rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 368 | unlock_page(page); |
| 369 | ufs_put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 371 | BUG(); |
| 372 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 374 | got_it: |
| 375 | from = (char*)de - (char*)page_address(page); |
| 376 | to = from + rec_len; |
| 377 | err = page->mapping->a_ops->prepare_write(NULL, page, from, to); |
| 378 | if (err) |
| 379 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (de->d_ino) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 381 | struct ufs_dir_entry *de1 = |
| 382 | (struct ufs_dir_entry *) ((char *) de + name_len); |
| 383 | de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len); |
| 384 | de->d_reclen = cpu_to_fs16(sb, name_len); |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | de = de1; |
| 387 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | ufs_set_de_namlen(sb, de, namelen); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 390 | memcpy(de->d_name, name, namelen + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | de->d_ino = cpu_to_fs32(sb, inode->i_ino); |
| 392 | ufs_set_de_type(sb, de, inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 394 | err = ufs_commit_chunk(page, from, to); |
| 395 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 396 | |
| 397 | mark_inode_dirty(dir); |
| 398 | /* OFFSET_CACHE */ |
| 399 | out_put: |
| 400 | ufs_put_page(page); |
| 401 | out: |
| 402 | return err; |
| 403 | out_unlock: |
| 404 | unlock_page(page); |
| 405 | goto out_put; |
| 406 | } |
| 407 | |
| 408 | static inline unsigned |
| 409 | ufs_validate_entry(struct super_block *sb, char *base, |
| 410 | unsigned offset, unsigned mask) |
| 411 | { |
| 412 | struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset); |
| 413 | struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask)); |
| 414 | while ((char*)p < (char*)de) { |
| 415 | if (p->d_reclen == 0) |
| 416 | break; |
| 417 | p = ufs_next_entry(sb, p); |
| 418 | } |
| 419 | return (char *)p - base; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | /* |
| 424 | * This is blatantly stolen from ext2fs |
| 425 | */ |
| 426 | static int |
| 427 | ufs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 428 | { |
| 429 | loff_t pos = filp->f_pos; |
Josef Sipek | 763454d | 2006-12-08 02:37:45 -0800 | [diff] [blame] | 430 | struct inode *inode = filp->f_path.dentry->d_inode; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 431 | struct super_block *sb = inode->i_sb; |
| 432 | unsigned int offset = pos & ~PAGE_CACHE_MASK; |
| 433 | unsigned long n = pos >> PAGE_CACHE_SHIFT; |
| 434 | unsigned long npages = ufs_dir_pages(inode); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 435 | unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 436 | int need_revalidate = filp->f_version != inode->i_version; |
| 437 | unsigned flags = UFS_SB(sb)->s_flags; |
| 438 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 439 | UFSD("BEGIN\n"); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 440 | |
| 441 | if (pos > inode->i_size - UFS_DIR_REC_LEN(1)) |
| 442 | return 0; |
| 443 | |
| 444 | for ( ; n < npages; n++, offset = 0) { |
| 445 | char *kaddr, *limit; |
| 446 | struct ufs_dir_entry *de; |
| 447 | |
| 448 | struct page *page = ufs_get_page(inode, n); |
| 449 | |
| 450 | if (IS_ERR(page)) { |
| 451 | ufs_error(sb, __FUNCTION__, |
| 452 | "bad page in #%lu", |
| 453 | inode->i_ino); |
| 454 | filp->f_pos += PAGE_CACHE_SIZE - offset; |
| 455 | return -EIO; |
| 456 | } |
| 457 | kaddr = page_address(page); |
| 458 | if (unlikely(need_revalidate)) { |
| 459 | if (offset) { |
| 460 | offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask); |
| 461 | filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset; |
| 462 | } |
| 463 | filp->f_version = inode->i_version; |
| 464 | need_revalidate = 0; |
| 465 | } |
| 466 | de = (struct ufs_dir_entry *)(kaddr+offset); |
| 467 | limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1); |
| 468 | for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) { |
| 469 | if (de->d_reclen == 0) { |
| 470 | ufs_error(sb, __FUNCTION__, |
| 471 | "zero-length directory entry"); |
| 472 | ufs_put_page(page); |
| 473 | return -EIO; |
| 474 | } |
| 475 | if (de->d_ino) { |
| 476 | int over; |
| 477 | unsigned char d_type = DT_UNKNOWN; |
| 478 | |
| 479 | offset = (char *)de - kaddr; |
| 480 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 481 | UFSD("filldir(%s,%u)\n", de->d_name, |
| 482 | fs32_to_cpu(sb, de->d_ino)); |
| 483 | UFSD("namlen %u\n", ufs_get_de_namlen(sb, de)); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 484 | |
| 485 | if ((flags & UFS_DE_MASK) == UFS_DE_44BSD) |
| 486 | d_type = de->d_u.d_44.d_type; |
| 487 | |
| 488 | over = filldir(dirent, de->d_name, |
| 489 | ufs_get_de_namlen(sb, de), |
| 490 | (n<<PAGE_CACHE_SHIFT) | offset, |
| 491 | fs32_to_cpu(sb, de->d_ino), d_type); |
| 492 | if (over) { |
| 493 | ufs_put_page(page); |
| 494 | return 0; |
| 495 | } |
| 496 | } |
| 497 | filp->f_pos += fs16_to_cpu(sb, de->d_reclen); |
| 498 | } |
| 499 | ufs_put_page(page); |
| 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | return 0; |
| 502 | } |
| 503 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | /* |
| 506 | * ufs_delete_entry deletes a directory entry by merging it with the |
| 507 | * previous entry. |
| 508 | */ |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 509 | int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir, |
| 510 | struct page * page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 512 | struct super_block *sb = inode->i_sb; |
| 513 | struct address_space *mapping = page->mapping; |
| 514 | char *kaddr = page_address(page); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 515 | unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 516 | unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen); |
| 517 | struct ufs_dir_entry *pde = NULL; |
| 518 | struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from); |
| 519 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 521 | UFSD("ENTER\n"); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 522 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 523 | UFSD("ino %u, reclen %u, namlen %u, name %s\n", |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 524 | fs32_to_cpu(sb, de->d_ino), |
| 525 | fs16_to_cpu(sb, de->d_reclen), |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 526 | ufs_get_de_namlen(sb, de), de->d_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 528 | while ((char*)de < (char*)dir) { |
| 529 | if (de->d_reclen == 0) { |
| 530 | ufs_error(inode->i_sb, __FUNCTION__, |
| 531 | "zero-length directory entry"); |
| 532 | err = -EIO; |
| 533 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 535 | pde = de; |
| 536 | de = ufs_next_entry(sb, de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 538 | if (pde) |
| 539 | from = (char*)pde - (char*)page_address(page); |
| 540 | lock_page(page); |
| 541 | err = mapping->a_ops->prepare_write(NULL, page, from, to); |
| 542 | BUG_ON(err); |
| 543 | if (pde) |
| 544 | pde->d_reclen = cpu_to_fs16(sb, to-from); |
| 545 | dir->d_ino = 0; |
| 546 | err = ufs_commit_chunk(page, from, to); |
| 547 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 548 | mark_inode_dirty(inode); |
| 549 | out: |
| 550 | ufs_put_page(page); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 551 | UFSD("EXIT\n"); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 552 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | int ufs_make_empty(struct inode * inode, struct inode *dir) |
| 556 | { |
| 557 | struct super_block * sb = dir->i_sb; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 558 | struct address_space *mapping = inode->i_mapping; |
| 559 | struct page *page = grab_cache_page(mapping, 0); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 560 | const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | struct ufs_dir_entry * de; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 562 | char *base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | int err; |
| 564 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 565 | if (!page) |
| 566 | return -ENOMEM; |
| 567 | kmap(page); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 568 | err = mapping->a_ops->prepare_write(NULL, page, 0, chunk_size); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 569 | if (err) { |
| 570 | unlock_page(page); |
| 571 | goto fail; |
| 572 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 574 | |
| 575 | base = (char*)page_address(page); |
| 576 | memset(base, 0, PAGE_CACHE_SIZE); |
| 577 | |
| 578 | de = (struct ufs_dir_entry *) base; |
| 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | de->d_ino = cpu_to_fs32(sb, inode->i_ino); |
| 581 | ufs_set_de_type(sb, de, inode->i_mode); |
| 582 | ufs_set_de_namlen(sb, de, 1); |
| 583 | de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1)); |
| 584 | strcpy (de->d_name, "."); |
| 585 | de = (struct ufs_dir_entry *) |
| 586 | ((char *)de + fs16_to_cpu(sb, de->d_reclen)); |
| 587 | de->d_ino = cpu_to_fs32(sb, dir->i_ino); |
| 588 | ufs_set_de_type(sb, de, dir->i_mode); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 589 | de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | ufs_set_de_namlen(sb, de, 2); |
| 591 | strcpy (de->d_name, ".."); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 592 | |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 593 | err = ufs_commit_chunk(page, 0, chunk_size); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 594 | fail: |
| 595 | kunmap(page); |
| 596 | page_cache_release(page); |
| 597 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* |
| 601 | * routine to check that the specified directory is empty (for rmdir) |
| 602 | */ |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 603 | int ufs_empty_dir(struct inode * inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 605 | struct super_block *sb = inode->i_sb; |
| 606 | struct page *page = NULL; |
| 607 | unsigned long i, npages = ufs_dir_pages(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 609 | for (i = 0; i < npages; i++) { |
| 610 | char *kaddr; |
| 611 | struct ufs_dir_entry *de; |
| 612 | page = ufs_get_page(inode, i); |
| 613 | |
| 614 | if (IS_ERR(page)) |
| 615 | continue; |
| 616 | |
| 617 | kaddr = page_address(page); |
| 618 | de = (struct ufs_dir_entry *)kaddr; |
| 619 | kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1); |
| 620 | |
| 621 | while ((char *)de <= kaddr) { |
| 622 | if (de->d_reclen == 0) { |
| 623 | ufs_error(inode->i_sb, __FUNCTION__, |
| 624 | "zero-length directory entry: " |
| 625 | "kaddr=%p, de=%p\n", kaddr, de); |
| 626 | goto not_empty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 628 | if (de->d_ino) { |
| 629 | u16 namelen=ufs_get_de_namlen(sb, de); |
| 630 | /* check for . and .. */ |
| 631 | if (de->d_name[0] != '.') |
| 632 | goto not_empty; |
| 633 | if (namelen > 2) |
| 634 | goto not_empty; |
| 635 | if (namelen < 2) { |
| 636 | if (inode->i_ino != |
| 637 | fs32_to_cpu(sb, de->d_ino)) |
| 638 | goto not_empty; |
| 639 | } else if (de->d_name[1] != '.') |
| 640 | goto not_empty; |
| 641 | } |
| 642 | de = ufs_next_entry(sb, de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 644 | ufs_put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | return 1; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 647 | |
| 648 | not_empty: |
| 649 | ufs_put_page(page); |
| 650 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 653 | const struct file_operations ufs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | .read = generic_read_dir, |
| 655 | .readdir = ufs_readdir, |
| 656 | .fsync = file_fsync, |
| 657 | }; |