blob: 48609f1d95803742656b74933fb6490548144354 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Dushistovb71034e2006-06-25 05:47:22 -070014 *
15 * Migration to usage of "page cache" on May 2006 by
16 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/time.h>
20#include <linux/fs.h>
Nick Piggin82b9d1d2007-10-16 01:25:19 -070021#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Mike Frysingere5420592008-02-08 04:21:31 -080023#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070024#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "swab.h"
26#include "util.h"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
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 */
33static inline int ufs_match(struct super_block *sb, int len,
Al Viro89031bc2010-01-31 20:49:54 -050034 const unsigned char *name, struct ufs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
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 Piggin82b9d1d2007-10-16 01:25:19 -070043static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Nick Piggin82b9d1d2007-10-16 01:25:19 -070045 struct address_space *mapping = page->mapping;
46 struct inode *dir = mapping->host;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070047 int err = 0;
Nick Piggin82b9d1d2007-10-16 01:25:19 -070048
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070049 dir->i_version++;
Nick Piggin82b9d1d2007-10-16 01:25:19 -070050 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 Dushistovb71034e2006-06-25 05:47:22 -070055 if (IS_DIRSYNC(dir))
Jeff Layton2b69c822017-07-05 15:26:48 -040056 err = write_one_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070057 else
58 unlock_page(page);
59 return err;
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070062static inline void ufs_put_page(struct page *page)
63{
64 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030065 put_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070066}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Al Viro89031bc2010-01-31 20:49:54 -050068ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070069{
70 ino_t res = 0;
71 struct ufs_dir_entry *de;
72 struct page *page;
73
Alexey Dobriyan08049702009-12-15 16:46:50 -080074 de = ufs_find_entry(dir, qstr, &page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070075 if (de) {
76 res = fs32_to_cpu(dir->i_sb, de->d_ino);
77 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070079 return res;
80}
81
82
83/* Releases the page */
84void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
Al Viro70d45cd2015-06-16 01:56:23 -040085 struct page *page, struct inode *inode,
86 bool update_times)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070087{
Nick Piggin82b9d1d2007-10-16 01:25:19 -070088 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 Dushistovb71034e2006-06-25 05:47:22 -070091 int err;
92
93 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +020094 err = ufs_prepare_chunk(page, pos, len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070095 BUG_ON(err);
Nick Piggin82b9d1d2007-10-16 01:25:19 -070096
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070097 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 Piggin82b9d1d2007-10-16 01:25:19 -070099
100 err = ufs_commit_chunk(page, pos, len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700101 ufs_put_page(page);
Al Viro70d45cd2015-06-16 01:56:23 -0400102 if (update_times)
Deepa Dinamani02027d42016-09-14 07:48:05 -0700103 dir->i_mtime = dir->i_ctime = current_time(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700104 mark_inode_dirty(dir);
105}
106
107
Al Virobe5b82d2016-04-22 15:06:44 -0400108static bool ufs_check_page(struct page *page)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700109{
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;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300114 unsigned limit = PAGE_SIZE;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800115 const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700116 struct ufs_dir_entry *p;
117 char *error;
118
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300119 if ((dir->i_size >> PAGE_SHIFT) == page->index) {
120 limit = dir->i_size & ~PAGE_MASK;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800121 if (limit & chunk_mask)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700122 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 Dushistovf3369532007-02-08 14:20:25 -0800136 if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700137 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;
144out:
145 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400146 return true;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700147
148 /* Too bad, we had an error */
149
150Ebadsize:
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;
156Eshort:
157 error = "rec_len is smaller than minimal";
158 goto bad_entry;
159Ealign:
160 error = "unaligned directory entry";
161 goto bad_entry;
162Enamelen:
163 error = "rec_len is too small for name_len";
164 goto bad_entry;
165Espan:
166 error = "directory entry across blocks";
167 goto bad_entry;
168Einumber:
169 error = "inode out of bounds";
170bad_entry:
171 ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
172 "offset=%lu, rec_len=%d, name_len=%d",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300173 dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700174 rec_len, ufs_get_de_namlen(sb, p));
175 goto fail;
176Eend:
177 p = (struct ufs_dir_entry *)(kaddr + offs);
Harvey Harrison9746077a72008-04-28 02:16:17 -0700178 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700179 "entry in directory #%lu spans the page boundary"
180 "offset=%lu",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300181 dir->i_ino, (page->index<<PAGE_SHIFT)+offs);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700182fail:
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700183 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400184 return false;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700185}
186
187static struct page *ufs_get_page(struct inode *dir, unsigned long n)
188{
189 struct address_space *mapping = dir->i_mapping;
Nick Piggin6fe69002007-05-06 14:49:04 -0700190 struct page *page = read_mapping_page(mapping, n, NULL);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700191 if (!IS_ERR(page)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700192 kmap(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400193 if (unlikely(!PageChecked(page))) {
194 if (PageError(page) || !ufs_check_page(page))
195 goto fail;
196 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700197 }
198 return page;
199
200fail:
201 ufs_put_page(page);
202 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700206 * Return the offset into page `page_nr' of the last valid
207 * byte in that page, plus one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700209static unsigned
210ufs_last_byte(struct inode *inode, unsigned long page_nr)
211{
212 unsigned last_byte = inode->i_size;
213
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300214 last_byte -= page_nr << PAGE_SHIFT;
215 if (last_byte > PAGE_SIZE)
216 last_byte = PAGE_SIZE;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700217 return last_byte;
218}
219
220static inline struct ufs_dir_entry *
221ufs_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
227struct 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 Torvalds1da177e2005-04-16 15:20:36 -0700239
240/*
241 * ufs_find_entry()
242 *
243 * finds an entry in the specified directory with the wanted name. It
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700244 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700247 */
Al Viro89031bc2010-01-31 20:49:54 -0500248struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700249 struct page **res_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct super_block *sb = dir->i_sb;
Al Viro89031bc2010-01-31 20:49:54 -0500252 const unsigned char *name = qstr->name;
Alexey Dobriyan08049702009-12-15 16:46:50 -0800253 int namelen = qstr->len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700254 unsigned reclen = UFS_DIR_REC_LEN(namelen);
255 unsigned long start, n;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200256 unsigned long npages = dir_pages(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700257 struct page *page = NULL;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700258 struct ufs_inode_info *ui = UFS_I(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700259 struct ufs_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700261 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700263 if (npages == 0 || namelen > UFS_MAXNAMLEN)
264 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700266 /* OFFSET_CACHE */
267 *res_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700269 start = ui->i_dir_start_lookup;
270
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700271 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) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700282 if (ufs_match(sb, namelen, name, de))
283 goto found;
284 de = ufs_next_entry(sb, de);
285 }
286 ufs_put_page(page);
287 }
288 if (++n >= npages)
289 n = 0;
290 } while (n != start);
291out:
292 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700294found:
295 *res_page = page;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700296 ui->i_dir_start_lookup = n;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700297 return de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700301 * Parent is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303int ufs_add_link(struct dentry *dentry, struct inode *inode)
304{
David Howells2b0143b2015-03-17 22:25:59 +0000305 struct inode *dir = d_inode(dentry->d_parent);
Al Viro89031bc2010-01-31 20:49:54 -0500306 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int namelen = dentry->d_name.len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700308 struct super_block *sb = dir->i_sb;
309 unsigned reclen = UFS_DIR_REC_LEN(namelen);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800310 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700311 unsigned short rec_len, name_len;
312 struct page *page = NULL;
313 struct ufs_dir_entry *de;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200314 unsigned long npages = dir_pages(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700315 unsigned long n;
316 char *kaddr;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700317 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int err;
319
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700320 UFSD("ENTER, name %s, namelen %u\n", name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700322 /*
323 * We take care of directory expansion in the same loop.
324 * This code plays outside i_size, so it locks the page
325 * to protect that region.
326 */
327 for (n = 0; n <= npages; n++) {
328 char *dir_end;
329
330 page = ufs_get_page(dir, n);
331 err = PTR_ERR(page);
332 if (IS_ERR(page))
333 goto out;
334 lock_page(page);
335 kaddr = page_address(page);
336 dir_end = kaddr + ufs_last_byte(dir, n);
337 de = (struct ufs_dir_entry *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300338 kaddr += PAGE_SIZE - reclen;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700339 while ((char *)de <= kaddr) {
340 if ((char *)de == dir_end) {
341 /* We hit i_size */
342 name_len = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800343 rec_len = chunk_size;
344 de->d_reclen = cpu_to_fs16(sb, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700345 de->d_ino = 0;
346 goto got_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700348 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700349 ufs_error(dir->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700350 "zero-length directory entry");
351 err = -EIO;
352 goto out_unlock;
353 }
354 err = -EEXIST;
355 if (ufs_match(sb, namelen, name, de))
356 goto out_unlock;
357 name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
358 rec_len = fs16_to_cpu(sb, de->d_reclen);
359 if (!de->d_ino && rec_len >= reclen)
360 goto got_it;
361 if (rec_len >= name_len + reclen)
362 goto got_it;
363 de = (struct ufs_dir_entry *) ((char *) de + rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700365 unlock_page(page);
366 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700368 BUG();
369 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700371got_it:
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700372 pos = page_offset(page) +
373 (char*)de - (char*)page_address(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200374 err = ufs_prepare_chunk(page, pos, rec_len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700375 if (err)
376 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (de->d_ino) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700378 struct ufs_dir_entry *de1 =
379 (struct ufs_dir_entry *) ((char *) de + name_len);
380 de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
381 de->d_reclen = cpu_to_fs16(sb, name_len);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 de = de1;
384 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 ufs_set_de_namlen(sb, de, namelen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700387 memcpy(de->d_name, name, namelen + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
389 ufs_set_de_type(sb, de, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700391 err = ufs_commit_chunk(page, pos, rec_len);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700392 dir->i_mtime = dir->i_ctime = current_time(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700393
394 mark_inode_dirty(dir);
395 /* OFFSET_CACHE */
396out_put:
397 ufs_put_page(page);
398out:
399 return err;
400out_unlock:
401 unlock_page(page);
402 goto out_put;
403}
404
405static inline unsigned
406ufs_validate_entry(struct super_block *sb, char *base,
407 unsigned offset, unsigned mask)
408{
409 struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
410 struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
Al Viroe0d508f2016-05-27 21:22:04 -0400411 while ((char*)p < (char*)de)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700412 p = ufs_next_entry(sb, p);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700413 return (char *)p - base;
414}
415
416
417/*
418 * This is blatantly stolen from ext2fs
419 */
420static int
Al Viro80886292013-05-15 18:51:49 -0400421ufs_readdir(struct file *file, struct dir_context *ctx)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700422{
Al Viro80886292013-05-15 18:51:49 -0400423 loff_t pos = ctx->pos;
424 struct inode *inode = file_inode(file);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700425 struct super_block *sb = inode->i_sb;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300426 unsigned int offset = pos & ~PAGE_MASK;
427 unsigned long n = pos >> PAGE_SHIFT;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200428 unsigned long npages = dir_pages(inode);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800429 unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
Al Viro80886292013-05-15 18:51:49 -0400430 int need_revalidate = file->f_version != inode->i_version;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700431 unsigned flags = UFS_SB(sb)->s_flags;
432
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700433 UFSD("BEGIN\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700434
435 if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
436 return 0;
437
438 for ( ; n < npages; n++, offset = 0) {
439 char *kaddr, *limit;
440 struct ufs_dir_entry *de;
441
442 struct page *page = ufs_get_page(inode, n);
443
444 if (IS_ERR(page)) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700445 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700446 "bad page in #%lu",
447 inode->i_ino);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300448 ctx->pos += PAGE_SIZE - offset;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700449 return -EIO;
450 }
451 kaddr = page_address(page);
452 if (unlikely(need_revalidate)) {
453 if (offset) {
454 offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300455 ctx->pos = (n<<PAGE_SHIFT) + offset;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700456 }
Al Viro80886292013-05-15 18:51:49 -0400457 file->f_version = inode->i_version;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700458 need_revalidate = 0;
459 }
460 de = (struct ufs_dir_entry *)(kaddr+offset);
461 limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
462 for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700463 if (de->d_ino) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700464 unsigned char d_type = DT_UNKNOWN;
465
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700466 UFSD("filldir(%s,%u)\n", de->d_name,
467 fs32_to_cpu(sb, de->d_ino));
468 UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700469
470 if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
471 d_type = de->d_u.d_44.d_type;
472
Al Viro80886292013-05-15 18:51:49 -0400473 if (!dir_emit(ctx, de->d_name,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700474 ufs_get_de_namlen(sb, de),
Al Viro80886292013-05-15 18:51:49 -0400475 fs32_to_cpu(sb, de->d_ino),
476 d_type)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700477 ufs_put_page(page);
478 return 0;
479 }
480 }
Al Viro80886292013-05-15 18:51:49 -0400481 ctx->pos += fs16_to_cpu(sb, de->d_reclen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700482 }
483 ufs_put_page(page);
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486}
487
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
490 * ufs_delete_entry deletes a directory entry by merging it with the
491 * previous entry.
492 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700493int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
494 struct page * page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700496 struct super_block *sb = inode->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700497 char *kaddr = page_address(page);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800498 unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700499 unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700500 loff_t pos;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700501 struct ufs_dir_entry *pde = NULL;
502 struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
503 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700505 UFSD("ENTER\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700506
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700507 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700508 fs32_to_cpu(sb, de->d_ino),
509 fs16_to_cpu(sb, de->d_reclen),
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700510 ufs_get_de_namlen(sb, de), de->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700512 while ((char*)de < (char*)dir) {
513 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700514 ufs_error(inode->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700515 "zero-length directory entry");
516 err = -EIO;
517 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700519 pde = de;
520 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700522 if (pde)
523 from = (char*)pde - (char*)page_address(page);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700524
525 pos = page_offset(page) + from;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700526 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200527 err = ufs_prepare_chunk(page, pos, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700528 BUG_ON(err);
529 if (pde)
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700530 pde->d_reclen = cpu_to_fs16(sb, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700531 dir->d_ino = 0;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700532 err = ufs_commit_chunk(page, pos, to - from);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700533 inode->i_ctime = inode->i_mtime = current_time(inode);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700534 mark_inode_dirty(inode);
535out:
536 ufs_put_page(page);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700537 UFSD("EXIT\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700538 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541int ufs_make_empty(struct inode * inode, struct inode *dir)
542{
543 struct super_block * sb = dir->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700544 struct address_space *mapping = inode->i_mapping;
545 struct page *page = grab_cache_page(mapping, 0);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800546 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct ufs_dir_entry * de;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700548 char *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int err;
550
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700551 if (!page)
552 return -ENOMEM;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700553
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200554 err = ufs_prepare_chunk(page, 0, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700555 if (err) {
556 unlock_page(page);
557 goto fail;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700560 kmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700561 base = (char*)page_address(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300562 memset(base, 0, PAGE_SIZE);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700563
564 de = (struct ufs_dir_entry *) base;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
567 ufs_set_de_type(sb, de, inode->i_mode);
568 ufs_set_de_namlen(sb, de, 1);
569 de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
570 strcpy (de->d_name, ".");
571 de = (struct ufs_dir_entry *)
572 ((char *)de + fs16_to_cpu(sb, de->d_reclen));
573 de->d_ino = cpu_to_fs32(sb, dir->i_ino);
574 ufs_set_de_type(sb, de, dir->i_mode);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800575 de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 ufs_set_de_namlen(sb, de, 2);
577 strcpy (de->d_name, "..");
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700578 kunmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700579
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800580 err = ufs_commit_chunk(page, 0, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700581fail:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300582 put_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700583 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586/*
587 * routine to check that the specified directory is empty (for rmdir)
588 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700589int ufs_empty_dir(struct inode * inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700591 struct super_block *sb = inode->i_sb;
592 struct page *page = NULL;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200593 unsigned long i, npages = dir_pages(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700595 for (i = 0; i < npages; i++) {
596 char *kaddr;
597 struct ufs_dir_entry *de;
598 page = ufs_get_page(inode, i);
599
600 if (IS_ERR(page))
601 continue;
602
603 kaddr = page_address(page);
604 de = (struct ufs_dir_entry *)kaddr;
605 kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
606
607 while ((char *)de <= kaddr) {
608 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700609 ufs_error(inode->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700610 "zero-length directory entry: "
611 "kaddr=%p, de=%p\n", kaddr, de);
612 goto not_empty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700614 if (de->d_ino) {
615 u16 namelen=ufs_get_de_namlen(sb, de);
616 /* check for . and .. */
617 if (de->d_name[0] != '.')
618 goto not_empty;
619 if (namelen > 2)
620 goto not_empty;
621 if (namelen < 2) {
622 if (inode->i_ino !=
623 fs32_to_cpu(sb, de->d_ino))
624 goto not_empty;
625 } else if (de->d_name[1] != '.')
626 goto not_empty;
627 }
628 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700630 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700633
634not_empty:
635 ufs_put_page(page);
636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800639const struct file_operations ufs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -0400641 .iterate_shared = ufs_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200642 .fsync = generic_file_fsync,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200643 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644};