dir.c 32 KB

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