dir.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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;
  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. if (err != -ENOENT) {
  405. ubifs_err(c, "cannot find next direntry, error %d", err);
  406. return err;
  407. }
  408. kfree(file->private_data);
  409. file->private_data = NULL;
  410. /* 2 is a special value indicating that there are no more direntries */
  411. ctx->pos = 2;
  412. return 0;
  413. }
  414. /* Free saved readdir() state when the directory is closed */
  415. static int ubifs_dir_release(struct inode *dir, struct file *file)
  416. {
  417. kfree(file->private_data);
  418. file->private_data = NULL;
  419. return 0;
  420. }
  421. /**
  422. * lock_2_inodes - a wrapper for locking two UBIFS inodes.
  423. * @inode1: first inode
  424. * @inode2: second inode
  425. *
  426. * We do not implement any tricks to guarantee strict lock ordering, because
  427. * VFS has already done it for us on the @i_mutex. So this is just a simple
  428. * wrapper function.
  429. */
  430. static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
  431. {
  432. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  433. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  434. }
  435. /**
  436. * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
  437. * @inode1: first inode
  438. * @inode2: second inode
  439. */
  440. static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
  441. {
  442. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  443. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  444. }
  445. static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
  446. struct dentry *dentry)
  447. {
  448. struct ubifs_info *c = dir->i_sb->s_fs_info;
  449. struct inode *inode = d_inode(old_dentry);
  450. struct ubifs_inode *ui = ubifs_inode(inode);
  451. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  452. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  453. struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
  454. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  455. /*
  456. * Budget request settings: new direntry, changing the target inode,
  457. * changing the parent inode.
  458. */
  459. dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
  460. dentry, inode->i_ino,
  461. inode->i_nlink, dir->i_ino);
  462. ubifs_assert(mutex_is_locked(&dir->i_mutex));
  463. ubifs_assert(mutex_is_locked(&inode->i_mutex));
  464. err = dbg_check_synced_i_size(c, inode);
  465. if (err)
  466. return err;
  467. err = ubifs_budget_space(c, &req);
  468. if (err)
  469. return err;
  470. lock_2_inodes(dir, inode);
  471. inc_nlink(inode);
  472. ihold(inode);
  473. inode->i_ctime = ubifs_current_time(inode);
  474. dir->i_size += sz_change;
  475. dir_ui->ui_size = dir->i_size;
  476. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  477. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  478. if (err)
  479. goto out_cancel;
  480. unlock_2_inodes(dir, inode);
  481. ubifs_release_budget(c, &req);
  482. d_instantiate(dentry, inode);
  483. return 0;
  484. out_cancel:
  485. dir->i_size -= sz_change;
  486. dir_ui->ui_size = dir->i_size;
  487. drop_nlink(inode);
  488. unlock_2_inodes(dir, inode);
  489. ubifs_release_budget(c, &req);
  490. iput(inode);
  491. return err;
  492. }
  493. static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
  494. {
  495. struct ubifs_info *c = dir->i_sb->s_fs_info;
  496. struct inode *inode = d_inode(dentry);
  497. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  498. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  499. int err, budgeted = 1;
  500. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  501. unsigned int saved_nlink = inode->i_nlink;
  502. /*
  503. * Budget request settings: deletion direntry, deletion inode (+1 for
  504. * @dirtied_ino), changing the parent directory inode. If budgeting
  505. * fails, go ahead anyway because we have extra space reserved for
  506. * deletions.
  507. */
  508. dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
  509. dentry, inode->i_ino,
  510. inode->i_nlink, dir->i_ino);
  511. ubifs_assert(mutex_is_locked(&dir->i_mutex));
  512. ubifs_assert(mutex_is_locked(&inode->i_mutex));
  513. err = dbg_check_synced_i_size(c, inode);
  514. if (err)
  515. return err;
  516. err = ubifs_budget_space(c, &req);
  517. if (err) {
  518. if (err != -ENOSPC)
  519. return err;
  520. budgeted = 0;
  521. }
  522. lock_2_inodes(dir, inode);
  523. inode->i_ctime = ubifs_current_time(dir);
  524. drop_nlink(inode);
  525. dir->i_size -= sz_change;
  526. dir_ui->ui_size = dir->i_size;
  527. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  528. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
  529. if (err)
  530. goto out_cancel;
  531. unlock_2_inodes(dir, inode);
  532. if (budgeted)
  533. ubifs_release_budget(c, &req);
  534. else {
  535. /* We've deleted something - clean the "no space" flags */
  536. c->bi.nospace = c->bi.nospace_rp = 0;
  537. smp_wmb();
  538. }
  539. return 0;
  540. out_cancel:
  541. dir->i_size += sz_change;
  542. dir_ui->ui_size = dir->i_size;
  543. set_nlink(inode, saved_nlink);
  544. unlock_2_inodes(dir, inode);
  545. if (budgeted)
  546. ubifs_release_budget(c, &req);
  547. return err;
  548. }
  549. /**
  550. * check_dir_empty - check if a directory is empty or not.
  551. * @c: UBIFS file-system description object
  552. * @dir: VFS inode object of the directory to check
  553. *
  554. * This function checks if directory @dir is empty. Returns zero if the
  555. * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
  556. * in case of of errors.
  557. */
  558. static int check_dir_empty(struct ubifs_info *c, struct inode *dir)
  559. {
  560. struct qstr nm = { .name = NULL };
  561. struct ubifs_dent_node *dent;
  562. union ubifs_key key;
  563. int err;
  564. lowest_dent_key(c, &key, dir->i_ino);
  565. dent = ubifs_tnc_next_ent(c, &key, &nm);
  566. if (IS_ERR(dent)) {
  567. err = PTR_ERR(dent);
  568. if (err == -ENOENT)
  569. err = 0;
  570. } else {
  571. kfree(dent);
  572. err = -ENOTEMPTY;
  573. }
  574. return err;
  575. }
  576. static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
  577. {
  578. struct ubifs_info *c = dir->i_sb->s_fs_info;
  579. struct inode *inode = d_inode(dentry);
  580. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  581. int err, budgeted = 1;
  582. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  583. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  584. /*
  585. * Budget request settings: deletion direntry, deletion inode and
  586. * changing the parent inode. If budgeting fails, go ahead anyway
  587. * because we have extra space reserved for deletions.
  588. */
  589. dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
  590. inode->i_ino, dir->i_ino);
  591. ubifs_assert(mutex_is_locked(&dir->i_mutex));
  592. ubifs_assert(mutex_is_locked(&inode->i_mutex));
  593. err = check_dir_empty(c, d_inode(dentry));
  594. if (err)
  595. return err;
  596. err = ubifs_budget_space(c, &req);
  597. if (err) {
  598. if (err != -ENOSPC)
  599. return err;
  600. budgeted = 0;
  601. }
  602. lock_2_inodes(dir, inode);
  603. inode->i_ctime = ubifs_current_time(dir);
  604. clear_nlink(inode);
  605. drop_nlink(dir);
  606. dir->i_size -= sz_change;
  607. dir_ui->ui_size = dir->i_size;
  608. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  609. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
  610. if (err)
  611. goto out_cancel;
  612. unlock_2_inodes(dir, inode);
  613. if (budgeted)
  614. ubifs_release_budget(c, &req);
  615. else {
  616. /* We've deleted something - clean the "no space" flags */
  617. c->bi.nospace = c->bi.nospace_rp = 0;
  618. smp_wmb();
  619. }
  620. return 0;
  621. out_cancel:
  622. dir->i_size += sz_change;
  623. dir_ui->ui_size = dir->i_size;
  624. inc_nlink(dir);
  625. set_nlink(inode, 2);
  626. unlock_2_inodes(dir, inode);
  627. if (budgeted)
  628. ubifs_release_budget(c, &req);
  629. return err;
  630. }
  631. static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  632. {
  633. struct inode *inode;
  634. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  635. struct ubifs_info *c = dir->i_sb->s_fs_info;
  636. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  637. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
  638. /*
  639. * Budget request settings: new inode, new direntry and changing parent
  640. * directory inode.
  641. */
  642. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  643. dentry, mode, dir->i_ino);
  644. err = ubifs_budget_space(c, &req);
  645. if (err)
  646. return err;
  647. inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
  648. if (IS_ERR(inode)) {
  649. err = PTR_ERR(inode);
  650. goto out_budg;
  651. }
  652. err = ubifs_init_security(dir, inode, &dentry->d_name);
  653. if (err)
  654. goto out_inode;
  655. mutex_lock(&dir_ui->ui_mutex);
  656. insert_inode_hash(inode);
  657. inc_nlink(inode);
  658. inc_nlink(dir);
  659. dir->i_size += sz_change;
  660. dir_ui->ui_size = dir->i_size;
  661. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  662. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  663. if (err) {
  664. ubifs_err(c, "cannot create directory, error %d", err);
  665. goto out_cancel;
  666. }
  667. mutex_unlock(&dir_ui->ui_mutex);
  668. ubifs_release_budget(c, &req);
  669. d_instantiate(dentry, inode);
  670. return 0;
  671. out_cancel:
  672. dir->i_size -= sz_change;
  673. dir_ui->ui_size = dir->i_size;
  674. drop_nlink(dir);
  675. mutex_unlock(&dir_ui->ui_mutex);
  676. out_inode:
  677. make_bad_inode(inode);
  678. iput(inode);
  679. out_budg:
  680. ubifs_release_budget(c, &req);
  681. return err;
  682. }
  683. static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
  684. umode_t mode, dev_t rdev)
  685. {
  686. struct inode *inode;
  687. struct ubifs_inode *ui;
  688. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  689. struct ubifs_info *c = dir->i_sb->s_fs_info;
  690. union ubifs_dev_desc *dev = NULL;
  691. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  692. int err, devlen = 0;
  693. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  694. .new_ino_d = ALIGN(devlen, 8),
  695. .dirtied_ino = 1 };
  696. /*
  697. * Budget request settings: new inode, new direntry and changing parent
  698. * directory inode.
  699. */
  700. dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
  701. if (!new_valid_dev(rdev))
  702. return -EINVAL;
  703. if (S_ISBLK(mode) || S_ISCHR(mode)) {
  704. dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  705. if (!dev)
  706. return -ENOMEM;
  707. devlen = ubifs_encode_dev(dev, rdev);
  708. }
  709. err = ubifs_budget_space(c, &req);
  710. if (err) {
  711. kfree(dev);
  712. return err;
  713. }
  714. inode = ubifs_new_inode(c, dir, mode);
  715. if (IS_ERR(inode)) {
  716. kfree(dev);
  717. err = PTR_ERR(inode);
  718. goto out_budg;
  719. }
  720. init_special_inode(inode, inode->i_mode, rdev);
  721. inode->i_size = ubifs_inode(inode)->ui_size = devlen;
  722. ui = ubifs_inode(inode);
  723. ui->data = dev;
  724. ui->data_len = devlen;
  725. err = ubifs_init_security(dir, inode, &dentry->d_name);
  726. if (err)
  727. goto out_inode;
  728. mutex_lock(&dir_ui->ui_mutex);
  729. dir->i_size += sz_change;
  730. dir_ui->ui_size = dir->i_size;
  731. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  732. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  733. if (err)
  734. goto out_cancel;
  735. mutex_unlock(&dir_ui->ui_mutex);
  736. ubifs_release_budget(c, &req);
  737. insert_inode_hash(inode);
  738. d_instantiate(dentry, inode);
  739. return 0;
  740. out_cancel:
  741. dir->i_size -= sz_change;
  742. dir_ui->ui_size = dir->i_size;
  743. mutex_unlock(&dir_ui->ui_mutex);
  744. out_inode:
  745. make_bad_inode(inode);
  746. iput(inode);
  747. out_budg:
  748. ubifs_release_budget(c, &req);
  749. return err;
  750. }
  751. static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
  752. const char *symname)
  753. {
  754. struct inode *inode;
  755. struct ubifs_inode *ui;
  756. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  757. struct ubifs_info *c = dir->i_sb->s_fs_info;
  758. int err, len = strlen(symname);
  759. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  760. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  761. .new_ino_d = ALIGN(len, 8),
  762. .dirtied_ino = 1 };
  763. /*
  764. * Budget request settings: new inode, new direntry and changing parent
  765. * directory inode.
  766. */
  767. dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
  768. symname, dir->i_ino);
  769. if (len > UBIFS_MAX_INO_DATA)
  770. return -ENAMETOOLONG;
  771. err = ubifs_budget_space(c, &req);
  772. if (err)
  773. return err;
  774. inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
  775. if (IS_ERR(inode)) {
  776. err = PTR_ERR(inode);
  777. goto out_budg;
  778. }
  779. ui = ubifs_inode(inode);
  780. ui->data = kmalloc(len + 1, GFP_NOFS);
  781. if (!ui->data) {
  782. err = -ENOMEM;
  783. goto out_inode;
  784. }
  785. memcpy(ui->data, symname, len);
  786. ((char *)ui->data)[len] = '\0';
  787. inode->i_link = ui->data;
  788. /*
  789. * The terminating zero byte is not written to the flash media and it
  790. * is put just to make later in-memory string processing simpler. Thus,
  791. * data length is @len, not @len + %1.
  792. */
  793. ui->data_len = len;
  794. inode->i_size = ubifs_inode(inode)->ui_size = len;
  795. err = ubifs_init_security(dir, inode, &dentry->d_name);
  796. if (err)
  797. goto out_inode;
  798. mutex_lock(&dir_ui->ui_mutex);
  799. dir->i_size += sz_change;
  800. dir_ui->ui_size = dir->i_size;
  801. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  802. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  803. if (err)
  804. goto out_cancel;
  805. mutex_unlock(&dir_ui->ui_mutex);
  806. ubifs_release_budget(c, &req);
  807. insert_inode_hash(inode);
  808. d_instantiate(dentry, inode);
  809. return 0;
  810. out_cancel:
  811. dir->i_size -= sz_change;
  812. dir_ui->ui_size = dir->i_size;
  813. mutex_unlock(&dir_ui->ui_mutex);
  814. out_inode:
  815. make_bad_inode(inode);
  816. iput(inode);
  817. out_budg:
  818. ubifs_release_budget(c, &req);
  819. return err;
  820. }
  821. /**
  822. * lock_3_inodes - a wrapper for locking three UBIFS inodes.
  823. * @inode1: first inode
  824. * @inode2: second inode
  825. * @inode3: third inode
  826. *
  827. * This function is used for 'ubifs_rename()' and @inode1 may be the same as
  828. * @inode2 whereas @inode3 may be %NULL.
  829. *
  830. * We do not implement any tricks to guarantee strict lock ordering, because
  831. * VFS has already done it for us on the @i_mutex. So this is just a simple
  832. * wrapper function.
  833. */
  834. static void lock_3_inodes(struct inode *inode1, struct inode *inode2,
  835. struct inode *inode3)
  836. {
  837. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  838. if (inode2 != inode1)
  839. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  840. if (inode3)
  841. mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
  842. }
  843. /**
  844. * unlock_3_inodes - a wrapper for unlocking three UBIFS inodes for rename.
  845. * @inode1: first inode
  846. * @inode2: second inode
  847. * @inode3: third inode
  848. */
  849. static void unlock_3_inodes(struct inode *inode1, struct inode *inode2,
  850. struct inode *inode3)
  851. {
  852. if (inode3)
  853. mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
  854. if (inode1 != inode2)
  855. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  856. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  857. }
  858. static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
  859. struct inode *new_dir, struct dentry *new_dentry)
  860. {
  861. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  862. struct inode *old_inode = d_inode(old_dentry);
  863. struct inode *new_inode = d_inode(new_dentry);
  864. struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
  865. int err, release, sync = 0, move = (new_dir != old_dir);
  866. int is_dir = S_ISDIR(old_inode->i_mode);
  867. int unlink = !!new_inode;
  868. int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
  869. int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
  870. struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  871. .dirtied_ino = 3 };
  872. struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
  873. .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
  874. struct timespec time;
  875. unsigned int uninitialized_var(saved_nlink);
  876. /*
  877. * Budget request settings: deletion direntry, new direntry, removing
  878. * the old inode, and changing old and new parent directory inodes.
  879. *
  880. * However, this operation also marks the target inode as dirty and
  881. * does not write it, so we allocate budget for the target inode
  882. * separately.
  883. */
  884. dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu",
  885. old_dentry, old_inode->i_ino, old_dir->i_ino,
  886. new_dentry, new_dir->i_ino);
  887. ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
  888. ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
  889. if (unlink)
  890. ubifs_assert(mutex_is_locked(&new_inode->i_mutex));
  891. if (unlink && is_dir) {
  892. err = check_dir_empty(c, new_inode);
  893. if (err)
  894. return err;
  895. }
  896. err = ubifs_budget_space(c, &req);
  897. if (err)
  898. return err;
  899. err = ubifs_budget_space(c, &ino_req);
  900. if (err) {
  901. ubifs_release_budget(c, &req);
  902. return err;
  903. }
  904. lock_3_inodes(old_dir, new_dir, new_inode);
  905. /*
  906. * Like most other Unix systems, set the @i_ctime for inodes on a
  907. * rename.
  908. */
  909. time = ubifs_current_time(old_dir);
  910. old_inode->i_ctime = time;
  911. /* We must adjust parent link count when renaming directories */
  912. if (is_dir) {
  913. if (move) {
  914. /*
  915. * @old_dir loses a link because we are moving
  916. * @old_inode to a different directory.
  917. */
  918. drop_nlink(old_dir);
  919. /*
  920. * @new_dir only gains a link if we are not also
  921. * overwriting an existing directory.
  922. */
  923. if (!unlink)
  924. inc_nlink(new_dir);
  925. } else {
  926. /*
  927. * @old_inode is not moving to a different directory,
  928. * but @old_dir still loses a link if we are
  929. * overwriting an existing directory.
  930. */
  931. if (unlink)
  932. drop_nlink(old_dir);
  933. }
  934. }
  935. old_dir->i_size -= old_sz;
  936. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  937. old_dir->i_mtime = old_dir->i_ctime = time;
  938. new_dir->i_mtime = new_dir->i_ctime = time;
  939. /*
  940. * And finally, if we unlinked a direntry which happened to have the
  941. * same name as the moved direntry, we have to decrement @i_nlink of
  942. * the unlinked inode and change its ctime.
  943. */
  944. if (unlink) {
  945. /*
  946. * Directories cannot have hard-links, so if this is a
  947. * directory, just clear @i_nlink.
  948. */
  949. saved_nlink = new_inode->i_nlink;
  950. if (is_dir)
  951. clear_nlink(new_inode);
  952. else
  953. drop_nlink(new_inode);
  954. new_inode->i_ctime = time;
  955. } else {
  956. new_dir->i_size += new_sz;
  957. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  958. }
  959. /*
  960. * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
  961. * is dirty, because this will be done later on at the end of
  962. * 'ubifs_rename()'.
  963. */
  964. if (IS_SYNC(old_inode)) {
  965. sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  966. if (unlink && IS_SYNC(new_inode))
  967. sync = 1;
  968. }
  969. err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry,
  970. sync);
  971. if (err)
  972. goto out_cancel;
  973. unlock_3_inodes(old_dir, new_dir, new_inode);
  974. ubifs_release_budget(c, &req);
  975. mutex_lock(&old_inode_ui->ui_mutex);
  976. release = old_inode_ui->dirty;
  977. mark_inode_dirty_sync(old_inode);
  978. mutex_unlock(&old_inode_ui->ui_mutex);
  979. if (release)
  980. ubifs_release_budget(c, &ino_req);
  981. if (IS_SYNC(old_inode))
  982. err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
  983. return err;
  984. out_cancel:
  985. if (unlink) {
  986. set_nlink(new_inode, saved_nlink);
  987. } else {
  988. new_dir->i_size -= new_sz;
  989. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  990. }
  991. old_dir->i_size += old_sz;
  992. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  993. if (is_dir) {
  994. if (move) {
  995. inc_nlink(old_dir);
  996. if (!unlink)
  997. drop_nlink(new_dir);
  998. } else {
  999. if (unlink)
  1000. inc_nlink(old_dir);
  1001. }
  1002. }
  1003. unlock_3_inodes(old_dir, new_dir, new_inode);
  1004. ubifs_release_budget(c, &ino_req);
  1005. ubifs_release_budget(c, &req);
  1006. return err;
  1007. }
  1008. int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1009. struct kstat *stat)
  1010. {
  1011. loff_t size;
  1012. struct inode *inode = d_inode(dentry);
  1013. struct ubifs_inode *ui = ubifs_inode(inode);
  1014. mutex_lock(&ui->ui_mutex);
  1015. generic_fillattr(inode, stat);
  1016. stat->blksize = UBIFS_BLOCK_SIZE;
  1017. stat->size = ui->ui_size;
  1018. /*
  1019. * Unfortunately, the 'stat()' system call was designed for block
  1020. * device based file systems, and it is not appropriate for UBIFS,
  1021. * because UBIFS does not have notion of "block". For example, it is
  1022. * difficult to tell how many block a directory takes - it actually
  1023. * takes less than 300 bytes, but we have to round it to block size,
  1024. * which introduces large mistake. This makes utilities like 'du' to
  1025. * report completely senseless numbers. This is the reason why UBIFS
  1026. * goes the same way as JFFS2 - it reports zero blocks for everything
  1027. * but regular files, which makes more sense than reporting completely
  1028. * wrong sizes.
  1029. */
  1030. if (S_ISREG(inode->i_mode)) {
  1031. size = ui->xattr_size;
  1032. size += stat->size;
  1033. size = ALIGN(size, UBIFS_BLOCK_SIZE);
  1034. /*
  1035. * Note, user-space expects 512-byte blocks count irrespectively
  1036. * of what was reported in @stat->size.
  1037. */
  1038. stat->blocks = size >> 9;
  1039. } else
  1040. stat->blocks = 0;
  1041. mutex_unlock(&ui->ui_mutex);
  1042. return 0;
  1043. }
  1044. const struct inode_operations ubifs_dir_inode_operations = {
  1045. .lookup = ubifs_lookup,
  1046. .create = ubifs_create,
  1047. .link = ubifs_link,
  1048. .symlink = ubifs_symlink,
  1049. .unlink = ubifs_unlink,
  1050. .mkdir = ubifs_mkdir,
  1051. .rmdir = ubifs_rmdir,
  1052. .mknod = ubifs_mknod,
  1053. .rename = ubifs_rename,
  1054. .setattr = ubifs_setattr,
  1055. .getattr = ubifs_getattr,
  1056. .setxattr = ubifs_setxattr,
  1057. .getxattr = ubifs_getxattr,
  1058. .listxattr = ubifs_listxattr,
  1059. .removexattr = ubifs_removexattr,
  1060. };
  1061. const struct file_operations ubifs_dir_operations = {
  1062. .llseek = generic_file_llseek,
  1063. .release = ubifs_dir_release,
  1064. .read = generic_read_dir,
  1065. .iterate = ubifs_readdir,
  1066. .fsync = ubifs_fsync,
  1067. .unlocked_ioctl = ubifs_ioctl,
  1068. #ifdef CONFIG_COMPAT
  1069. .compat_ioctl = ubifs_compat_ioctl,
  1070. #endif
  1071. };