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