~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/exfat/namei.c

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*
  3  * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4  */
  5 
  6 #include <linux/iversion.h>
  7 #include <linux/namei.h>
  8 #include <linux/slab.h>
  9 #include <linux/buffer_head.h>
 10 #include <linux/nls.h>
 11 
 12 #include "exfat_raw.h"
 13 #include "exfat_fs.h"
 14 
 15 static inline unsigned long exfat_d_version(struct dentry *dentry)
 16 {
 17         return (unsigned long) dentry->d_fsdata;
 18 }
 19 
 20 static inline void exfat_d_version_set(struct dentry *dentry,
 21                 unsigned long version)
 22 {
 23         dentry->d_fsdata = (void *) version;
 24 }
 25 
 26 /*
 27  * If new entry was created in the parent, it could create the 8.3 alias (the
 28  * shortname of logname).  So, the parent may have the negative-dentry which
 29  * matches the created 8.3 alias.
 30  *
 31  * If it happened, the negative dentry isn't actually negative anymore.  So,
 32  * drop it.
 33  */
 34 static int exfat_d_revalidate(struct dentry *dentry, unsigned int flags)
 35 {
 36         int ret;
 37 
 38         if (flags & LOOKUP_RCU)
 39                 return -ECHILD;
 40 
 41         /*
 42          * This is not negative dentry. Always valid.
 43          *
 44          * Note, rename() to existing directory entry will have ->d_inode, and
 45          * will use existing name which isn't specified name by user.
 46          *
 47          * We may be able to drop this positive dentry here. But dropping
 48          * positive dentry isn't good idea. So it's unsupported like
 49          * rename("filename", "FILENAME") for now.
 50          */
 51         if (d_really_is_positive(dentry))
 52                 return 1;
 53 
 54         /*
 55          * Drop the negative dentry, in order to make sure to use the case
 56          * sensitive name which is specified by user if this is for creation.
 57          */
 58         if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
 59                 return 0;
 60 
 61         spin_lock(&dentry->d_lock);
 62         ret = inode_eq_iversion(d_inode(dentry->d_parent),
 63                         exfat_d_version(dentry));
 64         spin_unlock(&dentry->d_lock);
 65         return ret;
 66 }
 67 
 68 /* returns the length of a struct qstr, ignoring trailing dots */
 69 static unsigned int exfat_striptail_len(unsigned int len, const char *name)
 70 {
 71         while (len && name[len - 1] == '.')
 72                 len--;
 73         return len;
 74 }
 75 
 76 /*
 77  * Compute the hash for the exfat name corresponding to the dentry.  If the name
 78  * is invalid, we leave the hash code unchanged so that the existing dentry can
 79  * be used. The exfat fs routines will return ENOENT or EINVAL as appropriate.
 80  */
 81 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
 82 {
 83         struct super_block *sb = dentry->d_sb;
 84         struct nls_table *t = EXFAT_SB(sb)->nls_io;
 85         const unsigned char *name = qstr->name;
 86         unsigned int len = exfat_striptail_len(qstr->len, qstr->name);
 87         unsigned long hash = init_name_hash(dentry);
 88         int i, charlen;
 89         wchar_t c;
 90 
 91         for (i = 0; i < len; i += charlen) {
 92                 charlen = t->char2uni(&name[i], len - i, &c);
 93                 if (charlen < 0)
 94                         return charlen;
 95                 hash = partial_name_hash(exfat_toupper(sb, c), hash);
 96         }
 97 
 98         qstr->hash = end_name_hash(hash);
 99         return 0;
100 }
101 
102 static int exfat_d_cmp(const struct dentry *dentry, unsigned int len,
103                 const char *str, const struct qstr *name)
104 {
105         struct super_block *sb = dentry->d_sb;
106         struct nls_table *t = EXFAT_SB(sb)->nls_io;
107         unsigned int alen = exfat_striptail_len(name->len, name->name);
108         unsigned int blen = exfat_striptail_len(len, str);
109         wchar_t c1, c2;
110         int charlen, i;
111 
112         if (alen != blen)
113                 return 1;
114 
115         for (i = 0; i < len; i += charlen) {
116                 charlen = t->char2uni(&name->name[i], alen - i, &c1);
117                 if (charlen < 0)
118                         return 1;
119                 if (charlen != t->char2uni(&str[i], blen - i, &c2))
120                         return 1;
121 
122                 if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2))
123                         return 1;
124         }
125 
126         return 0;
127 }
128 
129 const struct dentry_operations exfat_dentry_ops = {
130         .d_revalidate   = exfat_d_revalidate,
131         .d_hash         = exfat_d_hash,
132         .d_compare      = exfat_d_cmp,
133 };
134 
135 static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr)
136 {
137         struct super_block *sb = dentry->d_sb;
138         const unsigned char *name = qstr->name;
139         unsigned int len = exfat_striptail_len(qstr->len, qstr->name);
140         unsigned long hash = init_name_hash(dentry);
141         int i, charlen;
142         unicode_t u;
143 
144         for (i = 0; i < len; i += charlen) {
145                 charlen = utf8_to_utf32(&name[i], len - i, &u);
146                 if (charlen < 0)
147                         return charlen;
148 
149                 /*
150                  * exfat_toupper() works only for code points up to the U+FFFF.
151                  */
152                 hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u,
153                                          hash);
154         }
155 
156         qstr->hash = end_name_hash(hash);
157         return 0;
158 }
159 
160 static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len,
161                 const char *str, const struct qstr *name)
162 {
163         struct super_block *sb = dentry->d_sb;
164         unsigned int alen = exfat_striptail_len(name->len, name->name);
165         unsigned int blen = exfat_striptail_len(len, str);
166         unicode_t u_a, u_b;
167         int charlen, i;
168 
169         if (alen != blen)
170                 return 1;
171 
172         for (i = 0; i < alen; i += charlen) {
173                 charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a);
174                 if (charlen < 0)
175                         return 1;
176                 if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b))
177                         return 1;
178 
179                 if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
180                         if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
181                                 return 1;
182                 } else {
183                         if (u_a != u_b)
184                                 return 1;
185                 }
186         }
187 
188         return 0;
189 }
190 
191 const struct dentry_operations exfat_utf8_dentry_ops = {
192         .d_revalidate   = exfat_d_revalidate,
193         .d_hash         = exfat_utf8_d_hash,
194         .d_compare      = exfat_utf8_d_cmp,
195 };
196 
197 /* used only in search empty_slot() */
198 #define CNT_UNUSED_NOHIT        (-1)
199 #define CNT_UNUSED_HIT          (-2)
200 /* search EMPTY CONTINUOUS "num_entries" entries */
201 static int exfat_search_empty_slot(struct super_block *sb,
202                 struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
203                 int num_entries)
204 {
205         int i, dentry, num_empty = 0;
206         int dentries_per_clu;
207         unsigned int type;
208         struct exfat_chain clu;
209         struct exfat_dentry *ep;
210         struct exfat_sb_info *sbi = EXFAT_SB(sb);
211         struct buffer_head *bh;
212 
213         dentries_per_clu = sbi->dentries_per_clu;
214 
215         if (hint_femp->eidx != EXFAT_HINT_NONE) {
216                 dentry = hint_femp->eidx;
217                 if (num_entries <= hint_femp->count) {
218                         hint_femp->eidx = EXFAT_HINT_NONE;
219                         return dentry;
220                 }
221 
222                 exfat_chain_dup(&clu, &hint_femp->cur);
223         } else {
224                 exfat_chain_dup(&clu, p_dir);
225                 dentry = 0;
226         }
227 
228         while (clu.dir != EXFAT_EOF_CLUSTER) {
229                 i = dentry & (dentries_per_clu - 1);
230 
231                 for (; i < dentries_per_clu; i++, dentry++) {
232                         ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
233                         if (!ep)
234                                 return -EIO;
235                         type = exfat_get_entry_type(ep);
236                         brelse(bh);
237 
238                         if (type == TYPE_UNUSED || type == TYPE_DELETED) {
239                                 num_empty++;
240                                 if (hint_femp->eidx == EXFAT_HINT_NONE) {
241                                         hint_femp->eidx = dentry;
242                                         hint_femp->count = CNT_UNUSED_NOHIT;
243                                         exfat_chain_set(&hint_femp->cur,
244                                                 clu.dir, clu.size, clu.flags);
245                                 }
246 
247                                 if (type == TYPE_UNUSED &&
248                                     hint_femp->count != CNT_UNUSED_HIT)
249                                         hint_femp->count = CNT_UNUSED_HIT;
250                         } else {
251                                 if (hint_femp->eidx != EXFAT_HINT_NONE &&
252                                     hint_femp->count == CNT_UNUSED_HIT) {
253                                         /* unused empty group means
254                                          * an empty group which includes
255                                          * unused dentry
256                                          */
257                                         exfat_fs_error(sb,
258                                                 "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)",
259                                                 dentry, hint_femp->eidx,
260                                                 p_dir->dir, clu.dir);
261                                         return -EIO;
262                                 }
263 
264                                 num_empty = 0;
265                                 hint_femp->eidx = EXFAT_HINT_NONE;
266                         }
267 
268                         if (num_empty >= num_entries) {
269                                 /* found and invalidate hint_femp */
270                                 hint_femp->eidx = EXFAT_HINT_NONE;
271                                 return (dentry - (num_entries - 1));
272                         }
273                 }
274 
275                 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
276                         if (--clu.size > 0)
277                                 clu.dir++;
278                         else
279                                 clu.dir = EXFAT_EOF_CLUSTER;
280                 } else {
281                         if (exfat_get_next_cluster(sb, &clu.dir))
282                                 return -EIO;
283                 }
284         }
285 
286         return -ENOSPC;
287 }
288 
289 static int exfat_check_max_dentries(struct inode *inode)
290 {
291         if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
292                 /*
293                  * exFAT spec allows a dir to grow upto 8388608(256MB)
294                  * dentries
295                  */
296                 return -ENOSPC;
297         }
298         return 0;
299 }
300 
301 /* find empty directory entry.
302  * if there isn't any empty slot, expand cluster chain.
303  */
304 static int exfat_find_empty_entry(struct inode *inode,
305                 struct exfat_chain *p_dir, int num_entries)
306 {
307         int dentry;
308         unsigned int ret, last_clu;
309         sector_t sector;
310         loff_t size = 0;
311         struct exfat_chain clu;
312         struct exfat_dentry *ep = NULL;
313         struct super_block *sb = inode->i_sb;
314         struct exfat_sb_info *sbi = EXFAT_SB(sb);
315         struct exfat_inode_info *ei = EXFAT_I(inode);
316         struct exfat_hint_femp hint_femp;
317 
318         hint_femp.eidx = EXFAT_HINT_NONE;
319 
320         if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
321                 memcpy(&hint_femp, &ei->hint_femp,
322                                 sizeof(struct exfat_hint_femp));
323                 ei->hint_femp.eidx = EXFAT_HINT_NONE;
324         }
325 
326         while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
327                                         num_entries)) < 0) {
328                 if (dentry == -EIO)
329                         break;
330 
331                 if (exfat_check_max_dentries(inode))
332                         return -ENOSPC;
333 
334                 /* we trust p_dir->size regardless of FAT type */
335                 if (exfat_find_last_cluster(sb, p_dir, &last_clu))
336                         return -EIO;
337 
338                 /*
339                  * Allocate new cluster to this directory
340                  */
341                 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
342 
343                 /* allocate a cluster */
344                 ret = exfat_alloc_cluster(inode, 1, &clu);
345                 if (ret)
346                         return ret;
347 
348                 if (exfat_zeroed_cluster(inode, clu.dir))
349                         return -EIO;
350 
351                 /* append to the FAT chain */
352                 if (clu.flags != p_dir->flags) {
353                         /* no-fat-chain bit is disabled,
354                          * so fat-chain should be synced with alloc-bitmap
355                          */
356                         exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
357                         p_dir->flags = ALLOC_FAT_CHAIN;
358                         hint_femp.cur.flags = ALLOC_FAT_CHAIN;
359                 }
360 
361                 if (clu.flags == ALLOC_FAT_CHAIN)
362                         if (exfat_ent_set(sb, last_clu, clu.dir))
363                                 return -EIO;
364 
365                 if (hint_femp.eidx == EXFAT_HINT_NONE) {
366                         /* the special case that new dentry
367                          * should be allocated from the start of new cluster
368                          */
369                         hint_femp.eidx = EXFAT_B_TO_DEN_IDX(p_dir->size, sbi);
370                         hint_femp.count = sbi->dentries_per_clu;
371 
372                         exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
373                 }
374                 hint_femp.cur.size++;
375                 p_dir->size++;
376                 size = EXFAT_CLU_TO_B(p_dir->size, sbi);
377 
378                 /* update the directory entry */
379                 if (p_dir->dir != sbi->root_dir) {
380                         struct buffer_head *bh;
381 
382                         ep = exfat_get_dentry(sb,
383                                 &(ei->dir), ei->entry + 1, &bh, &sector);
384                         if (!ep)
385                                 return -EIO;
386 
387                         ep->dentry.stream.valid_size = cpu_to_le64(size);
388                         ep->dentry.stream.size = ep->dentry.stream.valid_size;
389                         ep->dentry.stream.flags = p_dir->flags;
390                         exfat_update_bh(bh, IS_DIRSYNC(inode));
391                         brelse(bh);
392                         if (exfat_update_dir_chksum(inode, &(ei->dir),
393                             ei->entry))
394                                 return -EIO;
395                 }
396 
397                 /* directory inode should be updated in here */
398                 i_size_write(inode, size);
399                 EXFAT_I(inode)->i_size_ondisk += sbi->cluster_size;
400                 EXFAT_I(inode)->i_size_aligned += sbi->cluster_size;
401                 EXFAT_I(inode)->flags = p_dir->flags;
402                 inode->i_blocks += 1 << sbi->sect_per_clus_bits;
403         }
404 
405         return dentry;
406 }
407 
408 /*
409  * Name Resolution Functions :
410  * Zero if it was successful; otherwise nonzero.
411  */
412 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
413                 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
414                 int lookup)
415 {
416         int namelen;
417         int lossy = NLS_NAME_NO_LOSSY;
418         struct super_block *sb = inode->i_sb;
419         struct exfat_sb_info *sbi = EXFAT_SB(sb);
420         struct exfat_inode_info *ei = EXFAT_I(inode);
421 
422         /* strip all trailing periods */
423         namelen = exfat_striptail_len(strlen(path), path);
424         if (!namelen)
425                 return -ENOENT;
426 
427         if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
428                 return -ENAMETOOLONG;
429 
430         /*
431          * strip all leading spaces :
432          * "MS windows 7" supports leading spaces.
433          * So we should skip this preprocessing for compatibility.
434          */
435 
436         /* file name conversion :
437          * If lookup case, we allow bad-name for compatibility.
438          */
439         namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
440                         &lossy);
441         if (namelen < 0)
442                 return namelen; /* return error value */
443 
444         if ((lossy && !lookup) || !namelen)
445                 return -EINVAL;
446 
447         exfat_chain_set(p_dir, ei->start_clu,
448                 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
449 
450         return 0;
451 }
452 
453 static inline int exfat_resolve_path(struct inode *inode,
454                 const unsigned char *path, struct exfat_chain *dir,
455                 struct exfat_uni_name *uni)
456 {
457         return __exfat_resolve_path(inode, path, dir, uni, 0);
458 }
459 
460 static inline int exfat_resolve_path_for_lookup(struct inode *inode,
461                 const unsigned char *path, struct exfat_chain *dir,
462                 struct exfat_uni_name *uni)
463 {
464         return __exfat_resolve_path(inode, path, dir, uni, 1);
465 }
466 
467 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
468 {
469         return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
470 }
471 
472 static int exfat_add_entry(struct inode *inode, const char *path,
473                 struct exfat_chain *p_dir, unsigned int type,
474                 struct exfat_dir_entry *info)
475 {
476         int ret, dentry, num_entries;
477         struct super_block *sb = inode->i_sb;
478         struct exfat_sb_info *sbi = EXFAT_SB(sb);
479         struct exfat_uni_name uniname;
480         struct exfat_chain clu;
481         int clu_size = 0;
482         unsigned int start_clu = EXFAT_FREE_CLUSTER;
483 
484         ret = exfat_resolve_path(inode, path, p_dir, &uniname);
485         if (ret)
486                 goto out;
487 
488         num_entries = exfat_calc_num_entries(&uniname);
489         if (num_entries < 0) {
490                 ret = num_entries;
491                 goto out;
492         }
493 
494         /* exfat_find_empty_entry must be called before alloc_cluster() */
495         dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
496         if (dentry < 0) {
497                 ret = dentry; /* -EIO or -ENOSPC */
498                 goto out;
499         }
500 
501         if (type == TYPE_DIR) {
502                 ret = exfat_alloc_new_dir(inode, &clu);
503                 if (ret)
504                         goto out;
505                 start_clu = clu.dir;
506                 clu_size = sbi->cluster_size;
507         }
508 
509         /* update the directory entry */
510         /* fill the dos name directory entry information of the created file.
511          * the first cluster is not determined yet. (0)
512          */
513         ret = exfat_init_dir_entry(inode, p_dir, dentry, type,
514                 start_clu, clu_size);
515         if (ret)
516                 goto out;
517 
518         ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
519         if (ret)
520                 goto out;
521 
522         memcpy(&info->dir, p_dir, sizeof(struct exfat_chain));
523         info->entry = dentry;
524         info->flags = ALLOC_NO_FAT_CHAIN;
525         info->type = type;
526 
527         if (type == TYPE_FILE) {
528                 info->attr = ATTR_ARCHIVE;
529                 info->start_clu = EXFAT_EOF_CLUSTER;
530                 info->size = 0;
531                 info->num_subdirs = 0;
532         } else {
533                 int count;
534                 struct exfat_chain cdir;
535 
536                 info->attr = ATTR_SUBDIR;
537                 info->start_clu = start_clu;
538                 info->size = clu_size;
539 
540                 exfat_chain_set(&cdir, info->start_clu,
541                         EXFAT_B_TO_CLU(info->size, sbi), info->flags);
542                 count = exfat_count_dir_entries(sb, &cdir);
543                 if (count < 0)
544                         return -EIO;
545                 info->num_subdirs = count + EXFAT_MIN_SUBDIR;
546         }
547         memset(&info->crtime, 0, sizeof(info->crtime));
548         memset(&info->mtime, 0, sizeof(info->mtime));
549         memset(&info->atime, 0, sizeof(info->atime));
550 out:
551         return ret;
552 }
553 
554 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
555                 bool excl)
556 {
557         struct super_block *sb = dir->i_sb;
558         struct inode *inode;
559         struct exfat_chain cdir;
560         struct exfat_dir_entry info;
561         loff_t i_pos;
562         int err;
563 
564         mutex_lock(&EXFAT_SB(sb)->s_lock);
565         exfat_set_volume_dirty(sb);
566         err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
567                 &info);
568         exfat_clear_volume_dirty(sb);
569         if (err)
570                 goto unlock;
571 
572         inode_inc_iversion(dir);
573         dir->i_ctime = dir->i_mtime = current_time(dir);
574         if (IS_DIRSYNC(dir))
575                 exfat_sync_inode(dir);
576         else
577                 mark_inode_dirty(dir);
578 
579         i_pos = exfat_make_i_pos(&info);
580         inode = exfat_build_inode(sb, &info, i_pos);
581         err = PTR_ERR_OR_ZERO(inode);
582         if (err)
583                 goto unlock;
584 
585         inode_inc_iversion(inode);
586         inode->i_mtime = inode->i_atime = inode->i_ctime =
587                 EXFAT_I(inode)->i_crtime = current_time(inode);
588         exfat_truncate_atime(&inode->i_atime);
589         /* timestamp is already written, so mark_inode_dirty() is unneeded. */
590 
591         d_instantiate(dentry, inode);
592 unlock:
593         mutex_unlock(&EXFAT_SB(sb)->s_lock);
594         return err;
595 }
596 
597 /* lookup a file */
598 static int exfat_find(struct inode *dir, struct qstr *qname,
599                 struct exfat_dir_entry *info)
600 {
601         int ret, dentry, num_entries, count;
602         struct exfat_chain cdir;
603         struct exfat_uni_name uni_name;
604         struct super_block *sb = dir->i_sb;
605         struct exfat_sb_info *sbi = EXFAT_SB(sb);
606         struct exfat_inode_info *ei = EXFAT_I(dir);
607 
608         if (qname->len == 0)
609                 return -ENOENT;
610 
611         /* check the validity of directory name in the given pathname */
612         ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
613         if (ret)
614                 return ret;
615 
616         num_entries = exfat_calc_num_entries(&uni_name);
617         if (num_entries < 0)
618                 return num_entries;
619 
620         /* check the validation of hint_stat and initialize it if required */
621         if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
622                 ei->hint_stat.clu = cdir.dir;
623                 ei->hint_stat.eidx = 0;
624                 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
625                 ei->hint_femp.eidx = EXFAT_HINT_NONE;
626         }
627 
628         /* search the file name for directories */
629         dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name,
630                         num_entries, TYPE_ALL);
631 
632         if ((dentry < 0) && (dentry != -EEXIST))
633                 return dentry; /* -error value */
634 
635         memcpy(&info->dir, &cdir.dir, sizeof(struct exfat_chain));
636         info->entry = dentry;
637         info->num_subdirs = 0;
638 
639         /* root directory itself */
640         if (unlikely(dentry == -EEXIST)) {
641                 int num_clu = 0;
642 
643                 info->type = TYPE_DIR;
644                 info->attr = ATTR_SUBDIR;
645                 info->flags = ALLOC_FAT_CHAIN;
646                 info->start_clu = sbi->root_dir;
647                 memset(&info->crtime, 0, sizeof(info->crtime));
648                 memset(&info->mtime, 0, sizeof(info->mtime));
649                 memset(&info->atime, 0, sizeof(info->atime));
650 
651                 exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
652                 if (exfat_count_num_clusters(sb, &cdir, &num_clu))
653                         return -EIO;
654                 info->size = num_clu << sbi->cluster_size_bits;
655 
656                 count = exfat_count_dir_entries(sb, &cdir);
657                 if (count < 0)
658                         return -EIO;
659 
660                 info->num_subdirs = count;
661         } else {
662                 struct exfat_dentry *ep, *ep2;
663                 struct exfat_entry_set_cache *es;
664 
665                 es = exfat_get_dentry_set(sb, &cdir, dentry, ES_2_ENTRIES);
666                 if (!es)
667                         return -EIO;
668                 ep = exfat_get_dentry_cached(es, 0);
669                 ep2 = exfat_get_dentry_cached(es, 1);
670 
671                 info->type = exfat_get_entry_type(ep);
672                 info->attr = le16_to_cpu(ep->dentry.file.attr);
673                 info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
674                 if ((info->type == TYPE_FILE) && (info->size == 0)) {
675                         info->flags = ALLOC_NO_FAT_CHAIN;
676                         info->start_clu = EXFAT_EOF_CLUSTER;
677                 } else {
678                         info->flags = ep2->dentry.stream.flags;
679                         info->start_clu =
680                                 le32_to_cpu(ep2->dentry.stream.start_clu);
681                 }
682 
683                 if (ei->start_clu == EXFAT_FREE_CLUSTER) {
684                         exfat_fs_error(sb,
685                                 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
686                                 i_size_read(dir), ei->dir.dir, ei->entry);
687                         exfat_free_dentry_set(es, false);
688                         return -EIO;
689                 }
690 
691                 exfat_get_entry_time(sbi, &info->crtime,
692                                 ep->dentry.file.create_tz,
693                                 ep->dentry.file.create_time,
694                                 ep->dentry.file.create_date,
695                                 ep->dentry.file.create_time_cs);
696                 exfat_get_entry_time(sbi, &info->mtime,
697                                 ep->dentry.file.modify_tz,
698                                 ep->dentry.file.modify_time,
699                                 ep->dentry.file.modify_date,
700                                 ep->dentry.file.modify_time_cs);
701                 exfat_get_entry_time(sbi, &info->atime,
702                                 ep->dentry.file.access_tz,
703                                 ep->dentry.file.access_time,
704                                 ep->dentry.file.access_date,
705                                 0);
706                 exfat_free_dentry_set(es, false);
707 
708                 if (info->type == TYPE_DIR) {
709                         exfat_chain_set(&cdir, info->start_clu,
710                                 EXFAT_B_TO_CLU(info->size, sbi), info->flags);
711                         count = exfat_count_dir_entries(sb, &cdir);
712                         if (count < 0)
713                                 return -EIO;
714 
715                         info->num_subdirs = count + EXFAT_MIN_SUBDIR;
716                 }
717         }
718         return 0;
719 }
720 
721 static int exfat_d_anon_disconn(struct dentry *dentry)
722 {
723         return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
724 }
725 
726 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
727                 unsigned int flags)
728 {
729         struct super_block *sb = dir->i_sb;
730         struct inode *inode;
731         struct dentry *alias;
732         struct exfat_dir_entry info;
733         int err;
734         loff_t i_pos;
735         mode_t i_mode;
736 
737         mutex_lock(&EXFAT_SB(sb)->s_lock);
738         err = exfat_find(dir, &dentry->d_name, &info);
739         if (err) {
740                 if (err == -ENOENT) {
741                         inode = NULL;
742                         goto out;
743                 }
744                 goto unlock;
745         }
746 
747         i_pos = exfat_make_i_pos(&info);
748         inode = exfat_build_inode(sb, &info, i_pos);
749         err = PTR_ERR_OR_ZERO(inode);
750         if (err)
751                 goto unlock;
752 
753         i_mode = inode->i_mode;
754         alias = d_find_alias(inode);
755 
756         /*
757          * Checking "alias->d_parent == dentry->d_parent" to make sure
758          * FS is not corrupted (especially double linked dir).
759          */
760         if (alias && alias->d_parent == dentry->d_parent &&
761                         !exfat_d_anon_disconn(alias)) {
762 
763                 /*
764                  * Unhashed alias is able to exist because of revalidate()
765                  * called by lookup_fast. You can easily make this status
766                  * by calling create and lookup concurrently
767                  * In such case, we reuse an alias instead of new dentry
768                  */
769                 if (d_unhashed(alias)) {
770                         WARN_ON(alias->d_name.hash_len !=
771                                 dentry->d_name.hash_len);
772                         exfat_info(sb, "rehashed a dentry(%p) in read lookup",
773                                    alias);
774                         d_drop(dentry);
775                         d_rehash(alias);
776                 } else if (!S_ISDIR(i_mode)) {
777                         /*
778                          * This inode has non anonymous-DCACHE_DISCONNECTED
779                          * dentry. This means, the user did ->lookup() by an
780                          * another name (longname vs 8.3 alias of it) in past.
781                          *
782                          * Switch to new one for reason of locality if possible.
783                          */
784                         d_move(alias, dentry);
785                 }
786                 iput(inode);
787                 mutex_unlock(&EXFAT_SB(sb)->s_lock);
788                 return alias;
789         }
790         dput(alias);
791 out:
792         mutex_unlock(&EXFAT_SB(sb)->s_lock);
793         if (!inode)
794                 exfat_d_version_set(dentry, inode_query_iversion(dir));
795 
796         return d_splice_alias(inode, dentry);
797 unlock:
798         mutex_unlock(&EXFAT_SB(sb)->s_lock);
799         return ERR_PTR(err);
800 }
801 
802 /* remove an entry, BUT don't truncate */
803 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
804 {
805         struct exfat_chain cdir;
806         struct exfat_dentry *ep;
807         struct super_block *sb = dir->i_sb;
808         struct inode *inode = dentry->d_inode;
809         struct exfat_inode_info *ei = EXFAT_I(inode);
810         struct buffer_head *bh;
811         sector_t sector;
812         int num_entries, entry, err = 0;
813 
814         mutex_lock(&EXFAT_SB(sb)->s_lock);
815         exfat_chain_dup(&cdir, &ei->dir);
816         entry = ei->entry;
817         if (ei->dir.dir == DIR_DELETED) {
818                 exfat_err(sb, "abnormal access to deleted dentry");
819                 err = -ENOENT;
820                 goto unlock;
821         }
822 
823         ep = exfat_get_dentry(sb, &cdir, entry, &bh, &sector);
824         if (!ep) {
825                 err = -EIO;
826                 goto unlock;
827         }
828         num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
829         if (num_entries < 0) {
830                 err = -EIO;
831                 brelse(bh);
832                 goto unlock;
833         }
834         num_entries++;
835         brelse(bh);
836 
837         exfat_set_volume_dirty(sb);
838         /* update the directory entry */
839         if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) {
840                 err = -EIO;
841                 goto unlock;
842         }
843 
844         /* This doesn't modify ei */
845         ei->dir.dir = DIR_DELETED;
846         exfat_clear_volume_dirty(sb);
847 
848         inode_inc_iversion(dir);
849         dir->i_mtime = dir->i_atime = current_time(dir);
850         exfat_truncate_atime(&dir->i_atime);
851         if (IS_DIRSYNC(dir))
852                 exfat_sync_inode(dir);
853         else
854                 mark_inode_dirty(dir);
855 
856         clear_nlink(inode);
857         inode->i_mtime = inode->i_atime = current_time(inode);
858         exfat_truncate_atime(&inode->i_atime);
859         exfat_unhash_inode(inode);
860         exfat_d_version_set(dentry, inode_query_iversion(dir));
861 unlock:
862         mutex_unlock(&EXFAT_SB(sb)->s_lock);
863         return err;
864 }
865 
866 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
867 {
868         struct super_block *sb = dir->i_sb;
869         struct inode *inode;
870         struct exfat_dir_entry info;
871         struct exfat_chain cdir;
872         loff_t i_pos;
873         int err;
874 
875         mutex_lock(&EXFAT_SB(sb)->s_lock);
876         exfat_set_volume_dirty(sb);
877         err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
878                 &info);
879         exfat_clear_volume_dirty(sb);
880         if (err)
881                 goto unlock;
882 
883         inode_inc_iversion(dir);
884         dir->i_ctime = dir->i_mtime = current_time(dir);
885         if (IS_DIRSYNC(dir))
886                 exfat_sync_inode(dir);
887         else
888                 mark_inode_dirty(dir);
889         inc_nlink(dir);
890 
891         i_pos = exfat_make_i_pos(&info);
892         inode = exfat_build_inode(sb, &info, i_pos);
893         err = PTR_ERR_OR_ZERO(inode);
894         if (err)
895                 goto unlock;
896 
897         inode_inc_iversion(inode);
898         inode->i_mtime = inode->i_atime = inode->i_ctime =
899                 EXFAT_I(inode)->i_crtime = current_time(inode);
900         exfat_truncate_atime(&inode->i_atime);
901         /* timestamp is already written, so mark_inode_dirty() is unneeded. */
902 
903         d_instantiate(dentry, inode);
904 
905 unlock:
906         mutex_unlock(&EXFAT_SB(sb)->s_lock);
907         return err;
908 }
909 
910 static int exfat_check_dir_empty(struct super_block *sb,
911                 struct exfat_chain *p_dir)
912 {
913         int i, dentries_per_clu;
914         unsigned int type;
915         struct exfat_chain clu;
916         struct exfat_dentry *ep;
917         struct exfat_sb_info *sbi = EXFAT_SB(sb);
918         struct buffer_head *bh;
919 
920         dentries_per_clu = sbi->dentries_per_clu;
921 
922         exfat_chain_dup(&clu, p_dir);
923 
924         while (clu.dir != EXFAT_EOF_CLUSTER) {
925                 for (i = 0; i < dentries_per_clu; i++) {
926                         ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
927                         if (!ep)
928                                 return -EIO;
929                         type = exfat_get_entry_type(ep);
930                         brelse(bh);
931                         if (type == TYPE_UNUSED)
932                                 return 0;
933 
934                         if (type != TYPE_FILE && type != TYPE_DIR)
935                                 continue;
936 
937                         return -ENOTEMPTY;
938                 }
939 
940                 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
941                         if (--clu.size > 0)
942                                 clu.dir++;
943                         else
944                                 clu.dir = EXFAT_EOF_CLUSTER;
945                 } else {
946                         if (exfat_get_next_cluster(sb, &(clu.dir)))
947                                 return -EIO;
948                 }
949         }
950 
951         return 0;
952 }
953 
954 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
955 {
956         struct inode *inode = dentry->d_inode;
957         struct exfat_dentry *ep;
958         struct exfat_chain cdir, clu_to_free;
959         struct super_block *sb = inode->i_sb;
960         struct exfat_sb_info *sbi = EXFAT_SB(sb);
961         struct exfat_inode_info *ei = EXFAT_I(inode);
962         struct buffer_head *bh;
963         sector_t sector;
964         int num_entries, entry, err;
965 
966         mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
967 
968         exfat_chain_dup(&cdir, &ei->dir);
969         entry = ei->entry;
970 
971         if (ei->dir.dir == DIR_DELETED) {
972                 exfat_err(sb, "abnormal access to deleted dentry");
973                 err = -ENOENT;
974                 goto unlock;
975         }
976 
977         exfat_chain_set(&clu_to_free, ei->start_clu,
978                 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
979 
980         err = exfat_check_dir_empty(sb, &clu_to_free);
981         if (err) {
982                 if (err == -EIO)
983                         exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
984                                   err);
985                 goto unlock;
986         }
987 
988         ep = exfat_get_dentry(sb, &cdir, entry, &bh, &sector);
989         if (!ep) {
990                 err = -EIO;
991                 goto unlock;
992         }
993 
994         num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
995         if (num_entries < 0) {
996                 err = -EIO;
997                 brelse(bh);
998                 goto unlock;
999         }
1000         num_entries++;
1001         brelse(bh);
1002 
1003         exfat_set_volume_dirty(sb);
1004         err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries);
1005         if (err) {
1006                 exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err);
1007                 goto unlock;
1008         }
1009         ei->dir.dir = DIR_DELETED;
1010         exfat_clear_volume_dirty(sb);
1011 
1012         inode_inc_iversion(dir);
1013         dir->i_mtime = dir->i_atime = current_time(dir);
1014         exfat_truncate_atime(&dir->i_atime);
1015         if (IS_DIRSYNC(dir))
1016                 exfat_sync_inode(dir);
1017         else
1018                 mark_inode_dirty(dir);
1019         drop_nlink(dir);
1020 
1021         clear_nlink(inode);
1022         inode->i_mtime = inode->i_atime = current_time(inode);
1023         exfat_truncate_atime(&inode->i_atime);
1024         exfat_unhash_inode(inode);
1025         exfat_d_version_set(dentry, inode_query_iversion(dir));
1026 unlock:
1027         mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
1028         return err;
1029 }
1030 
1031 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
1032                 int oldentry, struct exfat_uni_name *p_uniname,
1033                 struct exfat_inode_info *ei)
1034 {
1035         int ret, num_old_entries, num_new_entries;
1036         sector_t sector_old, sector_new;
1037         struct exfat_dentry *epold, *epnew;
1038         struct super_block *sb = inode->i_sb;
1039         struct buffer_head *new_bh, *old_bh;
1040         int sync = IS_DIRSYNC(inode);
1041 
1042         epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh, &sector_old);
1043         if (!epold)
1044                 return -EIO;
1045 
1046         num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold);
1047         if (num_old_entries < 0)
1048                 return -EIO;
1049         num_old_entries++;
1050 
1051         num_new_entries = exfat_calc_num_entries(p_uniname);
1052         if (num_new_entries < 0)
1053                 return num_new_entries;
1054 
1055         if (num_old_entries < num_new_entries) {
1056                 int newentry;
1057 
1058                 newentry =
1059                         exfat_find_empty_entry(inode, p_dir, num_new_entries);
1060                 if (newentry < 0)
1061                         return newentry; /* -EIO or -ENOSPC */
1062 
1063                 epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh,
1064                         &sector_new);
1065                 if (!epnew)
1066                         return -EIO;
1067 
1068                 memcpy(epnew, epold, DENTRY_SIZE);
1069                 if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1070                         epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1071                         ei->attr |= ATTR_ARCHIVE;
1072                 }
1073                 exfat_update_bh(new_bh, sync);
1074                 brelse(old_bh);
1075                 brelse(new_bh);
1076 
1077                 epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh,
1078                         &sector_old);
1079                 if (!epold)
1080                         return -EIO;
1081                 epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh,
1082                         &sector_new);
1083                 if (!epnew) {
1084                         brelse(old_bh);
1085                         return -EIO;
1086                 }
1087 
1088                 memcpy(epnew, epold, DENTRY_SIZE);
1089                 exfat_update_bh(new_bh, sync);
1090                 brelse(old_bh);
1091                 brelse(new_bh);
1092 
1093                 ret = exfat_init_ext_entry(inode, p_dir, newentry,
1094                         num_new_entries, p_uniname);
1095                 if (ret)
1096                         return ret;
1097 
1098                 exfat_remove_entries(inode, p_dir, oldentry, 0,
1099                         num_old_entries);
1100                 ei->entry = newentry;
1101         } else {
1102                 if (exfat_get_entry_type(epold) == TYPE_FILE) {
1103                         epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1104                         ei->attr |= ATTR_ARCHIVE;
1105                 }
1106                 exfat_update_bh(old_bh, sync);
1107                 brelse(old_bh);
1108                 ret = exfat_init_ext_entry(inode, p_dir, oldentry,
1109                         num_new_entries, p_uniname);
1110                 if (ret)
1111                         return ret;
1112 
1113                 exfat_remove_entries(inode, p_dir, oldentry, num_new_entries,
1114                         num_old_entries);
1115         }
1116         return 0;
1117 }
1118 
1119 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
1120                 int oldentry, struct exfat_chain *p_newdir,
1121                 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
1122 {
1123         int ret, newentry, num_new_entries, num_old_entries;
1124         sector_t sector_mov, sector_new;
1125         struct exfat_dentry *epmov, *epnew;
1126         struct super_block *sb = inode->i_sb;
1127         struct buffer_head *mov_bh, *new_bh;
1128 
1129         epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh, &sector_mov);
1130         if (!epmov)
1131                 return -EIO;
1132 
1133         /* check if the source and target directory is the same */
1134         if (exfat_get_entry_type(epmov) == TYPE_DIR &&
1135             le32_to_cpu(epmov->dentry.stream.start_clu) == p_newdir->dir)
1136                 return -EINVAL;
1137 
1138         num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry,
1139                 epmov);
1140         if (num_old_entries < 0)
1141                 return -EIO;
1142         num_old_entries++;
1143 
1144         num_new_entries = exfat_calc_num_entries(p_uniname);
1145         if (num_new_entries < 0)
1146                 return num_new_entries;
1147 
1148         newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
1149         if (newentry < 0)
1150                 return newentry; /* -EIO or -ENOSPC */
1151 
1152         epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh, &sector_new);
1153         if (!epnew)
1154                 return -EIO;
1155 
1156         memcpy(epnew, epmov, DENTRY_SIZE);
1157         if (exfat_get_entry_type(epnew) == TYPE_FILE) {
1158                 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
1159                 ei->attr |= ATTR_ARCHIVE;
1160         }
1161         exfat_update_bh(new_bh, IS_DIRSYNC(inode));
1162         brelse(mov_bh);
1163         brelse(new_bh);
1164 
1165         epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh,
1166                 &sector_mov);
1167         if (!epmov)
1168                 return -EIO;
1169         epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh,
1170                 &sector_new);
1171         if (!epnew) {
1172                 brelse(mov_bh);
1173                 return -EIO;
1174         }
1175 
1176         memcpy(epnew, epmov, DENTRY_SIZE);
1177         exfat_update_bh(new_bh, IS_DIRSYNC(inode));
1178         brelse(mov_bh);
1179         brelse(new_bh);
1180 
1181         ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
1182                 p_uniname);
1183         if (ret)
1184                 return ret;
1185 
1186         exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries);
1187 
1188         exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
1189                 p_newdir->flags);
1190 
1191         ei->entry = newentry;
1192         return 0;
1193 }
1194 
1195 static void exfat_update_parent_info(struct exfat_inode_info *ei,
1196                 struct inode *parent_inode)
1197 {
1198         struct exfat_sb_info *sbi = EXFAT_SB(parent_inode->i_sb);
1199         struct exfat_inode_info *parent_ei = EXFAT_I(parent_inode);
1200         loff_t parent_isize = i_size_read(parent_inode);
1201 
1202         /*
1203          * the problem that struct exfat_inode_info caches wrong parent info.
1204          *
1205          * because of flag-mismatch of ei->dir,
1206          * there is abnormal traversing cluster chain.
1207          */
1208         if (unlikely(parent_ei->flags != ei->dir.flags ||
1209                      parent_isize != EXFAT_CLU_TO_B(ei->dir.size, sbi) ||
1210                      parent_ei->start_clu != ei->dir.dir)) {
1211                 exfat_chain_set(&ei->dir, parent_ei->start_clu,
1212                         EXFAT_B_TO_CLU_ROUND_UP(parent_isize, sbi),
1213                         parent_ei->flags);
1214         }
1215 }
1216 
1217 /* rename or move a old file into a new file */
1218 static int __exfat_rename(struct inode *old_parent_inode,
1219                 struct exfat_inode_info *ei, struct inode *new_parent_inode,
1220                 struct dentry *new_dentry)
1221 {
1222         int ret;
1223         int dentry;
1224         struct exfat_chain olddir, newdir;
1225         struct exfat_chain *p_dir = NULL;
1226         struct exfat_uni_name uni_name;
1227         struct exfat_dentry *ep;
1228         struct super_block *sb = old_parent_inode->i_sb;
1229         struct exfat_sb_info *sbi = EXFAT_SB(sb);
1230         const unsigned char *new_path = new_dentry->d_name.name;
1231         struct inode *new_inode = new_dentry->d_inode;
1232         int num_entries;
1233         struct exfat_inode_info *new_ei = NULL;
1234         unsigned int new_entry_type = TYPE_UNUSED;
1235         int new_entry = 0;
1236         struct buffer_head *old_bh, *new_bh = NULL;
1237 
1238         /* check the validity of pointer parameters */
1239         if (new_path == NULL || strlen(new_path) == 0)
1240                 return -EINVAL;
1241 
1242         if (ei->dir.dir == DIR_DELETED) {
1243                 exfat_err(sb, "abnormal access to deleted source dentry");
1244                 return -ENOENT;
1245         }
1246 
1247         exfat_update_parent_info(ei, old_parent_inode);
1248 
1249         exfat_chain_dup(&olddir, &ei->dir);
1250         dentry = ei->entry;
1251 
1252         ep = exfat_get_dentry(sb, &olddir, dentry, &old_bh, NULL);
1253         if (!ep) {
1254                 ret = -EIO;
1255                 goto out;
1256         }
1257         brelse(old_bh);
1258 
1259         /* check whether new dir is existing directory and empty */
1260         if (new_inode) {
1261                 ret = -EIO;
1262                 new_ei = EXFAT_I(new_inode);
1263 
1264                 if (new_ei->dir.dir == DIR_DELETED) {
1265                         exfat_err(sb, "abnormal access to deleted target dentry");
1266                         goto out;
1267                 }
1268 
1269                 exfat_update_parent_info(new_ei, new_parent_inode);
1270 
1271                 p_dir = &(new_ei->dir);
1272                 new_entry = new_ei->entry;
1273                 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1274                 if (!ep)
1275                         goto out;
1276 
1277                 new_entry_type = exfat_get_entry_type(ep);
1278                 brelse(new_bh);
1279 
1280                 /* if new_inode exists, update ei */
1281                 if (new_entry_type == TYPE_DIR) {
1282                         struct exfat_chain new_clu;
1283 
1284                         new_clu.dir = new_ei->start_clu;
1285                         new_clu.size =
1286                                 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1287                                 sbi);
1288                         new_clu.flags = new_ei->flags;
1289 
1290                         ret = exfat_check_dir_empty(sb, &new_clu);
1291                         if (ret)
1292                                 goto out;
1293                 }
1294         }
1295 
1296         /* check the validity of directory name in the given new pathname */
1297         ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
1298                         &uni_name);
1299         if (ret)
1300                 goto out;
1301 
1302         exfat_set_volume_dirty(sb);
1303 
1304         if (olddir.dir == newdir.dir)
1305                 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1306                                 &uni_name, ei);
1307         else
1308                 ret = exfat_move_file(new_parent_inode, &olddir, dentry,
1309                                 &newdir, &uni_name, ei);
1310 
1311         if (!ret && new_inode) {
1312                 /* delete entries of new_dir */
1313                 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
1314                 if (!ep) {
1315                         ret = -EIO;
1316                         goto del_out;
1317                 }
1318 
1319                 num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep);
1320                 if (num_entries < 0) {
1321                         ret = -EIO;
1322                         goto del_out;
1323                 }
1324                 brelse(new_bh);
1325 
1326                 if (exfat_remove_entries(new_inode, p_dir, new_entry, 0,
1327                                 num_entries + 1)) {
1328                         ret = -EIO;
1329                         goto del_out;
1330                 }
1331 
1332                 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
1333                 if (new_entry_type == TYPE_DIR) {
1334                         /* new_ei, new_clu_to_free */
1335                         struct exfat_chain new_clu_to_free;
1336 
1337                         exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
1338                                 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
1339                                 sbi), new_ei->flags);
1340 
1341                         if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
1342                                 /* just set I/O error only */
1343                                 ret = -EIO;
1344                         }
1345 
1346                         i_size_write(new_inode, 0);
1347                         new_ei->start_clu = EXFAT_EOF_CLUSTER;
1348                         new_ei->flags = ALLOC_NO_FAT_CHAIN;
1349                 }
1350 del_out:
1351                 /* Update new_inode ei
1352                  * Prevent syncing removed new_inode
1353                  * (new_ei is already initialized above code ("if (new_inode)")
1354                  */
1355                 new_ei->dir.dir = DIR_DELETED;
1356         }
1357         exfat_clear_volume_dirty(sb);
1358 out:
1359         return ret;
1360 }
1361 
1362 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
1363                 struct inode *new_dir, struct dentry *new_dentry,
1364                 unsigned int flags)
1365 {
1366         struct inode *old_inode, *new_inode;
1367         struct super_block *sb = old_dir->i_sb;
1368         loff_t i_pos;
1369         int err;
1370 
1371         /*
1372          * The VFS already checks for existence, so for local filesystems
1373          * the RENAME_NOREPLACE implementation is equivalent to plain rename.
1374          * Don't support any other flags
1375          */
1376         if (flags & ~RENAME_NOREPLACE)
1377                 return -EINVAL;
1378 
1379         mutex_lock(&EXFAT_SB(sb)->s_lock);
1380         old_inode = old_dentry->d_inode;
1381         new_inode = new_dentry->d_inode;
1382 
1383         err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
1384         if (err)
1385                 goto unlock;
1386 
1387         inode_inc_iversion(new_dir);
1388         new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime =
1389                 EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
1390         exfat_truncate_atime(&new_dir->i_atime);
1391         if (IS_DIRSYNC(new_dir))
1392                 exfat_sync_inode(new_dir);
1393         else
1394                 mark_inode_dirty(new_dir);
1395 
1396         i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
1397                 (EXFAT_I(old_inode)->entry & 0xffffffff);
1398         exfat_unhash_inode(old_inode);
1399         exfat_hash_inode(old_inode, i_pos);
1400         if (IS_DIRSYNC(new_dir))
1401                 exfat_sync_inode(old_inode);
1402         else
1403                 mark_inode_dirty(old_inode);
1404 
1405         if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1406                 drop_nlink(old_dir);
1407                 if (!new_inode)
1408                         inc_nlink(new_dir);
1409         }
1410 
1411         inode_inc_iversion(old_dir);
1412         old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1413         if (IS_DIRSYNC(old_dir))
1414                 exfat_sync_inode(old_dir);
1415         else
1416                 mark_inode_dirty(old_dir);
1417 
1418         if (new_inode) {
1419                 exfat_unhash_inode(new_inode);
1420 
1421                 /* skip drop_nlink if new_inode already has been dropped */
1422                 if (new_inode->i_nlink) {
1423                         drop_nlink(new_inode);
1424                         if (S_ISDIR(new_inode->i_mode))
1425                                 drop_nlink(new_inode);
1426                 } else {
1427                         exfat_warn(sb, "abnormal access to an inode dropped");
1428                         WARN_ON(new_inode->i_nlink == 0);
1429                 }
1430                 new_inode->i_ctime = EXFAT_I(new_inode)->i_crtime =
1431                         current_time(new_inode);
1432         }
1433 
1434 unlock:
1435         mutex_unlock(&EXFAT_SB(sb)->s_lock);
1436         return err;
1437 }
1438 
1439 const struct inode_operations exfat_dir_inode_operations = {
1440         .create         = exfat_create,
1441         .lookup         = exfat_lookup,
1442         .unlink         = exfat_unlink,
1443         .mkdir          = exfat_mkdir,
1444         .rmdir          = exfat_rmdir,
1445         .rename         = exfat_rename,
1446         .setattr        = exfat_setattr,
1447         .getattr        = exfat_getattr,
1448 };
1449 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp