Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/minix/dir.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * minix directory handling functions |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 7 | * |
| 8 | * Updated to filesystem version 3 by Daniel Aragones |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "minix.h" |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 12 | #include <linux/buffer_head.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/highmem.h> |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 14 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | typedef struct minix_dir_entry minix_dirent; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 17 | typedef struct minix3_dir_entry minix3_dirent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 19 | static int minix_readdir(struct file *, struct dir_context *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 21 | const struct file_operations minix_dir_operations = { |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 22 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | .read = generic_read_dir, |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 24 | .iterate = minix_readdir, |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 25 | .fsync = generic_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static inline void dir_put_page(struct page *page) |
| 29 | { |
| 30 | kunmap(page); |
| 31 | page_cache_release(page); |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * Return the offset into page `page_nr' of the last valid |
| 36 | * byte in that page, plus one. |
| 37 | */ |
| 38 | static unsigned |
| 39 | minix_last_byte(struct inode *inode, unsigned long page_nr) |
| 40 | { |
| 41 | unsigned last_byte = PAGE_CACHE_SIZE; |
| 42 | |
| 43 | if (page_nr == (inode->i_size >> PAGE_CACHE_SHIFT)) |
| 44 | last_byte = inode->i_size & (PAGE_CACHE_SIZE - 1); |
| 45 | return last_byte; |
| 46 | } |
| 47 | |
| 48 | static inline unsigned long dir_pages(struct inode *inode) |
| 49 | { |
| 50 | return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT; |
| 51 | } |
| 52 | |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 53 | 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] | 54 | { |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 55 | struct address_space *mapping = page->mapping; |
| 56 | struct inode *dir = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | int err = 0; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 58 | block_write_end(NULL, mapping, pos, len, len, page, NULL); |
| 59 | |
| 60 | if (pos+len > dir->i_size) { |
| 61 | i_size_write(dir, pos+len); |
| 62 | mark_inode_dirty(dir); |
| 63 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | if (IS_DIRSYNC(dir)) |
| 65 | err = write_one_page(page, 1); |
| 66 | else |
| 67 | unlock_page(page); |
| 68 | return err; |
| 69 | } |
| 70 | |
| 71 | static struct page * dir_get_page(struct inode *dir, unsigned long n) |
| 72 | { |
| 73 | struct address_space *mapping = dir->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 74 | struct page *page = read_mapping_page(mapping, n, NULL); |
Al Viro | 49837a8 | 2010-05-28 11:34:50 -0400 | [diff] [blame] | 75 | if (!IS_ERR(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | kmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi) |
| 81 | { |
| 82 | return (void*)((char*)de + sbi->s_dirsize); |
| 83 | } |
| 84 | |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 85 | static int minix_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 87 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | struct super_block *sb = inode->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | struct minix_sb_info *sbi = minix_sb(sb); |
| 90 | unsigned chunk_size = sbi->s_dirsize; |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 91 | unsigned long npages = dir_pages(inode); |
| 92 | unsigned long pos = ctx->pos; |
| 93 | unsigned offset; |
| 94 | unsigned long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Dan Carpenter | 642b704 | 2013-06-19 10:08:10 +1000 | [diff] [blame] | 96 | ctx->pos = pos = ALIGN(pos, chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (pos >= inode->i_size) |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 98 | return 0; |
| 99 | |
| 100 | offset = pos & ~PAGE_CACHE_MASK; |
| 101 | n = pos >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | for ( ; n < npages; n++, offset = 0) { |
| 104 | char *p, *kaddr, *limit; |
| 105 | struct page *page = dir_get_page(inode, n); |
| 106 | |
| 107 | if (IS_ERR(page)) |
| 108 | continue; |
| 109 | kaddr = (char *)page_address(page); |
| 110 | p = kaddr+offset; |
| 111 | limit = kaddr + minix_last_byte(inode, n) - chunk_size; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 112 | for ( ; p <= limit; p = minix_next_entry(p, sbi)) { |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 113 | const char *name; |
| 114 | __u32 inumber; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 115 | if (sbi->s_version == MINIX_V3) { |
| 116 | minix3_dirent *de3 = (minix3_dirent *)p; |
| 117 | name = de3->name; |
| 118 | inumber = de3->inode; |
| 119 | } else { |
| 120 | minix_dirent *de = (minix_dirent *)p; |
| 121 | name = de->name; |
| 122 | inumber = de->inode; |
| 123 | } |
| 124 | if (inumber) { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 125 | unsigned l = strnlen(name, sbi->s_namelen); |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 126 | if (!dir_emit(ctx, name, l, |
| 127 | inumber, DT_UNKNOWN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | dir_put_page(page); |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 132 | ctx->pos += chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | dir_put_page(page); |
| 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static inline int namecompare(int len, int maxlen, |
| 140 | const char * name, const char * buffer) |
| 141 | { |
| 142 | if (len < maxlen && buffer[len]) |
| 143 | return 0; |
| 144 | return !memcmp(name, buffer, len); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * minix_find_entry() |
| 149 | * |
| 150 | * finds an entry in the specified directory with the wanted name. It |
| 151 | * returns the cache buffer in which the entry was found, and the entry |
| 152 | * itself (as a parameter - res_dir). It does NOT read the inode of the |
| 153 | * entry - you'll have to do that yourself if you want to. |
| 154 | */ |
| 155 | minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page) |
| 156 | { |
| 157 | const char * name = dentry->d_name.name; |
| 158 | int namelen = dentry->d_name.len; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 159 | struct inode * dir = d_inode(dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | struct super_block * sb = dir->i_sb; |
| 161 | struct minix_sb_info * sbi = minix_sb(sb); |
| 162 | unsigned long n; |
| 163 | unsigned long npages = dir_pages(dir); |
| 164 | struct page *page = NULL; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 165 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 167 | char *namx; |
| 168 | __u32 inumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | *res_page = NULL; |
| 170 | |
| 171 | for (n = 0; n < npages; n++) { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 172 | char *kaddr, *limit; |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | page = dir_get_page(dir, n); |
| 175 | if (IS_ERR(page)) |
| 176 | continue; |
| 177 | |
| 178 | kaddr = (char*)page_address(page); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 179 | limit = kaddr + minix_last_byte(dir, n) - sbi->s_dirsize; |
| 180 | for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) { |
| 181 | if (sbi->s_version == MINIX_V3) { |
| 182 | minix3_dirent *de3 = (minix3_dirent *)p; |
| 183 | namx = de3->name; |
| 184 | inumber = de3->inode; |
| 185 | } else { |
| 186 | minix_dirent *de = (minix_dirent *)p; |
| 187 | namx = de->name; |
| 188 | inumber = de->inode; |
| 189 | } |
| 190 | if (!inumber) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | continue; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 192 | if (namecompare(namelen, sbi->s_namelen, name, namx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | goto found; |
| 194 | } |
| 195 | dir_put_page(page); |
| 196 | } |
| 197 | return NULL; |
| 198 | |
| 199 | found: |
| 200 | *res_page = page; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 201 | return (minix_dirent *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int minix_add_link(struct dentry *dentry, struct inode *inode) |
| 205 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 206 | struct inode *dir = d_inode(dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | const char * name = dentry->d_name.name; |
| 208 | int namelen = dentry->d_name.len; |
| 209 | struct super_block * sb = dir->i_sb; |
| 210 | struct minix_sb_info * sbi = minix_sb(sb); |
| 211 | struct page *page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | unsigned long npages = dir_pages(dir); |
| 213 | unsigned long n; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 214 | char *kaddr, *p; |
| 215 | minix_dirent *de; |
| 216 | minix3_dirent *de3; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 217 | loff_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | int err; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 219 | char *namx = NULL; |
| 220 | __u32 inumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * We take care of directory expansion in the same loop |
| 224 | * This code plays outside i_size, so it locks the page |
| 225 | * to protect that region. |
| 226 | */ |
| 227 | for (n = 0; n <= npages; n++) { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 228 | char *limit, *dir_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | page = dir_get_page(dir, n); |
| 231 | err = PTR_ERR(page); |
| 232 | if (IS_ERR(page)) |
| 233 | goto out; |
| 234 | lock_page(page); |
| 235 | kaddr = (char*)page_address(page); |
| 236 | dir_end = kaddr + minix_last_byte(dir, n); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 237 | limit = kaddr + PAGE_CACHE_SIZE - sbi->s_dirsize; |
| 238 | for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) { |
| 239 | de = (minix_dirent *)p; |
| 240 | de3 = (minix3_dirent *)p; |
| 241 | if (sbi->s_version == MINIX_V3) { |
| 242 | namx = de3->name; |
| 243 | inumber = de3->inode; |
| 244 | } else { |
| 245 | namx = de->name; |
| 246 | inumber = de->inode; |
| 247 | } |
| 248 | if (p == dir_end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | /* We hit i_size */ |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 250 | if (sbi->s_version == MINIX_V3) |
| 251 | de3->inode = 0; |
| 252 | else |
| 253 | de->inode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | goto got_it; |
| 255 | } |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 256 | if (!inumber) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | goto got_it; |
| 258 | err = -EEXIST; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 259 | if (namecompare(namelen, sbi->s_namelen, name, namx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | unlock_page(page); |
| 263 | dir_put_page(page); |
| 264 | } |
| 265 | BUG(); |
| 266 | return -EINVAL; |
| 267 | |
| 268 | got_it: |
Evgeniy Dushistov | d6b5484 | 2009-01-06 14:42:38 -0800 | [diff] [blame] | 269 | pos = page_offset(page) + p - (char *)page_address(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 270 | err = minix_prepare_chunk(page, pos, sbi->s_dirsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (err) |
| 272 | goto out_unlock; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 273 | memcpy (namx, name, namelen); |
| 274 | if (sbi->s_version == MINIX_V3) { |
| 275 | memset (namx + namelen, 0, sbi->s_dirsize - namelen - 4); |
| 276 | de3->inode = inode->i_ino; |
| 277 | } else { |
| 278 | memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2); |
| 279 | de->inode = inode->i_ino; |
| 280 | } |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 281 | err = dir_commit_chunk(page, pos, sbi->s_dirsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 283 | mark_inode_dirty(dir); |
| 284 | out_put: |
| 285 | dir_put_page(page); |
| 286 | out: |
| 287 | return err; |
| 288 | out_unlock: |
| 289 | unlock_page(page); |
| 290 | goto out_put; |
| 291 | } |
| 292 | |
| 293 | int minix_delete_entry(struct minix_dir_entry *de, struct page *page) |
| 294 | { |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 295 | struct inode *inode = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | char *kaddr = page_address(page); |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 297 | loff_t pos = page_offset(page) + (char*)de - kaddr; |
Doug Graham | 9f6c133 | 2009-09-22 16:48:00 -0700 | [diff] [blame] | 298 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
| 299 | unsigned len = sbi->s_dirsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | int err; |
| 301 | |
| 302 | lock_page(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 303 | err = minix_prepare_chunk(page, pos, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | if (err == 0) { |
Doug Graham | 9f6c133 | 2009-09-22 16:48:00 -0700 | [diff] [blame] | 305 | if (sbi->s_version == MINIX_V3) |
| 306 | ((minix3_dirent *) de)->inode = 0; |
| 307 | else |
| 308 | de->inode = 0; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 309 | err = dir_commit_chunk(page, pos, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } else { |
| 311 | unlock_page(page); |
| 312 | } |
| 313 | dir_put_page(page); |
| 314 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 315 | mark_inode_dirty(inode); |
| 316 | return err; |
| 317 | } |
| 318 | |
| 319 | int minix_make_empty(struct inode *inode, struct inode *dir) |
| 320 | { |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 321 | struct page *page = grab_cache_page(inode->i_mapping, 0); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 322 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | char *kaddr; |
| 324 | int err; |
| 325 | |
| 326 | if (!page) |
| 327 | return -ENOMEM; |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 328 | err = minix_prepare_chunk(page, 0, 2 * sbi->s_dirsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (err) { |
| 330 | unlock_page(page); |
| 331 | goto fail; |
| 332 | } |
| 333 | |
Cong Wang | 27a6d5c | 2011-11-25 23:14:32 +0800 | [diff] [blame] | 334 | kaddr = kmap_atomic(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | memset(kaddr, 0, PAGE_CACHE_SIZE); |
| 336 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 337 | if (sbi->s_version == MINIX_V3) { |
| 338 | minix3_dirent *de3 = (minix3_dirent *)kaddr; |
| 339 | |
| 340 | de3->inode = inode->i_ino; |
| 341 | strcpy(de3->name, "."); |
| 342 | de3 = minix_next_entry(de3, sbi); |
| 343 | de3->inode = dir->i_ino; |
| 344 | strcpy(de3->name, ".."); |
| 345 | } else { |
| 346 | minix_dirent *de = (minix_dirent *)kaddr; |
| 347 | |
| 348 | de->inode = inode->i_ino; |
| 349 | strcpy(de->name, "."); |
| 350 | de = minix_next_entry(de, sbi); |
| 351 | de->inode = dir->i_ino; |
| 352 | strcpy(de->name, ".."); |
| 353 | } |
Cong Wang | 27a6d5c | 2011-11-25 23:14:32 +0800 | [diff] [blame] | 354 | kunmap_atomic(kaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize); |
| 357 | fail: |
| 358 | page_cache_release(page); |
| 359 | return err; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * routine to check that the specified directory is empty (for rmdir) |
| 364 | */ |
| 365 | int minix_empty_dir(struct inode * inode) |
| 366 | { |
| 367 | struct page *page = NULL; |
| 368 | unsigned long i, npages = dir_pages(inode); |
| 369 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 370 | char *name; |
| 371 | __u32 inumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | for (i = 0; i < npages; i++) { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 374 | char *p, *kaddr, *limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 376 | page = dir_get_page(inode, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | if (IS_ERR(page)) |
| 378 | continue; |
| 379 | |
| 380 | kaddr = (char *)page_address(page); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 381 | limit = kaddr + minix_last_byte(inode, i) - sbi->s_dirsize; |
| 382 | for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) { |
| 383 | if (sbi->s_version == MINIX_V3) { |
| 384 | minix3_dirent *de3 = (minix3_dirent *)p; |
| 385 | name = de3->name; |
| 386 | inumber = de3->inode; |
| 387 | } else { |
| 388 | minix_dirent *de = (minix_dirent *)p; |
| 389 | name = de->name; |
| 390 | inumber = de->inode; |
| 391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 393 | if (inumber != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | /* check for . and .. */ |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 395 | if (name[0] != '.') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | goto not_empty; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 397 | if (!name[1]) { |
| 398 | if (inumber != inode->i_ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | goto not_empty; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 400 | } else if (name[1] != '.') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | goto not_empty; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 402 | else if (name[2]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | goto not_empty; |
| 404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | dir_put_page(page); |
| 407 | } |
| 408 | return 1; |
| 409 | |
| 410 | not_empty: |
| 411 | dir_put_page(page); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* Releases the page */ |
| 416 | void minix_set_link(struct minix_dir_entry *de, struct page *page, |
| 417 | struct inode *inode) |
| 418 | { |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 419 | struct inode *dir = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | struct minix_sb_info *sbi = minix_sb(dir->i_sb); |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 421 | loff_t pos = page_offset(page) + |
| 422 | (char *)de-(char*)page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | int err; |
| 424 | |
| 425 | lock_page(page); |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 426 | |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 427 | err = minix_prepare_chunk(page, pos, sbi->s_dirsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | if (err == 0) { |
Doug Graham | 9f6c133 | 2009-09-22 16:48:00 -0700 | [diff] [blame] | 429 | if (sbi->s_version == MINIX_V3) |
| 430 | ((minix3_dirent *) de)->inode = inode->i_ino; |
| 431 | else |
| 432 | de->inode = inode->i_ino; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 433 | err = dir_commit_chunk(page, pos, sbi->s_dirsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } else { |
| 435 | unlock_page(page); |
| 436 | } |
| 437 | dir_put_page(page); |
| 438 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 439 | mark_inode_dirty(dir); |
| 440 | } |
| 441 | |
| 442 | struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p) |
| 443 | { |
| 444 | struct page *page = dir_get_page(dir, 0); |
| 445 | struct minix_sb_info *sbi = minix_sb(dir->i_sb); |
| 446 | struct minix_dir_entry *de = NULL; |
| 447 | |
| 448 | if (!IS_ERR(page)) { |
| 449 | de = minix_next_entry(page_address(page), sbi); |
| 450 | *p = page; |
| 451 | } |
| 452 | return de; |
| 453 | } |
| 454 | |
| 455 | ino_t minix_inode_by_name(struct dentry *dentry) |
| 456 | { |
| 457 | struct page *page; |
| 458 | struct minix_dir_entry *de = minix_find_entry(dentry, &page); |
| 459 | ino_t res = 0; |
| 460 | |
| 461 | if (de) { |
Doug Graham | 9f6c133 | 2009-09-22 16:48:00 -0700 | [diff] [blame] | 462 | struct address_space *mapping = page->mapping; |
| 463 | struct inode *inode = mapping->host; |
| 464 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
| 465 | |
| 466 | if (sbi->s_version == MINIX_V3) |
| 467 | res = ((minix3_dirent *) de)->inode; |
| 468 | else |
| 469 | res = de->inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | dir_put_page(page); |
| 471 | } |
| 472 | return res; |
| 473 | } |