blob: b721d0bda5e5d18f6d189c9ee035f9fdd28990ed [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/ufs/ufs_dir.c
4 *
5 * Copyright (C) 1996
6 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
7 * Laboratory for Computer Science Research Computing Facility
8 * Rutgers, The State University of New Jersey
9 *
10 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
11 *
12 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
13 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
14 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070015 *
16 * Migration to usage of "page cache" on May 2006 by
17 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
20#include <linux/time.h>
21#include <linux/fs.h>
Nick Piggin82b9d1d2007-10-16 01:25:19 -070022#include <linux/swap.h>
Jeff Laytonbb8c2d62017-12-11 06:35:18 -050023#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Mike Frysingere5420592008-02-08 04:21:31 -080025#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070026#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "swab.h"
28#include "util.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
32 *
33 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
34 */
35static inline int ufs_match(struct super_block *sb, int len,
Al Viro89031bc2010-01-31 20:49:54 -050036 const unsigned char *name, struct ufs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 if (len != ufs_get_de_namlen(sb, de))
39 return 0;
40 if (!de->d_ino)
41 return 0;
42 return !memcmp(name, de->d_name, len);
43}
44
Nick Piggin82b9d1d2007-10-16 01:25:19 -070045static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Nick Piggin82b9d1d2007-10-16 01:25:19 -070047 struct address_space *mapping = page->mapping;
48 struct inode *dir = mapping->host;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070049 int err = 0;
Nick Piggin82b9d1d2007-10-16 01:25:19 -070050
Jeff Laytonbb8c2d62017-12-11 06:35:18 -050051 inode_inc_iversion(dir);
Nick Piggin82b9d1d2007-10-16 01:25:19 -070052 block_write_end(NULL, mapping, pos, len, len, page, NULL);
53 if (pos+len > dir->i_size) {
54 i_size_write(dir, pos+len);
55 mark_inode_dirty(dir);
56 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070057 if (IS_DIRSYNC(dir))
Jeff Layton2b69c822017-07-05 15:26:48 -040058 err = write_one_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070059 else
60 unlock_page(page);
61 return err;
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070064static inline void ufs_put_page(struct page *page)
65{
66 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030067 put_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070068}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Al Viro89031bc2010-01-31 20:49:54 -050070ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070071{
72 ino_t res = 0;
73 struct ufs_dir_entry *de;
74 struct page *page;
75
Alexey Dobriyan08049702009-12-15 16:46:50 -080076 de = ufs_find_entry(dir, qstr, &page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070077 if (de) {
78 res = fs32_to_cpu(dir->i_sb, de->d_ino);
79 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070081 return res;
82}
83
84
85/* Releases the page */
86void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
Al Viro70d45cd2015-06-16 01:56:23 -040087 struct page *page, struct inode *inode,
88 bool update_times)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070089{
Nick Piggin82b9d1d2007-10-16 01:25:19 -070090 loff_t pos = page_offset(page) +
91 (char *) de - (char *) page_address(page);
92 unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070093 int err;
94
95 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +020096 err = ufs_prepare_chunk(page, pos, len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070097 BUG_ON(err);
Nick Piggin82b9d1d2007-10-16 01:25:19 -070098
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070099 de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
100 ufs_set_de_type(dir->i_sb, de, inode->i_mode);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700101
102 err = ufs_commit_chunk(page, pos, len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700103 ufs_put_page(page);
Al Viro70d45cd2015-06-16 01:56:23 -0400104 if (update_times)
Deepa Dinamani02027d42016-09-14 07:48:05 -0700105 dir->i_mtime = dir->i_ctime = current_time(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700106 mark_inode_dirty(dir);
107}
108
109
Al Virobe5b82d2016-04-22 15:06:44 -0400110static bool ufs_check_page(struct page *page)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700111{
112 struct inode *dir = page->mapping->host;
113 struct super_block *sb = dir->i_sb;
114 char *kaddr = page_address(page);
115 unsigned offs, rec_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300116 unsigned limit = PAGE_SIZE;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800117 const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700118 struct ufs_dir_entry *p;
119 char *error;
120
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300121 if ((dir->i_size >> PAGE_SHIFT) == page->index) {
122 limit = dir->i_size & ~PAGE_MASK;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800123 if (limit & chunk_mask)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700124 goto Ebadsize;
125 if (!limit)
126 goto out;
127 }
128 for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
129 p = (struct ufs_dir_entry *)(kaddr + offs);
130 rec_len = fs16_to_cpu(sb, p->d_reclen);
131
132 if (rec_len < UFS_DIR_REC_LEN(1))
133 goto Eshort;
134 if (rec_len & 3)
135 goto Ealign;
136 if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
137 goto Enamelen;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800138 if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700139 goto Espan;
140 if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
141 UFS_SB(sb)->s_uspi->s_ncg))
142 goto Einumber;
143 }
144 if (offs != limit)
145 goto Eend;
146out:
147 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400148 return true;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700149
150 /* Too bad, we had an error */
151
152Ebadsize:
153 ufs_error(sb, "ufs_check_page",
154 "size of directory #%lu is not a multiple of chunk size",
155 dir->i_ino
156 );
157 goto fail;
158Eshort:
159 error = "rec_len is smaller than minimal";
160 goto bad_entry;
161Ealign:
162 error = "unaligned directory entry";
163 goto bad_entry;
164Enamelen:
165 error = "rec_len is too small for name_len";
166 goto bad_entry;
167Espan:
168 error = "directory entry across blocks";
169 goto bad_entry;
170Einumber:
171 error = "inode out of bounds";
172bad_entry:
173 ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
174 "offset=%lu, rec_len=%d, name_len=%d",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300175 dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700176 rec_len, ufs_get_de_namlen(sb, p));
177 goto fail;
178Eend:
179 p = (struct ufs_dir_entry *)(kaddr + offs);
Harvey Harrison9746077a72008-04-28 02:16:17 -0700180 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700181 "entry in directory #%lu spans the page boundary"
182 "offset=%lu",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300183 dir->i_ino, (page->index<<PAGE_SHIFT)+offs);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700184fail:
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700185 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400186 return false;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700187}
188
189static struct page *ufs_get_page(struct inode *dir, unsigned long n)
190{
191 struct address_space *mapping = dir->i_mapping;
Nick Piggin6fe69002007-05-06 14:49:04 -0700192 struct page *page = read_mapping_page(mapping, n, NULL);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700193 if (!IS_ERR(page)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700194 kmap(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400195 if (unlikely(!PageChecked(page))) {
196 if (PageError(page) || !ufs_check_page(page))
197 goto fail;
198 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700199 }
200 return page;
201
202fail:
203 ufs_put_page(page);
204 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700208 * Return the offset into page `page_nr' of the last valid
209 * byte in that page, plus one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700211static unsigned
212ufs_last_byte(struct inode *inode, unsigned long page_nr)
213{
214 unsigned last_byte = inode->i_size;
215
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 last_byte -= page_nr << PAGE_SHIFT;
217 if (last_byte > PAGE_SIZE)
218 last_byte = PAGE_SIZE;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700219 return last_byte;
220}
221
222static inline struct ufs_dir_entry *
223ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
224{
225 return (struct ufs_dir_entry *)((char *)p +
226 fs16_to_cpu(sb, p->d_reclen));
227}
228
229struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
230{
231 struct page *page = ufs_get_page(dir, 0);
232 struct ufs_dir_entry *de = NULL;
233
234 if (!IS_ERR(page)) {
235 de = ufs_next_entry(dir->i_sb,
236 (struct ufs_dir_entry *)page_address(page));
237 *p = page;
238 }
239 return de;
240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/*
243 * ufs_find_entry()
244 *
245 * finds an entry in the specified directory with the wanted name. It
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700246 * returns the page in which the entry was found, and the entry itself
247 * (as a parameter - res_dir). Page is returned mapped and unlocked.
248 * Entry is guaranteed to be valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Al Viro89031bc2010-01-31 20:49:54 -0500250struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700251 struct page **res_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct super_block *sb = dir->i_sb;
Al Viro89031bc2010-01-31 20:49:54 -0500254 const unsigned char *name = qstr->name;
Alexey Dobriyan08049702009-12-15 16:46:50 -0800255 int namelen = qstr->len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700256 unsigned reclen = UFS_DIR_REC_LEN(namelen);
257 unsigned long start, n;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200258 unsigned long npages = dir_pages(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700259 struct page *page = NULL;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700260 struct ufs_inode_info *ui = UFS_I(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700261 struct ufs_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700263 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700265 if (npages == 0 || namelen > UFS_MAXNAMLEN)
266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700268 /* OFFSET_CACHE */
269 *res_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700271 start = ui->i_dir_start_lookup;
272
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700273 if (start >= npages)
274 start = 0;
275 n = start;
276 do {
277 char *kaddr;
278 page = ufs_get_page(dir, n);
279 if (!IS_ERR(page)) {
280 kaddr = page_address(page);
281 de = (struct ufs_dir_entry *) kaddr;
282 kaddr += ufs_last_byte(dir, n) - reclen;
283 while ((char *) de <= kaddr) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700284 if (ufs_match(sb, namelen, name, de))
285 goto found;
286 de = ufs_next_entry(sb, de);
287 }
288 ufs_put_page(page);
289 }
290 if (++n >= npages)
291 n = 0;
292 } while (n != start);
293out:
294 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700296found:
297 *res_page = page;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700298 ui->i_dir_start_lookup = n;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700299 return de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700303 * Parent is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305int ufs_add_link(struct dentry *dentry, struct inode *inode)
306{
David Howells2b0143b2015-03-17 22:25:59 +0000307 struct inode *dir = d_inode(dentry->d_parent);
Al Viro89031bc2010-01-31 20:49:54 -0500308 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 int namelen = dentry->d_name.len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700310 struct super_block *sb = dir->i_sb;
311 unsigned reclen = UFS_DIR_REC_LEN(namelen);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800312 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700313 unsigned short rec_len, name_len;
314 struct page *page = NULL;
315 struct ufs_dir_entry *de;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200316 unsigned long npages = dir_pages(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700317 unsigned long n;
318 char *kaddr;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700319 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int err;
321
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700322 UFSD("ENTER, name %s, namelen %u\n", name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700324 /*
325 * We take care of directory expansion in the same loop.
326 * This code plays outside i_size, so it locks the page
327 * to protect that region.
328 */
329 for (n = 0; n <= npages; n++) {
330 char *dir_end;
331
332 page = ufs_get_page(dir, n);
333 err = PTR_ERR(page);
334 if (IS_ERR(page))
335 goto out;
336 lock_page(page);
337 kaddr = page_address(page);
338 dir_end = kaddr + ufs_last_byte(dir, n);
339 de = (struct ufs_dir_entry *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300340 kaddr += PAGE_SIZE - reclen;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700341 while ((char *)de <= kaddr) {
342 if ((char *)de == dir_end) {
343 /* We hit i_size */
344 name_len = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800345 rec_len = chunk_size;
346 de->d_reclen = cpu_to_fs16(sb, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700347 de->d_ino = 0;
348 goto got_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700350 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700351 ufs_error(dir->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700352 "zero-length directory entry");
353 err = -EIO;
354 goto out_unlock;
355 }
356 err = -EEXIST;
357 if (ufs_match(sb, namelen, name, de))
358 goto out_unlock;
359 name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
360 rec_len = fs16_to_cpu(sb, de->d_reclen);
361 if (!de->d_ino && rec_len >= reclen)
362 goto got_it;
363 if (rec_len >= name_len + reclen)
364 goto got_it;
365 de = (struct ufs_dir_entry *) ((char *) de + rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700367 unlock_page(page);
368 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700370 BUG();
371 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700373got_it:
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700374 pos = page_offset(page) +
375 (char*)de - (char*)page_address(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200376 err = ufs_prepare_chunk(page, pos, rec_len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700377 if (err)
378 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (de->d_ino) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700380 struct ufs_dir_entry *de1 =
381 (struct ufs_dir_entry *) ((char *) de + name_len);
382 de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
383 de->d_reclen = cpu_to_fs16(sb, name_len);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 de = de1;
386 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 ufs_set_de_namlen(sb, de, namelen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700389 memcpy(de->d_name, name, namelen + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
391 ufs_set_de_type(sb, de, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700393 err = ufs_commit_chunk(page, pos, rec_len);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700394 dir->i_mtime = dir->i_ctime = current_time(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700395
396 mark_inode_dirty(dir);
397 /* OFFSET_CACHE */
398out_put:
399 ufs_put_page(page);
400out:
401 return err;
402out_unlock:
403 unlock_page(page);
404 goto out_put;
405}
406
407static inline unsigned
408ufs_validate_entry(struct super_block *sb, char *base,
409 unsigned offset, unsigned mask)
410{
411 struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
412 struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
Al Viroe0d508f2016-05-27 21:22:04 -0400413 while ((char*)p < (char*)de)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700414 p = ufs_next_entry(sb, p);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700415 return (char *)p - base;
416}
417
418
419/*
420 * This is blatantly stolen from ext2fs
421 */
422static int
Al Viro80886292013-05-15 18:51:49 -0400423ufs_readdir(struct file *file, struct dir_context *ctx)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700424{
Al Viro80886292013-05-15 18:51:49 -0400425 loff_t pos = ctx->pos;
426 struct inode *inode = file_inode(file);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700427 struct super_block *sb = inode->i_sb;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300428 unsigned int offset = pos & ~PAGE_MASK;
429 unsigned long n = pos >> PAGE_SHIFT;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200430 unsigned long npages = dir_pages(inode);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800431 unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500432 bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700433 unsigned flags = UFS_SB(sb)->s_flags;
434
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700435 UFSD("BEGIN\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700436
437 if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
438 return 0;
439
440 for ( ; n < npages; n++, offset = 0) {
441 char *kaddr, *limit;
442 struct ufs_dir_entry *de;
443
444 struct page *page = ufs_get_page(inode, n);
445
446 if (IS_ERR(page)) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700447 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700448 "bad page in #%lu",
449 inode->i_ino);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300450 ctx->pos += PAGE_SIZE - offset;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700451 return -EIO;
452 }
453 kaddr = page_address(page);
454 if (unlikely(need_revalidate)) {
455 if (offset) {
456 offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300457 ctx->pos = (n<<PAGE_SHIFT) + offset;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700458 }
Jeff Laytonbb8c2d62017-12-11 06:35:18 -0500459 file->f_version = inode_query_iversion(inode);
460 need_revalidate = false;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700461 }
462 de = (struct ufs_dir_entry *)(kaddr+offset);
463 limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
464 for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700465 if (de->d_ino) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700466 unsigned char d_type = DT_UNKNOWN;
467
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700468 UFSD("filldir(%s,%u)\n", de->d_name,
469 fs32_to_cpu(sb, de->d_ino));
470 UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700471
472 if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
473 d_type = de->d_u.d_44.d_type;
474
Al Viro80886292013-05-15 18:51:49 -0400475 if (!dir_emit(ctx, de->d_name,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700476 ufs_get_de_namlen(sb, de),
Al Viro80886292013-05-15 18:51:49 -0400477 fs32_to_cpu(sb, de->d_ino),
478 d_type)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700479 ufs_put_page(page);
480 return 0;
481 }
482 }
Al Viro80886292013-05-15 18:51:49 -0400483 ctx->pos += fs16_to_cpu(sb, de->d_reclen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700484 }
485 ufs_put_page(page);
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488}
489
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
492 * ufs_delete_entry deletes a directory entry by merging it with the
493 * previous entry.
494 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700495int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
496 struct page * page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700498 struct super_block *sb = inode->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700499 char *kaddr = page_address(page);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800500 unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700501 unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700502 loff_t pos;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700503 struct ufs_dir_entry *pde = NULL;
504 struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
505 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700507 UFSD("ENTER\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700508
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700509 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700510 fs32_to_cpu(sb, de->d_ino),
511 fs16_to_cpu(sb, de->d_reclen),
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700512 ufs_get_de_namlen(sb, de), de->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700514 while ((char*)de < (char*)dir) {
515 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700516 ufs_error(inode->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700517 "zero-length directory entry");
518 err = -EIO;
519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700521 pde = de;
522 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700524 if (pde)
525 from = (char*)pde - (char*)page_address(page);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700526
527 pos = page_offset(page) + from;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700528 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200529 err = ufs_prepare_chunk(page, pos, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700530 BUG_ON(err);
531 if (pde)
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700532 pde->d_reclen = cpu_to_fs16(sb, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700533 dir->d_ino = 0;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700534 err = ufs_commit_chunk(page, pos, to - from);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700535 inode->i_ctime = inode->i_mtime = current_time(inode);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700536 mark_inode_dirty(inode);
537out:
538 ufs_put_page(page);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700539 UFSD("EXIT\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543int ufs_make_empty(struct inode * inode, struct inode *dir)
544{
545 struct super_block * sb = dir->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700546 struct address_space *mapping = inode->i_mapping;
547 struct page *page = grab_cache_page(mapping, 0);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800548 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct ufs_dir_entry * de;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700550 char *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int err;
552
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700553 if (!page)
554 return -ENOMEM;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700555
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200556 err = ufs_prepare_chunk(page, 0, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700557 if (err) {
558 unlock_page(page);
559 goto fail;
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700562 kmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700563 base = (char*)page_address(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300564 memset(base, 0, PAGE_SIZE);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700565
566 de = (struct ufs_dir_entry *) base;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
569 ufs_set_de_type(sb, de, inode->i_mode);
570 ufs_set_de_namlen(sb, de, 1);
571 de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
572 strcpy (de->d_name, ".");
573 de = (struct ufs_dir_entry *)
574 ((char *)de + fs16_to_cpu(sb, de->d_reclen));
575 de->d_ino = cpu_to_fs32(sb, dir->i_ino);
576 ufs_set_de_type(sb, de, dir->i_mode);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800577 de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 ufs_set_de_namlen(sb, de, 2);
579 strcpy (de->d_name, "..");
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700580 kunmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700581
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800582 err = ufs_commit_chunk(page, 0, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700583fail:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300584 put_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700585 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588/*
589 * routine to check that the specified directory is empty (for rmdir)
590 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700591int ufs_empty_dir(struct inode * inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700593 struct super_block *sb = inode->i_sb;
594 struct page *page = NULL;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200595 unsigned long i, npages = dir_pages(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700597 for (i = 0; i < npages; i++) {
598 char *kaddr;
599 struct ufs_dir_entry *de;
600 page = ufs_get_page(inode, i);
601
602 if (IS_ERR(page))
603 continue;
604
605 kaddr = page_address(page);
606 de = (struct ufs_dir_entry *)kaddr;
607 kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
608
609 while ((char *)de <= kaddr) {
610 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700611 ufs_error(inode->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700612 "zero-length directory entry: "
613 "kaddr=%p, de=%p\n", kaddr, de);
614 goto not_empty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700616 if (de->d_ino) {
617 u16 namelen=ufs_get_de_namlen(sb, de);
618 /* check for . and .. */
619 if (de->d_name[0] != '.')
620 goto not_empty;
621 if (namelen > 2)
622 goto not_empty;
623 if (namelen < 2) {
624 if (inode->i_ino !=
625 fs32_to_cpu(sb, de->d_ino))
626 goto not_empty;
627 } else if (de->d_name[1] != '.')
628 goto not_empty;
629 }
630 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700632 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700635
636not_empty:
637 ufs_put_page(page);
638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800641const struct file_operations ufs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -0400643 .iterate_shared = ufs_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200644 .fsync = generic_file_fsync,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200645 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646};