dir.c 32 KB

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