Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/sysv/dir.c |
| 3 | * |
| 4 | * minix/dir.c |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | * |
| 7 | * coh/dir.c |
| 8 | * Copyright (C) 1993 Pascal Haible, Bruno Haible |
| 9 | * |
| 10 | * sysv/dir.c |
| 11 | * Copyright (C) 1993 Bruno Haible |
| 12 | * |
| 13 | * SystemV/Coherent directory handling functions |
| 14 | */ |
| 15 | |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/highmem.h> |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 18 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "sysv.h" |
| 20 | |
| 21 | static int sysv_readdir(struct file *, void *, filldir_t); |
| 22 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 23 | const struct file_operations sysv_dir_operations = { |
Al Viro | 5ac3455 | 2009-06-16 23:59:37 -0400 | [diff] [blame] | 24 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | .read = generic_read_dir, |
| 26 | .readdir = sysv_readdir, |
Al Viro | 05459ca | 2009-06-07 15:29:45 -0400 | [diff] [blame] | 27 | .fsync = simple_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | static inline void dir_put_page(struct page *page) |
| 31 | { |
| 32 | kunmap(page); |
| 33 | page_cache_release(page); |
| 34 | } |
| 35 | |
| 36 | static inline unsigned long dir_pages(struct inode *inode) |
| 37 | { |
| 38 | return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT; |
| 39 | } |
| 40 | |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 41 | static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 43 | struct address_space *mapping = page->mapping; |
| 44 | struct inode *dir = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | int err = 0; |
| 46 | |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 47 | block_write_end(NULL, mapping, pos, len, len, page, NULL); |
| 48 | if (pos+len > dir->i_size) { |
| 49 | i_size_write(dir, pos+len); |
| 50 | mark_inode_dirty(dir); |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (IS_DIRSYNC(dir)) |
| 53 | err = write_one_page(page, 1); |
| 54 | else |
| 55 | unlock_page(page); |
| 56 | return err; |
| 57 | } |
| 58 | |
| 59 | static struct page * dir_get_page(struct inode *dir, unsigned long n) |
| 60 | { |
| 61 | struct address_space *mapping = dir->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 62 | struct page *page = read_mapping_page(mapping, n, NULL); |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 63 | if (!IS_ERR(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | kmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int sysv_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 69 | { |
| 70 | unsigned long pos = filp->f_pos; |
Josef Sipek | 5495ca65 | 2006-12-08 02:37:43 -0800 | [diff] [blame] | 71 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | struct super_block *sb = inode->i_sb; |
| 73 | unsigned offset = pos & ~PAGE_CACHE_MASK; |
| 74 | unsigned long n = pos >> PAGE_CACHE_SHIFT; |
| 75 | unsigned long npages = dir_pages(inode); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1); |
| 78 | if (pos >= inode->i_size) |
| 79 | goto done; |
| 80 | |
| 81 | for ( ; n < npages; n++, offset = 0) { |
| 82 | char *kaddr, *limit; |
| 83 | struct sysv_dir_entry *de; |
| 84 | struct page *page = dir_get_page(inode, n); |
| 85 | |
| 86 | if (IS_ERR(page)) |
| 87 | continue; |
| 88 | kaddr = (char *)page_address(page); |
| 89 | de = (struct sysv_dir_entry *)(kaddr+offset); |
| 90 | limit = kaddr + PAGE_CACHE_SIZE - SYSV_DIRSIZE; |
| 91 | for ( ;(char*)de <= limit; de++) { |
| 92 | char *name = de->name; |
| 93 | int over; |
| 94 | |
| 95 | if (!de->inode) |
| 96 | continue; |
| 97 | |
| 98 | offset = (char *)de - kaddr; |
| 99 | |
| 100 | over = filldir(dirent, name, strnlen(name,SYSV_NAMELEN), |
Andrew Morton | 54b21a7 | 2006-01-08 01:03:05 -0800 | [diff] [blame] | 101 | ((loff_t)n<<PAGE_CACHE_SHIFT) | offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | fs16_to_cpu(SYSV_SB(sb), de->inode), |
| 103 | DT_UNKNOWN); |
| 104 | if (over) { |
| 105 | dir_put_page(page); |
| 106 | goto done; |
| 107 | } |
| 108 | } |
| 109 | dir_put_page(page); |
| 110 | } |
| 111 | |
| 112 | done: |
Andrew Morton | 54b21a7 | 2006-01-08 01:03:05 -0800 | [diff] [blame] | 113 | filp->f_pos = ((loff_t)n << PAGE_CACHE_SHIFT) | offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* compare strings: name[0..len-1] (not zero-terminated) and |
| 118 | * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1]) |
| 119 | */ |
| 120 | static inline int namecompare(int len, int maxlen, |
| 121 | const char * name, const char * buffer) |
| 122 | { |
| 123 | if (len < maxlen && buffer[len]) |
| 124 | return 0; |
| 125 | return !memcmp(name, buffer, len); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * sysv_find_entry() |
| 130 | * |
| 131 | * finds an entry in the specified directory with the wanted name. It |
| 132 | * returns the cache buffer in which the entry was found, and the entry |
| 133 | * itself (as a parameter - res_dir). It does NOT read the inode of the |
| 134 | * entry - you'll have to do that yourself if you want to. |
| 135 | */ |
| 136 | struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page) |
| 137 | { |
| 138 | const char * name = dentry->d_name.name; |
| 139 | int namelen = dentry->d_name.len; |
| 140 | struct inode * dir = dentry->d_parent->d_inode; |
| 141 | unsigned long start, n; |
| 142 | unsigned long npages = dir_pages(dir); |
| 143 | struct page *page = NULL; |
| 144 | struct sysv_dir_entry *de; |
| 145 | |
| 146 | *res_page = NULL; |
| 147 | |
| 148 | start = SYSV_I(dir)->i_dir_start_lookup; |
| 149 | if (start >= npages) |
| 150 | start = 0; |
| 151 | n = start; |
| 152 | |
| 153 | do { |
| 154 | char *kaddr; |
| 155 | page = dir_get_page(dir, n); |
| 156 | if (!IS_ERR(page)) { |
| 157 | kaddr = (char*)page_address(page); |
| 158 | de = (struct sysv_dir_entry *) kaddr; |
| 159 | kaddr += PAGE_CACHE_SIZE - SYSV_DIRSIZE; |
| 160 | for ( ; (char *) de <= kaddr ; de++) { |
| 161 | if (!de->inode) |
| 162 | continue; |
| 163 | if (namecompare(namelen, SYSV_NAMELEN, |
| 164 | name, de->name)) |
| 165 | goto found; |
| 166 | } |
| 167 | } |
| 168 | dir_put_page(page); |
| 169 | |
| 170 | if (++n >= npages) |
| 171 | n = 0; |
| 172 | } while (n != start); |
| 173 | |
| 174 | return NULL; |
| 175 | |
| 176 | found: |
| 177 | SYSV_I(dir)->i_dir_start_lookup = n; |
| 178 | *res_page = page; |
| 179 | return de; |
| 180 | } |
| 181 | |
| 182 | int sysv_add_link(struct dentry *dentry, struct inode *inode) |
| 183 | { |
| 184 | struct inode *dir = dentry->d_parent->d_inode; |
| 185 | const char * name = dentry->d_name.name; |
| 186 | int namelen = dentry->d_name.len; |
| 187 | struct page *page = NULL; |
| 188 | struct sysv_dir_entry * de; |
| 189 | unsigned long npages = dir_pages(dir); |
| 190 | unsigned long n; |
| 191 | char *kaddr; |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 192 | loff_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | int err; |
| 194 | |
| 195 | /* We take care of directory expansion in the same loop */ |
| 196 | for (n = 0; n <= npages; n++) { |
| 197 | page = dir_get_page(dir, n); |
| 198 | err = PTR_ERR(page); |
| 199 | if (IS_ERR(page)) |
| 200 | goto out; |
| 201 | kaddr = (char*)page_address(page); |
| 202 | de = (struct sysv_dir_entry *)kaddr; |
| 203 | kaddr += PAGE_CACHE_SIZE - SYSV_DIRSIZE; |
| 204 | while ((char *)de <= kaddr) { |
| 205 | if (!de->inode) |
| 206 | goto got_it; |
| 207 | err = -EEXIST; |
| 208 | if (namecompare(namelen, SYSV_NAMELEN, name, de->name)) |
| 209 | goto out_page; |
| 210 | de++; |
| 211 | } |
| 212 | dir_put_page(page); |
| 213 | } |
| 214 | BUG(); |
| 215 | return -EINVAL; |
| 216 | |
| 217 | got_it: |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 218 | pos = page_offset(page) + |
| 219 | (char*)de - (char*)page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | lock_page(page); |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 221 | err = __sysv_write_begin(NULL, page->mapping, pos, SYSV_DIRSIZE, |
| 222 | AOP_FLAG_UNINTERRUPTIBLE, &page, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (err) |
| 224 | goto out_unlock; |
| 225 | memcpy (de->name, name, namelen); |
| 226 | memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2); |
| 227 | de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino); |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 228 | err = dir_commit_chunk(page, pos, SYSV_DIRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 230 | mark_inode_dirty(dir); |
| 231 | out_page: |
| 232 | dir_put_page(page); |
| 233 | out: |
| 234 | return err; |
| 235 | out_unlock: |
| 236 | unlock_page(page); |
| 237 | goto out_page; |
| 238 | } |
| 239 | |
| 240 | int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page) |
| 241 | { |
| 242 | struct address_space *mapping = page->mapping; |
| 243 | struct inode *inode = (struct inode*)mapping->host; |
| 244 | char *kaddr = (char*)page_address(page); |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 245 | loff_t pos = page_offset(page) + (char *)de - kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | int err; |
| 247 | |
| 248 | lock_page(page); |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 249 | err = __sysv_write_begin(NULL, mapping, pos, SYSV_DIRSIZE, |
| 250 | AOP_FLAG_UNINTERRUPTIBLE, &page, NULL); |
Eric Sesterhenn | d6735bf | 2006-04-02 13:39:21 +0200 | [diff] [blame] | 251 | BUG_ON(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | de->inode = 0; |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 253 | err = dir_commit_chunk(page, pos, SYSV_DIRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | dir_put_page(page); |
| 255 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 256 | mark_inode_dirty(inode); |
| 257 | return err; |
| 258 | } |
| 259 | |
| 260 | int sysv_make_empty(struct inode *inode, struct inode *dir) |
| 261 | { |
| 262 | struct address_space *mapping = inode->i_mapping; |
| 263 | struct page *page = grab_cache_page(mapping, 0); |
| 264 | struct sysv_dir_entry * de; |
| 265 | char *base; |
| 266 | int err; |
| 267 | |
| 268 | if (!page) |
| 269 | return -ENOMEM; |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 270 | err = __sysv_write_begin(NULL, mapping, 0, 2 * SYSV_DIRSIZE, |
| 271 | AOP_FLAG_UNINTERRUPTIBLE, &page, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (err) { |
| 273 | unlock_page(page); |
| 274 | goto fail; |
| 275 | } |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 276 | kmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | base = (char*)page_address(page); |
| 279 | memset(base, 0, PAGE_CACHE_SIZE); |
| 280 | |
| 281 | de = (struct sysv_dir_entry *) base; |
| 282 | de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino); |
| 283 | strcpy(de->name,"."); |
| 284 | de++; |
| 285 | de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino); |
| 286 | strcpy(de->name,".."); |
| 287 | |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 288 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE); |
| 290 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | page_cache_release(page); |
| 292 | return err; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * routine to check that the specified directory is empty (for rmdir) |
| 297 | */ |
| 298 | int sysv_empty_dir(struct inode * inode) |
| 299 | { |
| 300 | struct super_block *sb = inode->i_sb; |
| 301 | struct page *page = NULL; |
| 302 | unsigned long i, npages = dir_pages(inode); |
| 303 | |
| 304 | for (i = 0; i < npages; i++) { |
| 305 | char *kaddr; |
| 306 | struct sysv_dir_entry * de; |
| 307 | page = dir_get_page(inode, i); |
| 308 | |
| 309 | if (IS_ERR(page)) |
| 310 | continue; |
| 311 | |
| 312 | kaddr = (char *)page_address(page); |
| 313 | de = (struct sysv_dir_entry *)kaddr; |
| 314 | kaddr += PAGE_CACHE_SIZE-SYSV_DIRSIZE; |
| 315 | |
| 316 | for ( ;(char *)de <= kaddr; de++) { |
| 317 | if (!de->inode) |
| 318 | continue; |
| 319 | /* check for . and .. */ |
| 320 | if (de->name[0] != '.') |
| 321 | goto not_empty; |
| 322 | if (!de->name[1]) { |
| 323 | if (de->inode == cpu_to_fs16(SYSV_SB(sb), |
| 324 | inode->i_ino)) |
| 325 | continue; |
| 326 | goto not_empty; |
| 327 | } |
| 328 | if (de->name[1] != '.' || de->name[2]) |
| 329 | goto not_empty; |
| 330 | } |
| 331 | dir_put_page(page); |
| 332 | } |
| 333 | return 1; |
| 334 | |
| 335 | not_empty: |
| 336 | dir_put_page(page); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* Releases the page */ |
| 341 | void sysv_set_link(struct sysv_dir_entry *de, struct page *page, |
| 342 | struct inode *inode) |
| 343 | { |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 344 | struct address_space *mapping = page->mapping; |
| 345 | struct inode *dir = mapping->host; |
| 346 | loff_t pos = page_offset(page) + |
| 347 | (char *)de-(char*)page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | int err; |
| 349 | |
| 350 | lock_page(page); |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 351 | err = __sysv_write_begin(NULL, mapping, pos, SYSV_DIRSIZE, |
| 352 | AOP_FLAG_UNINTERRUPTIBLE, &page, NULL); |
Eric Sesterhenn | d6735bf | 2006-04-02 13:39:21 +0200 | [diff] [blame] | 353 | BUG_ON(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino); |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 355 | err = dir_commit_chunk(page, pos, SYSV_DIRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | dir_put_page(page); |
| 357 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 358 | mark_inode_dirty(dir); |
| 359 | } |
| 360 | |
| 361 | struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p) |
| 362 | { |
| 363 | struct page *page = dir_get_page(dir, 0); |
| 364 | struct sysv_dir_entry *de = NULL; |
| 365 | |
| 366 | if (!IS_ERR(page)) { |
| 367 | de = (struct sysv_dir_entry*) page_address(page) + 1; |
| 368 | *p = page; |
| 369 | } |
| 370 | return de; |
| 371 | } |
| 372 | |
| 373 | ino_t sysv_inode_by_name(struct dentry *dentry) |
| 374 | { |
| 375 | struct page *page; |
| 376 | struct sysv_dir_entry *de = sysv_find_entry (dentry, &page); |
| 377 | ino_t res = 0; |
| 378 | |
| 379 | if (de) { |
| 380 | res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode); |
| 381 | dir_put_page(page); |
| 382 | } |
| 383 | return res; |
| 384 | } |