dir.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /* * This file is part of UBIFS.
  2. *
  3. * Copyright (C) 2006-2008 Nokia Corporation.
  4. * Copyright (C) 2006, 2007 University of Szeged, Hungary
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. * Zoltan Sogor
  22. */
  23. /*
  24. * This file implements directory operations.
  25. *
  26. * All FS operations in this file allocate budget before writing anything to the
  27. * media. If they fail to allocate it, the error is returned. The only
  28. * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
  29. * if they unable to allocate the budget, because deletion %-ENOSPC failure is
  30. * not what users are usually ready to get. UBIFS budgeting subsystem has some
  31. * space reserved for these purposes.
  32. *
  33. * All operations in this file write all inodes which they change straight
  34. * away, instead of marking them dirty. For example, 'ubifs_link()' changes
  35. * @i_size of the parent inode and writes the parent inode together with the
  36. * target inode. This was done to simplify file-system recovery which would
  37. * otherwise be very difficult to do. The only exception is rename which marks
  38. * the re-named inode dirty (because its @i_ctime is updated) but does not
  39. * write it, but just marks it as dirty.
  40. */
  41. #include "ubifs.h"
  42. /**
  43. * inherit_flags - inherit flags of the parent inode.
  44. * @dir: parent inode
  45. * @mode: new inode mode flags
  46. *
  47. * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
  48. * parent directory inode @dir. UBIFS inodes inherit the following flags:
  49. * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
  50. * sub-directory basis;
  51. * o %UBIFS_SYNC_FL - useful for the same reasons;
  52. * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
  53. *
  54. * This function returns the inherited flags.
  55. */
  56. static int inherit_flags(const struct inode *dir, int mode)
  57. {
  58. int flags;
  59. const struct ubifs_inode *ui = ubifs_inode(dir);
  60. if (!S_ISDIR(dir->i_mode))
  61. /*
  62. * The parent is not a directory, which means that an extended
  63. * attribute inode is being created. No flags.
  64. */
  65. return 0;
  66. flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
  67. if (!S_ISDIR(mode))
  68. /* The "DIRSYNC" flag only applies to directories */
  69. flags &= ~UBIFS_DIRSYNC_FL;
  70. return flags;
  71. }
  72. /**
  73. * ubifs_new_inode - allocate new UBIFS inode object.
  74. * @c: UBIFS file-system description object
  75. * @dir: parent directory inode
  76. * @mode: inode mode flags
  77. *
  78. * This function finds an unused inode number, allocates new inode and
  79. * initializes it. Returns new inode in case of success and an error code in
  80. * case of failure.
  81. */
  82. struct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir,
  83. int mode)
  84. {
  85. struct inode *inode;
  86. struct ubifs_inode *ui;
  87. inode = new_inode(c->vfs_sb);
  88. ui = ubifs_inode(inode);
  89. if (!inode)
  90. return ERR_PTR(-ENOMEM);
  91. /*
  92. * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
  93. * marking them dirty in file write path (see 'file_update_time()').
  94. * UBIFS has to fully control "clean <-> dirty" transitions of inodes
  95. * to make budgeting work.
  96. */
  97. inode->i_flags |= (S_NOCMTIME);
  98. inode->i_uid = current_fsuid();
  99. if (dir->i_mode & S_ISGID) {
  100. inode->i_gid = dir->i_gid;
  101. if (S_ISDIR(mode))
  102. mode |= S_ISGID;
  103. } else
  104. inode->i_gid = current_fsgid();
  105. inode->i_mode = mode;
  106. inode->i_mtime = inode->i_atime = inode->i_ctime =
  107. ubifs_current_time(inode);
  108. inode->i_mapping->nrpages = 0;
  109. /* Disable readahead */
  110. inode->i_mapping->backing_dev_info = &c->bdi;
  111. switch (mode & S_IFMT) {
  112. case S_IFREG:
  113. inode->i_mapping->a_ops = &ubifs_file_address_operations;
  114. inode->i_op = &ubifs_file_inode_operations;
  115. inode->i_fop = &ubifs_file_operations;
  116. break;
  117. case S_IFDIR:
  118. inode->i_op = &ubifs_dir_inode_operations;
  119. inode->i_fop = &ubifs_dir_operations;
  120. inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
  121. break;
  122. case S_IFLNK:
  123. inode->i_op = &ubifs_symlink_inode_operations;
  124. break;
  125. case S_IFSOCK:
  126. case S_IFIFO:
  127. case S_IFBLK:
  128. case S_IFCHR:
  129. inode->i_op = &ubifs_file_inode_operations;
  130. break;
  131. default:
  132. BUG();
  133. }
  134. ui->flags = inherit_flags(dir, mode);
  135. ubifs_set_inode_flags(inode);
  136. if (S_ISREG(mode))
  137. ui->compr_type = c->default_compr;
  138. else
  139. ui->compr_type = UBIFS_COMPR_NONE;
  140. ui->synced_i_size = 0;
  141. spin_lock(&c->cnt_lock);
  142. /* Inode number overflow is currently not supported */
  143. if (c->highest_inum >= INUM_WARN_WATERMARK) {
  144. if (c->highest_inum >= INUM_WATERMARK) {
  145. spin_unlock(&c->cnt_lock);
  146. ubifs_err("out of inode numbers");
  147. make_bad_inode(inode);
  148. iput(inode);
  149. return ERR_PTR(-EINVAL);
  150. }
  151. ubifs_warn("running out of inode numbers (current %lu, max %d)",
  152. (unsigned long)c->highest_inum, INUM_WATERMARK);
  153. }
  154. inode->i_ino = ++c->highest_inum;
  155. /*
  156. * The creation sequence number remains with this inode for its
  157. * lifetime. All nodes for this inode have a greater sequence number,
  158. * and so it is possible to distinguish obsolete nodes belonging to a
  159. * previous incarnation of the same inode number - for example, for the
  160. * purpose of rebuilding the index.
  161. */
  162. ui->creat_sqnum = ++c->max_sqnum;
  163. spin_unlock(&c->cnt_lock);
  164. return inode;
  165. }
  166. #ifdef CONFIG_UBIFS_FS_DEBUG
  167. static int dbg_check_name(struct ubifs_dent_node *dent, struct qstr *nm)
  168. {
  169. if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
  170. return 0;
  171. if (le16_to_cpu(dent->nlen) != nm->len)
  172. return -EINVAL;
  173. if (memcmp(dent->name, nm->name, nm->len))
  174. return -EINVAL;
  175. return 0;
  176. }
  177. #else
  178. #define dbg_check_name(dent, nm) 0
  179. #endif
  180. static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
  181. struct nameidata *nd)
  182. {
  183. int err;
  184. union ubifs_key key;
  185. struct inode *inode = NULL;
  186. struct ubifs_dent_node *dent;
  187. struct ubifs_info *c = dir->i_sb->s_fs_info;
  188. dbg_gen("'%.*s' in dir ino %lu",
  189. dentry->d_name.len, dentry->d_name.name, dir->i_ino);
  190. if (dentry->d_name.len > UBIFS_MAX_NLEN)
  191. return ERR_PTR(-ENAMETOOLONG);
  192. dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  193. if (!dent)
  194. return ERR_PTR(-ENOMEM);
  195. dent_key_init(c, &key, dir->i_ino, &dentry->d_name);
  196. err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name);
  197. if (err) {
  198. if (err == -ENOENT) {
  199. dbg_gen("not found");
  200. goto done;
  201. }
  202. goto out;
  203. }
  204. if (dbg_check_name(dent, &dentry->d_name)) {
  205. err = -EINVAL;
  206. goto out;
  207. }
  208. inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
  209. if (IS_ERR(inode)) {
  210. /*
  211. * This should not happen. Probably the file-system needs
  212. * checking.
  213. */
  214. err = PTR_ERR(inode);
  215. ubifs_err("dead directory entry '%.*s', error %d",
  216. dentry->d_name.len, dentry->d_name.name, err);
  217. ubifs_ro_mode(c, err);
  218. goto out;
  219. }
  220. done:
  221. kfree(dent);
  222. /*
  223. * Note, d_splice_alias() would be required instead if we supported
  224. * NFS.
  225. */
  226. d_add(dentry, inode);
  227. return NULL;
  228. out:
  229. kfree(dent);
  230. return ERR_PTR(err);
  231. }
  232. static int ubifs_create(struct inode *dir, struct dentry *dentry, int mode,
  233. struct nameidata *nd)
  234. {
  235. struct inode *inode;
  236. struct ubifs_info *c = dir->i_sb->s_fs_info;
  237. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  238. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  239. .dirtied_ino = 1 };
  240. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  241. /*
  242. * Budget request settings: new inode, new direntry, changing the
  243. * parent directory inode.
  244. */
  245. dbg_gen("dent '%.*s', mode %#x in dir ino %lu",
  246. dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino);
  247. err = ubifs_budget_space(c, &req);
  248. if (err)
  249. return err;
  250. inode = ubifs_new_inode(c, dir, mode);
  251. if (IS_ERR(inode)) {
  252. err = PTR_ERR(inode);
  253. goto out_budg;
  254. }
  255. mutex_lock(&dir_ui->ui_mutex);
  256. dir->i_size += sz_change;
  257. dir_ui->ui_size = dir->i_size;
  258. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  259. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  260. if (err)
  261. goto out_cancel;
  262. mutex_unlock(&dir_ui->ui_mutex);
  263. ubifs_release_budget(c, &req);
  264. insert_inode_hash(inode);
  265. d_instantiate(dentry, inode);
  266. return 0;
  267. out_cancel:
  268. dir->i_size -= sz_change;
  269. dir_ui->ui_size = dir->i_size;
  270. mutex_unlock(&dir_ui->ui_mutex);
  271. make_bad_inode(inode);
  272. iput(inode);
  273. out_budg:
  274. ubifs_release_budget(c, &req);
  275. ubifs_err("cannot create regular file, error %d", err);
  276. return err;
  277. }
  278. /**
  279. * vfs_dent_type - get VFS directory entry type.
  280. * @type: UBIFS directory entry type
  281. *
  282. * This function converts UBIFS directory entry type into VFS directory entry
  283. * type.
  284. */
  285. static unsigned int vfs_dent_type(uint8_t type)
  286. {
  287. switch (type) {
  288. case UBIFS_ITYPE_REG:
  289. return DT_REG;
  290. case UBIFS_ITYPE_DIR:
  291. return DT_DIR;
  292. case UBIFS_ITYPE_LNK:
  293. return DT_LNK;
  294. case UBIFS_ITYPE_BLK:
  295. return DT_BLK;
  296. case UBIFS_ITYPE_CHR:
  297. return DT_CHR;
  298. case UBIFS_ITYPE_FIFO:
  299. return DT_FIFO;
  300. case UBIFS_ITYPE_SOCK:
  301. return DT_SOCK;
  302. default:
  303. BUG();
  304. }
  305. return 0;
  306. }
  307. /*
  308. * The classical Unix view for directory is that it is a linear array of
  309. * (name, inode number) entries. Linux/VFS assumes this model as well.
  310. * Particularly, 'readdir()' call wants us to return a directory entry offset
  311. * which later may be used to continue 'readdir()'ing the directory or to
  312. * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
  313. * model because directory entries are identified by keys, which may collide.
  314. *
  315. * UBIFS uses directory entry hash value for directory offsets, so
  316. * 'seekdir()'/'telldir()' may not always work because of possible key
  317. * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
  318. * properly by means of saving full directory entry name in the private field
  319. * of the file description object.
  320. *
  321. * This means that UBIFS cannot support NFS which requires full
  322. * 'seekdir()'/'telldir()' support.
  323. */
  324. static int ubifs_readdir(struct file *file, void *dirent, filldir_t filldir)
  325. {
  326. int err, over = 0;
  327. struct qstr nm;
  328. union ubifs_key key;
  329. struct ubifs_dent_node *dent;
  330. struct inode *dir = file->f_path.dentry->d_inode;
  331. struct ubifs_info *c = dir->i_sb->s_fs_info;
  332. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  333. if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
  334. /*
  335. * The directory was seek'ed to a senseless position or there
  336. * are no more entries.
  337. */
  338. return 0;
  339. /* File positions 0 and 1 correspond to "." and ".." */
  340. if (file->f_pos == 0) {
  341. ubifs_assert(!file->private_data);
  342. over = filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR);
  343. if (over)
  344. return 0;
  345. file->f_pos = 1;
  346. }
  347. if (file->f_pos == 1) {
  348. ubifs_assert(!file->private_data);
  349. over = filldir(dirent, "..", 2, 1,
  350. parent_ino(file->f_path.dentry), DT_DIR);
  351. if (over)
  352. return 0;
  353. /* Find the first entry in TNC and save it */
  354. lowest_dent_key(c, &key, dir->i_ino);
  355. nm.name = NULL;
  356. dent = ubifs_tnc_next_ent(c, &key, &nm);
  357. if (IS_ERR(dent)) {
  358. err = PTR_ERR(dent);
  359. goto out;
  360. }
  361. file->f_pos = key_hash_flash(c, &dent->key);
  362. file->private_data = dent;
  363. }
  364. dent = file->private_data;
  365. if (!dent) {
  366. /*
  367. * The directory was seek'ed to and is now readdir'ed.
  368. * Find the entry corresponding to @file->f_pos or the
  369. * closest one.
  370. */
  371. dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
  372. nm.name = NULL;
  373. dent = ubifs_tnc_next_ent(c, &key, &nm);
  374. if (IS_ERR(dent)) {
  375. err = PTR_ERR(dent);
  376. goto out;
  377. }
  378. file->f_pos = key_hash_flash(c, &dent->key);
  379. file->private_data = dent;
  380. }
  381. while (1) {
  382. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  383. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  384. key_hash_flash(c, &dent->key));
  385. ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
  386. ubifs_inode(dir)->creat_sqnum);
  387. nm.len = le16_to_cpu(dent->nlen);
  388. over = filldir(dirent, dent->name, nm.len, file->f_pos,
  389. le64_to_cpu(dent->inum),
  390. vfs_dent_type(dent->type));
  391. if (over)
  392. return 0;
  393. /* Switch to the next entry */
  394. key_read(c, &dent->key, &key);
  395. nm.name = dent->name;
  396. dent = ubifs_tnc_next_ent(c, &key, &nm);
  397. if (IS_ERR(dent)) {
  398. err = PTR_ERR(dent);
  399. goto out;
  400. }
  401. kfree(file->private_data);
  402. file->f_pos = key_hash_flash(c, &dent->key);
  403. file->private_data = dent;
  404. cond_resched();
  405. }
  406. out:
  407. if (err != -ENOENT) {
  408. ubifs_err("cannot find next direntry, error %d", err);
  409. return err;
  410. }
  411. kfree(file->private_data);
  412. file->private_data = NULL;
  413. file->f_pos = 2;
  414. return 0;
  415. }
  416. /* If a directory is seeked, we have to free saved readdir() state */
  417. static loff_t ubifs_dir_llseek(struct file *file, loff_t offset, int origin)
  418. {
  419. kfree(file->private_data);
  420. file->private_data = NULL;
  421. return generic_file_llseek(file, offset, origin);
  422. }
  423. /* Free saved readdir() state when the directory is closed */
  424. static int ubifs_dir_release(struct inode *dir, struct file *file)
  425. {
  426. kfree(file->private_data);
  427. file->private_data = NULL;
  428. return 0;
  429. }
  430. /**
  431. * lock_2_inodes - a wrapper for locking two UBIFS inodes.
  432. * @inode1: first inode
  433. * @inode2: second inode
  434. *
  435. * We do not implement any tricks to guarantee strict lock ordering, because
  436. * VFS has already done it for us on the @i_mutex. So this is just a simple
  437. * wrapper function.
  438. */
  439. static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
  440. {
  441. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  442. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  443. }
  444. /**
  445. * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
  446. * @inode1: first inode
  447. * @inode2: second inode
  448. */
  449. static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
  450. {
  451. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  452. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  453. }
  454. static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
  455. struct dentry *dentry)
  456. {
  457. struct ubifs_info *c = dir->i_sb->s_fs_info;
  458. struct inode *inode = old_dentry->d_inode;
  459. struct ubifs_inode *ui = ubifs_inode(inode);
  460. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  461. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  462. struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
  463. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  464. /*
  465. * Budget request settings: new direntry, changing the target inode,
  466. * changing the parent inode.
  467. */
  468. dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu",
  469. dentry->d_name.len, dentry->d_name.name, inode->i_ino,
  470. inode->i_nlink, dir->i_ino);
  471. ubifs_assert(mutex_is_locked(&dir->i_mutex));
  472. ubifs_assert(mutex_is_locked(&inode->i_mutex));
  473. /*
  474. * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
  475. * otherwise has the potential to corrupt the orphan inode list.
  476. *
  477. * Indeed, consider a scenario when 'vfs_link(dirA/fileA)' and
  478. * 'vfs_unlink(dirA/fileA, dirB/fileB)' race. 'vfs_link()' does not
  479. * lock 'dirA->i_mutex', so this is possible. Both of the functions
  480. * lock 'fileA->i_mutex' though. Suppose 'vfs_unlink()' wins, and takes
  481. * 'fileA->i_mutex' mutex first. Suppose 'fileA->i_nlink' is 1. In this
  482. * case 'ubifs_unlink()' will drop the last reference, and put 'inodeA'
  483. * to the list of orphans. After this, 'vfs_link()' will link
  484. * 'dirB/fileB' to 'inodeA'. This is a problem because, for example,
  485. * the subsequent 'vfs_unlink(dirB/fileB)' will add the same inode
  486. * to the list of orphans.
  487. */
  488. if (inode->i_nlink == 0)
  489. return -ENOENT;
  490. err = dbg_check_synced_i_size(inode);
  491. if (err)
  492. return err;
  493. err = ubifs_budget_space(c, &req);
  494. if (err)
  495. return err;
  496. lock_2_inodes(dir, inode);
  497. inc_nlink(inode);
  498. atomic_inc(&inode->i_count);
  499. inode->i_ctime = ubifs_current_time(inode);
  500. dir->i_size += sz_change;
  501. dir_ui->ui_size = dir->i_size;
  502. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  503. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  504. if (err)
  505. goto out_cancel;
  506. unlock_2_inodes(dir, inode);
  507. ubifs_release_budget(c, &req);
  508. d_instantiate(dentry, inode);
  509. return 0;
  510. out_cancel:
  511. dir->i_size -= sz_change;
  512. dir_ui->ui_size = dir->i_size;
  513. drop_nlink(inode);
  514. unlock_2_inodes(dir, inode);
  515. ubifs_release_budget(c, &req);
  516. iput(inode);
  517. return err;
  518. }
  519. static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
  520. {
  521. struct ubifs_info *c = dir->i_sb->s_fs_info;
  522. struct inode *inode = dentry->d_inode;
  523. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  524. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  525. int err, budgeted = 1;
  526. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  527. /*
  528. * Budget request settings: deletion direntry, deletion inode (+1 for
  529. * @dirtied_ino), changing the parent directory inode. If budgeting
  530. * fails, go ahead anyway because we have extra space reserved for
  531. * deletions.
  532. */
  533. dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu",
  534. dentry->d_name.len, dentry->d_name.name, inode->i_ino,
  535. inode->i_nlink, dir->i_ino);
  536. ubifs_assert(mutex_is_locked(&dir->i_mutex));
  537. ubifs_assert(mutex_is_locked(&inode->i_mutex));
  538. err = dbg_check_synced_i_size(inode);
  539. if (err)
  540. return err;
  541. err = ubifs_budget_space(c, &req);
  542. if (err) {
  543. if (err != -ENOSPC)
  544. return err;
  545. budgeted = 0;
  546. }
  547. lock_2_inodes(dir, inode);
  548. inode->i_ctime = ubifs_current_time(dir);
  549. drop_nlink(inode);
  550. dir->i_size -= sz_change;
  551. dir_ui->ui_size = dir->i_size;
  552. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  553. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
  554. if (err)
  555. goto out_cancel;
  556. unlock_2_inodes(dir, inode);
  557. if (budgeted)
  558. ubifs_release_budget(c, &req);
  559. else {
  560. /* We've deleted something - clean the "no space" flags */
  561. c->nospace = c->nospace_rp = 0;
  562. smp_wmb();
  563. }
  564. return 0;
  565. out_cancel:
  566. dir->i_size += sz_change;
  567. dir_ui->ui_size = dir->i_size;
  568. inc_nlink(inode);
  569. unlock_2_inodes(dir, inode);
  570. if (budgeted)
  571. ubifs_release_budget(c, &req);
  572. return err;
  573. }
  574. /**
  575. * check_dir_empty - check if a directory is empty or not.
  576. * @c: UBIFS file-system description object
  577. * @dir: VFS inode object of the directory to check
  578. *
  579. * This function checks if directory @dir is empty. Returns zero if the
  580. * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
  581. * in case of of errors.
  582. */
  583. static int check_dir_empty(struct ubifs_info *c, struct inode *dir)
  584. {
  585. struct qstr nm = { .name = NULL };
  586. struct ubifs_dent_node *dent;
  587. union ubifs_key key;
  588. int err;
  589. lowest_dent_key(c, &key, dir->i_ino);
  590. dent = ubifs_tnc_next_ent(c, &key, &nm);
  591. if (IS_ERR(dent)) {
  592. err = PTR_ERR(dent);
  593. if (err == -ENOENT)
  594. err = 0;
  595. } else {
  596. kfree(dent);
  597. err = -ENOTEMPTY;
  598. }
  599. return err;
  600. }
  601. static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
  602. {
  603. struct ubifs_info *c = dir->i_sb->s_fs_info;
  604. struct inode *inode = dentry->d_inode;
  605. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  606. int err, budgeted = 1;
  607. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  608. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  609. /*
  610. * Budget request settings: deletion direntry, deletion inode and
  611. * changing the parent inode. If budgeting fails, go ahead anyway
  612. * because we have extra space reserved for deletions.
  613. */
  614. dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len,
  615. dentry->d_name.name, inode->i_ino, dir->i_ino);
  616. ubifs_assert(mutex_is_locked(&dir->i_mutex));
  617. ubifs_assert(mutex_is_locked(&inode->i_mutex));
  618. err = check_dir_empty(c, dentry->d_inode);
  619. if (err)
  620. return err;
  621. err = ubifs_budget_space(c, &req);
  622. if (err) {
  623. if (err != -ENOSPC)
  624. return err;
  625. budgeted = 0;
  626. }
  627. lock_2_inodes(dir, inode);
  628. inode->i_ctime = ubifs_current_time(dir);
  629. clear_nlink(inode);
  630. drop_nlink(dir);
  631. dir->i_size -= sz_change;
  632. dir_ui->ui_size = dir->i_size;
  633. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  634. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
  635. if (err)
  636. goto out_cancel;
  637. unlock_2_inodes(dir, inode);
  638. if (budgeted)
  639. ubifs_release_budget(c, &req);
  640. else {
  641. /* We've deleted something - clean the "no space" flags */
  642. c->nospace = c->nospace_rp = 0;
  643. smp_wmb();
  644. }
  645. return 0;
  646. out_cancel:
  647. dir->i_size += sz_change;
  648. dir_ui->ui_size = dir->i_size;
  649. inc_nlink(dir);
  650. inc_nlink(inode);
  651. inc_nlink(inode);
  652. unlock_2_inodes(dir, inode);
  653. if (budgeted)
  654. ubifs_release_budget(c, &req);
  655. return err;
  656. }
  657. static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  658. {
  659. struct inode *inode;
  660. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  661. struct ubifs_info *c = dir->i_sb->s_fs_info;
  662. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  663. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
  664. /*
  665. * Budget request settings: new inode, new direntry and changing parent
  666. * directory inode.
  667. */
  668. dbg_gen("dent '%.*s', mode %#x in dir ino %lu",
  669. dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino);
  670. err = ubifs_budget_space(c, &req);
  671. if (err)
  672. return err;
  673. inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
  674. if (IS_ERR(inode)) {
  675. err = PTR_ERR(inode);
  676. goto out_budg;
  677. }
  678. mutex_lock(&dir_ui->ui_mutex);
  679. insert_inode_hash(inode);
  680. inc_nlink(inode);
  681. inc_nlink(dir);
  682. dir->i_size += sz_change;
  683. dir_ui->ui_size = dir->i_size;
  684. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  685. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  686. if (err) {
  687. ubifs_err("cannot create directory, error %d", err);
  688. goto out_cancel;
  689. }
  690. mutex_unlock(&dir_ui->ui_mutex);
  691. ubifs_release_budget(c, &req);
  692. d_instantiate(dentry, inode);
  693. return 0;
  694. out_cancel:
  695. dir->i_size -= sz_change;
  696. dir_ui->ui_size = dir->i_size;
  697. drop_nlink(dir);
  698. mutex_unlock(&dir_ui->ui_mutex);
  699. make_bad_inode(inode);
  700. iput(inode);
  701. out_budg:
  702. ubifs_release_budget(c, &req);
  703. return err;
  704. }
  705. static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
  706. int mode, dev_t rdev)
  707. {
  708. struct inode *inode;
  709. struct ubifs_inode *ui;
  710. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  711. struct ubifs_info *c = dir->i_sb->s_fs_info;
  712. union ubifs_dev_desc *dev = NULL;
  713. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  714. int err, devlen = 0;
  715. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  716. .new_ino_d = ALIGN(devlen, 8),
  717. .dirtied_ino = 1 };
  718. /*
  719. * Budget request settings: new inode, new direntry and changing parent
  720. * directory inode.
  721. */
  722. dbg_gen("dent '%.*s' in dir ino %lu",
  723. dentry->d_name.len, dentry->d_name.name, dir->i_ino);
  724. if (!new_valid_dev(rdev))
  725. return -EINVAL;
  726. if (S_ISBLK(mode) || S_ISCHR(mode)) {
  727. dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  728. if (!dev)
  729. return -ENOMEM;
  730. devlen = ubifs_encode_dev(dev, rdev);
  731. }
  732. err = ubifs_budget_space(c, &req);
  733. if (err) {
  734. kfree(dev);
  735. return err;
  736. }
  737. inode = ubifs_new_inode(c, dir, mode);
  738. if (IS_ERR(inode)) {
  739. kfree(dev);
  740. err = PTR_ERR(inode);
  741. goto out_budg;
  742. }
  743. init_special_inode(inode, inode->i_mode, rdev);
  744. inode->i_size = ubifs_inode(inode)->ui_size = devlen;
  745. ui = ubifs_inode(inode);
  746. ui->data = dev;
  747. ui->data_len = devlen;
  748. mutex_lock(&dir_ui->ui_mutex);
  749. dir->i_size += sz_change;
  750. dir_ui->ui_size = dir->i_size;
  751. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  752. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  753. if (err)
  754. goto out_cancel;
  755. mutex_unlock(&dir_ui->ui_mutex);
  756. ubifs_release_budget(c, &req);
  757. insert_inode_hash(inode);
  758. d_instantiate(dentry, inode);
  759. return 0;
  760. out_cancel:
  761. dir->i_size -= sz_change;
  762. dir_ui->ui_size = dir->i_size;
  763. mutex_unlock(&dir_ui->ui_mutex);
  764. make_bad_inode(inode);
  765. iput(inode);
  766. out_budg:
  767. ubifs_release_budget(c, &req);
  768. return err;
  769. }
  770. static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
  771. const char *symname)
  772. {
  773. struct inode *inode;
  774. struct ubifs_inode *ui;
  775. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  776. struct ubifs_info *c = dir->i_sb->s_fs_info;
  777. int err, len = strlen(symname);
  778. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  779. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  780. .new_ino_d = ALIGN(len, 8),
  781. .dirtied_ino = 1 };
  782. /*
  783. * Budget request settings: new inode, new direntry and changing parent
  784. * directory inode.
  785. */
  786. dbg_gen("dent '%.*s', target '%s' in dir ino %lu", dentry->d_name.len,
  787. dentry->d_name.name, symname, dir->i_ino);
  788. if (len > UBIFS_MAX_INO_DATA)
  789. return -ENAMETOOLONG;
  790. err = ubifs_budget_space(c, &req);
  791. if (err)
  792. return err;
  793. inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
  794. if (IS_ERR(inode)) {
  795. err = PTR_ERR(inode);
  796. goto out_budg;
  797. }
  798. ui = ubifs_inode(inode);
  799. ui->data = kmalloc(len + 1, GFP_NOFS);
  800. if (!ui->data) {
  801. err = -ENOMEM;
  802. goto out_inode;
  803. }
  804. memcpy(ui->data, symname, len);
  805. ((char *)ui->data)[len] = '\0';
  806. /*
  807. * The terminating zero byte is not written to the flash media and it
  808. * is put just to make later in-memory string processing simpler. Thus,
  809. * data length is @len, not @len + %1.
  810. */
  811. ui->data_len = len;
  812. inode->i_size = ubifs_inode(inode)->ui_size = len;
  813. mutex_lock(&dir_ui->ui_mutex);
  814. dir->i_size += sz_change;
  815. dir_ui->ui_size = dir->i_size;
  816. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  817. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  818. if (err)
  819. goto out_cancel;
  820. mutex_unlock(&dir_ui->ui_mutex);
  821. ubifs_release_budget(c, &req);
  822. insert_inode_hash(inode);
  823. d_instantiate(dentry, inode);
  824. return 0;
  825. out_cancel:
  826. dir->i_size -= sz_change;
  827. dir_ui->ui_size = dir->i_size;
  828. mutex_unlock(&dir_ui->ui_mutex);
  829. out_inode:
  830. make_bad_inode(inode);
  831. iput(inode);
  832. out_budg:
  833. ubifs_release_budget(c, &req);
  834. return err;
  835. }
  836. /**
  837. * lock_3_inodes - a wrapper for locking three UBIFS inodes.
  838. * @inode1: first inode
  839. * @inode2: second inode
  840. * @inode3: third inode
  841. *
  842. * This function is used for 'ubifs_rename()' and @inode1 may be the same as
  843. * @inode2 whereas @inode3 may be %NULL.
  844. *
  845. * We do not implement any tricks to guarantee strict lock ordering, because
  846. * VFS has already done it for us on the @i_mutex. So this is just a simple
  847. * wrapper function.
  848. */
  849. static void lock_3_inodes(struct inode *inode1, struct inode *inode2,
  850. struct inode *inode3)
  851. {
  852. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  853. if (inode2 != inode1)
  854. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  855. if (inode3)
  856. mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
  857. }
  858. /**
  859. * unlock_3_inodes - a wrapper for unlocking three UBIFS inodes for rename.
  860. * @inode1: first inode
  861. * @inode2: second inode
  862. * @inode3: third inode
  863. */
  864. static void unlock_3_inodes(struct inode *inode1, struct inode *inode2,
  865. struct inode *inode3)
  866. {
  867. if (inode3)
  868. mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
  869. if (inode1 != inode2)
  870. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  871. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  872. }
  873. static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
  874. struct inode *new_dir, struct dentry *new_dentry)
  875. {
  876. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  877. struct inode *old_inode = old_dentry->d_inode;
  878. struct inode *new_inode = new_dentry->d_inode;
  879. struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
  880. int err, release, sync = 0, move = (new_dir != old_dir);
  881. int is_dir = S_ISDIR(old_inode->i_mode);
  882. int unlink = !!new_inode;
  883. int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
  884. int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
  885. struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  886. .dirtied_ino = 3 };
  887. struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
  888. .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
  889. struct timespec time;
  890. /*
  891. * Budget request settings: deletion direntry, new direntry, removing
  892. * the old inode, and changing old and new parent directory inodes.
  893. *
  894. * However, this operation also marks the target inode as dirty and
  895. * does not write it, so we allocate budget for the target inode
  896. * separately.
  897. */
  898. dbg_gen("dent '%.*s' ino %lu in dir ino %lu to dent '%.*s' in "
  899. "dir ino %lu", old_dentry->d_name.len, old_dentry->d_name.name,
  900. old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len,
  901. new_dentry->d_name.name, new_dir->i_ino);
  902. ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
  903. ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
  904. if (unlink)
  905. ubifs_assert(mutex_is_locked(&new_inode->i_mutex));
  906. if (unlink && is_dir) {
  907. err = check_dir_empty(c, new_inode);
  908. if (err)
  909. return err;
  910. }
  911. err = ubifs_budget_space(c, &req);
  912. if (err)
  913. return err;
  914. err = ubifs_budget_space(c, &ino_req);
  915. if (err) {
  916. ubifs_release_budget(c, &req);
  917. return err;
  918. }
  919. lock_3_inodes(old_dir, new_dir, new_inode);
  920. /*
  921. * Like most other Unix systems, set the @i_ctime for inodes on a
  922. * rename.
  923. */
  924. time = ubifs_current_time(old_dir);
  925. old_inode->i_ctime = time;
  926. /* We must adjust parent link count when renaming directories */
  927. if (is_dir) {
  928. if (move) {
  929. /*
  930. * @old_dir loses a link because we are moving
  931. * @old_inode to a different directory.
  932. */
  933. drop_nlink(old_dir);
  934. /*
  935. * @new_dir only gains a link if we are not also
  936. * overwriting an existing directory.
  937. */
  938. if (!unlink)
  939. inc_nlink(new_dir);
  940. } else {
  941. /*
  942. * @old_inode is not moving to a different directory,
  943. * but @old_dir still loses a link if we are
  944. * overwriting an existing directory.
  945. */
  946. if (unlink)
  947. drop_nlink(old_dir);
  948. }
  949. }
  950. old_dir->i_size -= old_sz;
  951. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  952. old_dir->i_mtime = old_dir->i_ctime = time;
  953. new_dir->i_mtime = new_dir->i_ctime = time;
  954. /*
  955. * And finally, if we unlinked a direntry which happened to have the
  956. * same name as the moved direntry, we have to decrement @i_nlink of
  957. * the unlinked inode and change its ctime.
  958. */
  959. if (unlink) {
  960. /*
  961. * Directories cannot have hard-links, so if this is a
  962. * directory, decrement its @i_nlink twice because an empty
  963. * directory has @i_nlink 2.
  964. */
  965. if (is_dir)
  966. drop_nlink(new_inode);
  967. new_inode->i_ctime = time;
  968. drop_nlink(new_inode);
  969. } else {
  970. new_dir->i_size += new_sz;
  971. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  972. }
  973. /*
  974. * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
  975. * is dirty, because this will be done later on at the end of
  976. * 'ubifs_rename()'.
  977. */
  978. if (IS_SYNC(old_inode)) {
  979. sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  980. if (unlink && IS_SYNC(new_inode))
  981. sync = 1;
  982. }
  983. err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry,
  984. sync);
  985. if (err)
  986. goto out_cancel;
  987. unlock_3_inodes(old_dir, new_dir, new_inode);
  988. ubifs_release_budget(c, &req);
  989. mutex_lock(&old_inode_ui->ui_mutex);
  990. release = old_inode_ui->dirty;
  991. mark_inode_dirty_sync(old_inode);
  992. mutex_unlock(&old_inode_ui->ui_mutex);
  993. if (release)
  994. ubifs_release_budget(c, &ino_req);
  995. if (IS_SYNC(old_inode))
  996. err = old_inode->i_sb->s_op->write_inode(old_inode, 1);
  997. return err;
  998. out_cancel:
  999. if (unlink) {
  1000. if (is_dir)
  1001. inc_nlink(new_inode);
  1002. inc_nlink(new_inode);
  1003. } else {
  1004. new_dir->i_size -= new_sz;
  1005. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  1006. }
  1007. old_dir->i_size += old_sz;
  1008. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  1009. if (is_dir) {
  1010. if (move) {
  1011. inc_nlink(old_dir);
  1012. if (!unlink)
  1013. drop_nlink(new_dir);
  1014. } else {
  1015. if (unlink)
  1016. inc_nlink(old_dir);
  1017. }
  1018. }
  1019. unlock_3_inodes(old_dir, new_dir, new_inode);
  1020. ubifs_release_budget(c, &ino_req);
  1021. ubifs_release_budget(c, &req);
  1022. return err;
  1023. }
  1024. int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1025. struct kstat *stat)
  1026. {
  1027. loff_t size;
  1028. struct inode *inode = dentry->d_inode;
  1029. struct ubifs_inode *ui = ubifs_inode(inode);
  1030. mutex_lock(&ui->ui_mutex);
  1031. stat->dev = inode->i_sb->s_dev;
  1032. stat->ino = inode->i_ino;
  1033. stat->mode = inode->i_mode;
  1034. stat->nlink = inode->i_nlink;
  1035. stat->uid = inode->i_uid;
  1036. stat->gid = inode->i_gid;
  1037. stat->rdev = inode->i_rdev;
  1038. stat->atime = inode->i_atime;
  1039. stat->mtime = inode->i_mtime;
  1040. stat->ctime = inode->i_ctime;
  1041. stat->blksize = UBIFS_BLOCK_SIZE;
  1042. stat->size = ui->ui_size;
  1043. /*
  1044. * Unfortunately, the 'stat()' system call was designed for block
  1045. * device based file systems, and it is not appropriate for UBIFS,
  1046. * because UBIFS does not have notion of "block". For example, it is
  1047. * difficult to tell how many block a directory takes - it actually
  1048. * takes less than 300 bytes, but we have to round it to block size,
  1049. * which introduces large mistake. This makes utilities like 'du' to
  1050. * report completely senseless numbers. This is the reason why UBIFS
  1051. * goes the same way as JFFS2 - it reports zero blocks for everything
  1052. * but regular files, which makes more sense than reporting completely
  1053. * wrong sizes.
  1054. */
  1055. if (S_ISREG(inode->i_mode)) {
  1056. size = ui->xattr_size;
  1057. size += stat->size;
  1058. size = ALIGN(size, UBIFS_BLOCK_SIZE);
  1059. /*
  1060. * Note, user-space expects 512-byte blocks count irrespectively
  1061. * of what was reported in @stat->size.
  1062. */
  1063. stat->blocks = size >> 9;
  1064. } else
  1065. stat->blocks = 0;
  1066. mutex_unlock(&ui->ui_mutex);
  1067. return 0;
  1068. }
  1069. const struct inode_operations ubifs_dir_inode_operations = {
  1070. .lookup = ubifs_lookup,
  1071. .create = ubifs_create,
  1072. .link = ubifs_link,
  1073. .symlink = ubifs_symlink,
  1074. .unlink = ubifs_unlink,
  1075. .mkdir = ubifs_mkdir,
  1076. .rmdir = ubifs_rmdir,
  1077. .mknod = ubifs_mknod,
  1078. .rename = ubifs_rename,
  1079. .setattr = ubifs_setattr,
  1080. .getattr = ubifs_getattr,
  1081. #ifdef CONFIG_UBIFS_FS_XATTR
  1082. .setxattr = ubifs_setxattr,
  1083. .getxattr = ubifs_getxattr,
  1084. .listxattr = ubifs_listxattr,
  1085. .removexattr = ubifs_removexattr,
  1086. #endif
  1087. };
  1088. const struct file_operations ubifs_dir_operations = {
  1089. .llseek = ubifs_dir_llseek,
  1090. .release = ubifs_dir_release,
  1091. .read = generic_read_dir,
  1092. .readdir = ubifs_readdir,
  1093. .fsync = ubifs_fsync,
  1094. .unlocked_ioctl = ubifs_ioctl,
  1095. #ifdef CONFIG_COMPAT
  1096. .compat_ioctl = ubifs_compat_ioctl,
  1097. #endif
  1098. };