dir.c 32 KB

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