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