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 | |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 21 | static int sysv_readdir(struct file *, struct dir_context *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 26 | .iterate = sysv_readdir, |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 27 | .fsync = generic_file_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 | |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 68 | static int sysv_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 70 | unsigned long pos = ctx->pos; |
| 71 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | struct super_block *sb = inode->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | unsigned long npages = dir_pages(inode); |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 74 | unsigned offset; |
| 75 | unsigned long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 77 | ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | if (pos >= inode->i_size) |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 79 | return 0; |
| 80 | |
| 81 | offset = pos & ~PAGE_CACHE_MASK; |
| 82 | n = pos >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | for ( ; n < npages; n++, offset = 0) { |
| 85 | char *kaddr, *limit; |
| 86 | struct sysv_dir_entry *de; |
| 87 | struct page *page = dir_get_page(inode, n); |
| 88 | |
| 89 | if (IS_ERR(page)) |
| 90 | continue; |
| 91 | kaddr = (char *)page_address(page); |
| 92 | de = (struct sysv_dir_entry *)(kaddr+offset); |
| 93 | limit = kaddr + PAGE_CACHE_SIZE - SYSV_DIRSIZE; |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 94 | for ( ;(char*)de <= limit; de++, ctx->pos += sizeof(*de)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | char *name = de->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | if (!de->inode) |
| 98 | continue; |
| 99 | |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 100 | if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | fs16_to_cpu(SYSV_SB(sb), de->inode), |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 102 | DT_UNKNOWN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | dir_put_page(page); |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 104 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | dir_put_page(page); |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* compare strings: name[0..len-1] (not zero-terminated) and |
| 113 | * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1]) |
| 114 | */ |
| 115 | static inline int namecompare(int len, int maxlen, |
| 116 | const char * name, const char * buffer) |
| 117 | { |
| 118 | if (len < maxlen && buffer[len]) |
| 119 | return 0; |
| 120 | return !memcmp(name, buffer, len); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * sysv_find_entry() |
| 125 | * |
| 126 | * finds an entry in the specified directory with the wanted name. It |
| 127 | * returns the cache buffer in which the entry was found, and the entry |
| 128 | * itself (as a parameter - res_dir). It does NOT read the inode of the |
| 129 | * entry - you'll have to do that yourself if you want to. |
| 130 | */ |
| 131 | struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page) |
| 132 | { |
| 133 | const char * name = dentry->d_name.name; |
| 134 | int namelen = dentry->d_name.len; |
| 135 | struct inode * dir = dentry->d_parent->d_inode; |
| 136 | unsigned long start, n; |
| 137 | unsigned long npages = dir_pages(dir); |
| 138 | struct page *page = NULL; |
| 139 | struct sysv_dir_entry *de; |
| 140 | |
| 141 | *res_page = NULL; |
| 142 | |
| 143 | start = SYSV_I(dir)->i_dir_start_lookup; |
| 144 | if (start >= npages) |
| 145 | start = 0; |
| 146 | n = start; |
| 147 | |
| 148 | do { |
| 149 | char *kaddr; |
| 150 | page = dir_get_page(dir, n); |
| 151 | if (!IS_ERR(page)) { |
| 152 | kaddr = (char*)page_address(page); |
| 153 | de = (struct sysv_dir_entry *) kaddr; |
| 154 | kaddr += PAGE_CACHE_SIZE - SYSV_DIRSIZE; |
| 155 | for ( ; (char *) de <= kaddr ; de++) { |
| 156 | if (!de->inode) |
| 157 | continue; |
| 158 | if (namecompare(namelen, SYSV_NAMELEN, |
| 159 | name, de->name)) |
| 160 | goto found; |
| 161 | } |
Dan Carpenter | 404e781 | 2010-04-21 12:30:32 +0200 | [diff] [blame] | 162 | dir_put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | if (++n >= npages) |
| 166 | n = 0; |
| 167 | } while (n != start); |
| 168 | |
| 169 | return NULL; |
| 170 | |
| 171 | found: |
| 172 | SYSV_I(dir)->i_dir_start_lookup = n; |
| 173 | *res_page = page; |
| 174 | return de; |
| 175 | } |
| 176 | |
| 177 | int sysv_add_link(struct dentry *dentry, struct inode *inode) |
| 178 | { |
| 179 | struct inode *dir = dentry->d_parent->d_inode; |
| 180 | const char * name = dentry->d_name.name; |
| 181 | int namelen = dentry->d_name.len; |
| 182 | struct page *page = NULL; |
| 183 | struct sysv_dir_entry * de; |
| 184 | unsigned long npages = dir_pages(dir); |
| 185 | unsigned long n; |
| 186 | char *kaddr; |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 187 | loff_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | int err; |
| 189 | |
| 190 | /* We take care of directory expansion in the same loop */ |
| 191 | for (n = 0; n <= npages; n++) { |
| 192 | page = dir_get_page(dir, n); |
| 193 | err = PTR_ERR(page); |
| 194 | if (IS_ERR(page)) |
| 195 | goto out; |
| 196 | kaddr = (char*)page_address(page); |
| 197 | de = (struct sysv_dir_entry *)kaddr; |
| 198 | kaddr += PAGE_CACHE_SIZE - SYSV_DIRSIZE; |
| 199 | while ((char *)de <= kaddr) { |
| 200 | if (!de->inode) |
| 201 | goto got_it; |
| 202 | err = -EEXIST; |
| 203 | if (namecompare(namelen, SYSV_NAMELEN, name, de->name)) |
| 204 | goto out_page; |
| 205 | de++; |
| 206 | } |
| 207 | dir_put_page(page); |
| 208 | } |
| 209 | BUG(); |
| 210 | return -EINVAL; |
| 211 | |
| 212 | got_it: |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 213 | pos = page_offset(page) + |
| 214 | (char*)de - (char*)page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | lock_page(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 216 | err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (err) |
| 218 | goto out_unlock; |
| 219 | memcpy (de->name, name, namelen); |
| 220 | memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2); |
| 221 | 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] | 222 | err = dir_commit_chunk(page, pos, SYSV_DIRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 224 | mark_inode_dirty(dir); |
| 225 | out_page: |
| 226 | dir_put_page(page); |
| 227 | out: |
| 228 | return err; |
| 229 | out_unlock: |
| 230 | unlock_page(page); |
| 231 | goto out_page; |
| 232 | } |
| 233 | |
| 234 | int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page) |
| 235 | { |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 236 | struct inode *inode = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | char *kaddr = (char*)page_address(page); |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 238 | loff_t pos = page_offset(page) + (char *)de - kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | int err; |
| 240 | |
| 241 | lock_page(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 242 | err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE); |
Eric Sesterhenn | d6735bf | 2006-04-02 13:39:21 +0200 | [diff] [blame] | 243 | BUG_ON(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | de->inode = 0; |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 245 | err = dir_commit_chunk(page, pos, SYSV_DIRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | dir_put_page(page); |
| 247 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 248 | mark_inode_dirty(inode); |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | int sysv_make_empty(struct inode *inode, struct inode *dir) |
| 253 | { |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 254 | struct page *page = grab_cache_page(inode->i_mapping, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | struct sysv_dir_entry * de; |
| 256 | char *base; |
| 257 | int err; |
| 258 | |
| 259 | if (!page) |
| 260 | return -ENOMEM; |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 261 | err = sysv_prepare_chunk(page, 0, 2 * SYSV_DIRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (err) { |
| 263 | unlock_page(page); |
| 264 | goto fail; |
| 265 | } |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 266 | kmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | base = (char*)page_address(page); |
| 269 | memset(base, 0, PAGE_CACHE_SIZE); |
| 270 | |
| 271 | de = (struct sysv_dir_entry *) base; |
| 272 | de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino); |
| 273 | strcpy(de->name,"."); |
| 274 | de++; |
| 275 | de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino); |
| 276 | strcpy(de->name,".."); |
| 277 | |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 278 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE); |
| 280 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | page_cache_release(page); |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * routine to check that the specified directory is empty (for rmdir) |
| 287 | */ |
| 288 | int sysv_empty_dir(struct inode * inode) |
| 289 | { |
| 290 | struct super_block *sb = inode->i_sb; |
| 291 | struct page *page = NULL; |
| 292 | unsigned long i, npages = dir_pages(inode); |
| 293 | |
| 294 | for (i = 0; i < npages; i++) { |
| 295 | char *kaddr; |
| 296 | struct sysv_dir_entry * de; |
| 297 | page = dir_get_page(inode, i); |
| 298 | |
| 299 | if (IS_ERR(page)) |
| 300 | continue; |
| 301 | |
| 302 | kaddr = (char *)page_address(page); |
| 303 | de = (struct sysv_dir_entry *)kaddr; |
| 304 | kaddr += PAGE_CACHE_SIZE-SYSV_DIRSIZE; |
| 305 | |
| 306 | for ( ;(char *)de <= kaddr; de++) { |
| 307 | if (!de->inode) |
| 308 | continue; |
| 309 | /* check for . and .. */ |
| 310 | if (de->name[0] != '.') |
| 311 | goto not_empty; |
| 312 | if (!de->name[1]) { |
| 313 | if (de->inode == cpu_to_fs16(SYSV_SB(sb), |
| 314 | inode->i_ino)) |
| 315 | continue; |
| 316 | goto not_empty; |
| 317 | } |
| 318 | if (de->name[1] != '.' || de->name[2]) |
| 319 | goto not_empty; |
| 320 | } |
| 321 | dir_put_page(page); |
| 322 | } |
| 323 | return 1; |
| 324 | |
| 325 | not_empty: |
| 326 | dir_put_page(page); |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* Releases the page */ |
| 331 | void sysv_set_link(struct sysv_dir_entry *de, struct page *page, |
| 332 | struct inode *inode) |
| 333 | { |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 334 | struct inode *dir = page->mapping->host; |
Nick Piggin | 26a6441 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 335 | loff_t pos = page_offset(page) + |
| 336 | (char *)de-(char*)page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | int err; |
| 338 | |
| 339 | lock_page(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 340 | err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE); |
Eric Sesterhenn | d6735bf | 2006-04-02 13:39:21 +0200 | [diff] [blame] | 341 | BUG_ON(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | 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] | 343 | err = dir_commit_chunk(page, pos, SYSV_DIRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | dir_put_page(page); |
| 345 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 346 | mark_inode_dirty(dir); |
| 347 | } |
| 348 | |
| 349 | struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p) |
| 350 | { |
| 351 | struct page *page = dir_get_page(dir, 0); |
| 352 | struct sysv_dir_entry *de = NULL; |
| 353 | |
| 354 | if (!IS_ERR(page)) { |
| 355 | de = (struct sysv_dir_entry*) page_address(page) + 1; |
| 356 | *p = page; |
| 357 | } |
| 358 | return de; |
| 359 | } |
| 360 | |
| 361 | ino_t sysv_inode_by_name(struct dentry *dentry) |
| 362 | { |
| 363 | struct page *page; |
| 364 | struct sysv_dir_entry *de = sysv_find_entry (dentry, &page); |
| 365 | ino_t res = 0; |
| 366 | |
| 367 | if (de) { |
| 368 | res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode); |
| 369 | dir_put_page(page); |
| 370 | } |
| 371 | return res; |
| 372 | } |