dir.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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, struct inode *dir,
  83. umode_t mode)
  84. {
  85. int err;
  86. struct inode *inode;
  87. struct ubifs_inode *ui;
  88. bool encrypted = false;
  89. if (ubifs_crypt_is_encrypted(dir)) {
  90. err = fscrypt_get_encryption_info(dir);
  91. if (err) {
  92. ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err);
  93. return ERR_PTR(err);
  94. }
  95. if (!fscrypt_has_encryption_key(dir))
  96. return ERR_PTR(-EPERM);
  97. encrypted = true;
  98. }
  99. inode = new_inode(c->vfs_sb);
  100. ui = ubifs_inode(inode);
  101. if (!inode)
  102. return ERR_PTR(-ENOMEM);
  103. /*
  104. * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
  105. * marking them dirty in file write path (see 'file_update_time()').
  106. * UBIFS has to fully control "clean <-> dirty" transitions of inodes
  107. * to make budgeting work.
  108. */
  109. inode->i_flags |= S_NOCMTIME;
  110. inode_init_owner(inode, dir, mode);
  111. inode->i_mtime = inode->i_atime = inode->i_ctime =
  112. ubifs_current_time(inode);
  113. inode->i_mapping->nrpages = 0;
  114. switch (mode & S_IFMT) {
  115. case S_IFREG:
  116. inode->i_mapping->a_ops = &ubifs_file_address_operations;
  117. inode->i_op = &ubifs_file_inode_operations;
  118. inode->i_fop = &ubifs_file_operations;
  119. break;
  120. case S_IFDIR:
  121. inode->i_op = &ubifs_dir_inode_operations;
  122. inode->i_fop = &ubifs_dir_operations;
  123. inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
  124. break;
  125. case S_IFLNK:
  126. inode->i_op = &ubifs_symlink_inode_operations;
  127. break;
  128. case S_IFSOCK:
  129. case S_IFIFO:
  130. case S_IFBLK:
  131. case S_IFCHR:
  132. inode->i_op = &ubifs_file_inode_operations;
  133. break;
  134. default:
  135. BUG();
  136. }
  137. ui->flags = inherit_flags(dir, mode);
  138. ubifs_set_inode_flags(inode);
  139. if (S_ISREG(mode))
  140. ui->compr_type = c->default_compr;
  141. else
  142. ui->compr_type = UBIFS_COMPR_NONE;
  143. ui->synced_i_size = 0;
  144. spin_lock(&c->cnt_lock);
  145. /* Inode number overflow is currently not supported */
  146. if (c->highest_inum >= INUM_WARN_WATERMARK) {
  147. if (c->highest_inum >= INUM_WATERMARK) {
  148. spin_unlock(&c->cnt_lock);
  149. ubifs_err(c, "out of inode numbers");
  150. make_bad_inode(inode);
  151. iput(inode);
  152. return ERR_PTR(-EINVAL);
  153. }
  154. ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
  155. (unsigned long)c->highest_inum, INUM_WATERMARK);
  156. }
  157. inode->i_ino = ++c->highest_inum;
  158. /*
  159. * The creation sequence number remains with this inode for its
  160. * lifetime. All nodes for this inode have a greater sequence number,
  161. * and so it is possible to distinguish obsolete nodes belonging to a
  162. * previous incarnation of the same inode number - for example, for the
  163. * purpose of rebuilding the index.
  164. */
  165. ui->creat_sqnum = ++c->max_sqnum;
  166. spin_unlock(&c->cnt_lock);
  167. if (encrypted) {
  168. err = fscrypt_inherit_context(dir, inode, &encrypted, true);
  169. if (err) {
  170. ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
  171. make_bad_inode(inode);
  172. iput(inode);
  173. return ERR_PTR(err);
  174. }
  175. }
  176. return inode;
  177. }
  178. static int dbg_check_name(const struct ubifs_info *c,
  179. const struct ubifs_dent_node *dent,
  180. const struct fscrypt_name *nm)
  181. {
  182. if (!dbg_is_chk_gen(c))
  183. return 0;
  184. if (le16_to_cpu(dent->nlen) != fname_len(nm))
  185. return -EINVAL;
  186. if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
  187. return -EINVAL;
  188. return 0;
  189. }
  190. static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
  191. unsigned int flags)
  192. {
  193. int err;
  194. union ubifs_key key;
  195. struct inode *inode = NULL;
  196. struct ubifs_dent_node *dent;
  197. struct ubifs_info *c = dir->i_sb->s_fs_info;
  198. struct fscrypt_name nm;
  199. dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
  200. if (ubifs_crypt_is_encrypted(dir)) {
  201. err = fscrypt_get_encryption_info(dir);
  202. /*
  203. * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
  204. * created while the directory was encrypted and we
  205. * have access to the key.
  206. */
  207. if (fscrypt_has_encryption_key(dir))
  208. fscrypt_set_encrypted_dentry(dentry);
  209. fscrypt_set_d_op(dentry);
  210. if (err && err != -ENOKEY)
  211. return ERR_PTR(err);
  212. }
  213. err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
  214. if (err)
  215. return ERR_PTR(err);
  216. if (fname_len(&nm) > UBIFS_MAX_NLEN) {
  217. err = -ENAMETOOLONG;
  218. goto out_fname;
  219. }
  220. dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  221. if (!dent) {
  222. err = -ENOMEM;
  223. goto out_fname;
  224. }
  225. if (nm.hash) {
  226. ubifs_assert(fname_len(&nm) == 0);
  227. ubifs_assert(fname_name(&nm) == NULL);
  228. dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
  229. err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
  230. } else {
  231. dent_key_init(c, &key, dir->i_ino, &nm);
  232. err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
  233. }
  234. if (err) {
  235. if (err == -ENOENT) {
  236. dbg_gen("not found");
  237. goto done;
  238. }
  239. goto out_dent;
  240. }
  241. if (dbg_check_name(c, dent, &nm)) {
  242. err = -EINVAL;
  243. goto out_dent;
  244. }
  245. inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
  246. if (IS_ERR(inode)) {
  247. /*
  248. * This should not happen. Probably the file-system needs
  249. * checking.
  250. */
  251. err = PTR_ERR(inode);
  252. ubifs_err(c, "dead directory entry '%pd', error %d",
  253. dentry, err);
  254. ubifs_ro_mode(c, err);
  255. goto out_dent;
  256. }
  257. done:
  258. kfree(dent);
  259. fscrypt_free_filename(&nm);
  260. /*
  261. * Note, d_splice_alias() would be required instead if we supported
  262. * NFS.
  263. */
  264. d_add(dentry, inode);
  265. return NULL;
  266. out_dent:
  267. kfree(dent);
  268. out_fname:
  269. fscrypt_free_filename(&nm);
  270. return ERR_PTR(err);
  271. }
  272. static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  273. bool excl)
  274. {
  275. struct inode *inode;
  276. struct ubifs_info *c = dir->i_sb->s_fs_info;
  277. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  278. .dirtied_ino = 1 };
  279. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  280. struct fscrypt_name nm;
  281. int err, sz_change;
  282. /*
  283. * Budget request settings: new inode, new direntry, changing the
  284. * parent directory inode.
  285. */
  286. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  287. dentry, mode, dir->i_ino);
  288. err = ubifs_budget_space(c, &req);
  289. if (err)
  290. return err;
  291. err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
  292. if (err)
  293. goto out_budg;
  294. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  295. inode = ubifs_new_inode(c, dir, mode);
  296. if (IS_ERR(inode)) {
  297. err = PTR_ERR(inode);
  298. goto out_fname;
  299. }
  300. err = ubifs_init_security(dir, inode, &dentry->d_name);
  301. if (err)
  302. goto out_inode;
  303. mutex_lock(&dir_ui->ui_mutex);
  304. dir->i_size += sz_change;
  305. dir_ui->ui_size = dir->i_size;
  306. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  307. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  308. if (err)
  309. goto out_cancel;
  310. mutex_unlock(&dir_ui->ui_mutex);
  311. ubifs_release_budget(c, &req);
  312. fscrypt_free_filename(&nm);
  313. insert_inode_hash(inode);
  314. d_instantiate(dentry, inode);
  315. return 0;
  316. out_cancel:
  317. dir->i_size -= sz_change;
  318. dir_ui->ui_size = dir->i_size;
  319. mutex_unlock(&dir_ui->ui_mutex);
  320. out_inode:
  321. make_bad_inode(inode);
  322. iput(inode);
  323. out_fname:
  324. fscrypt_free_filename(&nm);
  325. out_budg:
  326. ubifs_release_budget(c, &req);
  327. ubifs_err(c, "cannot create regular file, error %d", err);
  328. return err;
  329. }
  330. static int do_tmpfile(struct inode *dir, struct dentry *dentry,
  331. umode_t mode, struct inode **whiteout)
  332. {
  333. struct inode *inode;
  334. struct ubifs_info *c = dir->i_sb->s_fs_info;
  335. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
  336. struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
  337. struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
  338. int err, instantiated = 0;
  339. struct fscrypt_name nm;
  340. /*
  341. * Budget request settings: new dirty inode, new direntry,
  342. * budget for dirtied inode will be released via writeback.
  343. */
  344. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  345. dentry, mode, dir->i_ino);
  346. err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
  347. if (err)
  348. return err;
  349. err = ubifs_budget_space(c, &req);
  350. if (err) {
  351. fscrypt_free_filename(&nm);
  352. return err;
  353. }
  354. err = ubifs_budget_space(c, &ino_req);
  355. if (err) {
  356. ubifs_release_budget(c, &req);
  357. fscrypt_free_filename(&nm);
  358. return err;
  359. }
  360. inode = ubifs_new_inode(c, dir, mode);
  361. if (IS_ERR(inode)) {
  362. err = PTR_ERR(inode);
  363. goto out_budg;
  364. }
  365. ui = ubifs_inode(inode);
  366. if (whiteout) {
  367. init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
  368. ubifs_assert(inode->i_op == &ubifs_file_inode_operations);
  369. }
  370. err = ubifs_init_security(dir, inode, &dentry->d_name);
  371. if (err)
  372. goto out_inode;
  373. mutex_lock(&ui->ui_mutex);
  374. insert_inode_hash(inode);
  375. if (whiteout) {
  376. mark_inode_dirty(inode);
  377. drop_nlink(inode);
  378. *whiteout = inode;
  379. } else {
  380. d_tmpfile(dentry, inode);
  381. }
  382. ubifs_assert(ui->dirty);
  383. instantiated = 1;
  384. mutex_unlock(&ui->ui_mutex);
  385. mutex_lock(&dir_ui->ui_mutex);
  386. err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
  387. if (err)
  388. goto out_cancel;
  389. mutex_unlock(&dir_ui->ui_mutex);
  390. ubifs_release_budget(c, &req);
  391. return 0;
  392. out_cancel:
  393. mutex_unlock(&dir_ui->ui_mutex);
  394. out_inode:
  395. make_bad_inode(inode);
  396. if (!instantiated)
  397. iput(inode);
  398. out_budg:
  399. ubifs_release_budget(c, &req);
  400. if (!instantiated)
  401. ubifs_release_budget(c, &ino_req);
  402. fscrypt_free_filename(&nm);
  403. ubifs_err(c, "cannot create temporary file, error %d", err);
  404. return err;
  405. }
  406. static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
  407. umode_t mode)
  408. {
  409. return do_tmpfile(dir, dentry, mode, NULL);
  410. }
  411. /**
  412. * vfs_dent_type - get VFS directory entry type.
  413. * @type: UBIFS directory entry type
  414. *
  415. * This function converts UBIFS directory entry type into VFS directory entry
  416. * type.
  417. */
  418. static unsigned int vfs_dent_type(uint8_t type)
  419. {
  420. switch (type) {
  421. case UBIFS_ITYPE_REG:
  422. return DT_REG;
  423. case UBIFS_ITYPE_DIR:
  424. return DT_DIR;
  425. case UBIFS_ITYPE_LNK:
  426. return DT_LNK;
  427. case UBIFS_ITYPE_BLK:
  428. return DT_BLK;
  429. case UBIFS_ITYPE_CHR:
  430. return DT_CHR;
  431. case UBIFS_ITYPE_FIFO:
  432. return DT_FIFO;
  433. case UBIFS_ITYPE_SOCK:
  434. return DT_SOCK;
  435. default:
  436. BUG();
  437. }
  438. return 0;
  439. }
  440. /*
  441. * The classical Unix view for directory is that it is a linear array of
  442. * (name, inode number) entries. Linux/VFS assumes this model as well.
  443. * Particularly, 'readdir()' call wants us to return a directory entry offset
  444. * which later may be used to continue 'readdir()'ing the directory or to
  445. * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
  446. * model because directory entries are identified by keys, which may collide.
  447. *
  448. * UBIFS uses directory entry hash value for directory offsets, so
  449. * 'seekdir()'/'telldir()' may not always work because of possible key
  450. * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
  451. * properly by means of saving full directory entry name in the private field
  452. * of the file description object.
  453. *
  454. * This means that UBIFS cannot support NFS which requires full
  455. * 'seekdir()'/'telldir()' support.
  456. */
  457. static int ubifs_readdir(struct file *file, struct dir_context *ctx)
  458. {
  459. int fstr_real_len = 0, err = 0;
  460. struct fscrypt_name nm;
  461. struct fscrypt_str fstr = {0};
  462. union ubifs_key key;
  463. struct ubifs_dent_node *dent;
  464. struct inode *dir = file_inode(file);
  465. struct ubifs_info *c = dir->i_sb->s_fs_info;
  466. bool encrypted = ubifs_crypt_is_encrypted(dir);
  467. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
  468. if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
  469. /*
  470. * The directory was seek'ed to a senseless position or there
  471. * are no more entries.
  472. */
  473. return 0;
  474. if (encrypted) {
  475. err = fscrypt_get_encryption_info(dir);
  476. if (err && err != -ENOKEY)
  477. return err;
  478. err = fscrypt_fname_alloc_buffer(dir, UBIFS_MAX_NLEN, &fstr);
  479. if (err)
  480. return err;
  481. fstr_real_len = fstr.len;
  482. }
  483. if (file->f_version == 0) {
  484. /*
  485. * The file was seek'ed, which means that @file->private_data
  486. * is now invalid. This may also be just the first
  487. * 'ubifs_readdir()' invocation, in which case
  488. * @file->private_data is NULL, and the below code is
  489. * basically a no-op.
  490. */
  491. kfree(file->private_data);
  492. file->private_data = NULL;
  493. }
  494. /*
  495. * 'generic_file_llseek()' unconditionally sets @file->f_version to
  496. * zero, and we use this for detecting whether the file was seek'ed.
  497. */
  498. file->f_version = 1;
  499. /* File positions 0 and 1 correspond to "." and ".." */
  500. if (ctx->pos < 2) {
  501. ubifs_assert(!file->private_data);
  502. if (!dir_emit_dots(file, ctx)) {
  503. if (encrypted)
  504. fscrypt_fname_free_buffer(&fstr);
  505. return 0;
  506. }
  507. /* Find the first entry in TNC and save it */
  508. lowest_dent_key(c, &key, dir->i_ino);
  509. fname_len(&nm) = 0;
  510. dent = ubifs_tnc_next_ent(c, &key, &nm);
  511. if (IS_ERR(dent)) {
  512. err = PTR_ERR(dent);
  513. goto out;
  514. }
  515. ctx->pos = key_hash_flash(c, &dent->key);
  516. file->private_data = dent;
  517. }
  518. dent = file->private_data;
  519. if (!dent) {
  520. /*
  521. * The directory was seek'ed to and is now readdir'ed.
  522. * Find the entry corresponding to @ctx->pos or the closest one.
  523. */
  524. dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
  525. fname_len(&nm) = 0;
  526. dent = ubifs_tnc_next_ent(c, &key, &nm);
  527. if (IS_ERR(dent)) {
  528. err = PTR_ERR(dent);
  529. goto out;
  530. }
  531. ctx->pos = key_hash_flash(c, &dent->key);
  532. file->private_data = dent;
  533. }
  534. while (1) {
  535. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  536. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  537. key_hash_flash(c, &dent->key));
  538. ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
  539. ubifs_inode(dir)->creat_sqnum);
  540. fname_len(&nm) = le16_to_cpu(dent->nlen);
  541. fname_name(&nm) = dent->name;
  542. if (encrypted) {
  543. fstr.len = fstr_real_len;
  544. err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
  545. &dent->key),
  546. le32_to_cpu(dent->cookie),
  547. &nm.disk_name, &fstr);
  548. if (err)
  549. goto out;
  550. } else {
  551. fstr.len = fname_len(&nm);
  552. fstr.name = fname_name(&nm);
  553. }
  554. if (!dir_emit(ctx, fstr.name, fstr.len,
  555. le64_to_cpu(dent->inum),
  556. vfs_dent_type(dent->type))) {
  557. if (encrypted)
  558. fscrypt_fname_free_buffer(&fstr);
  559. return 0;
  560. }
  561. /* Switch to the next entry */
  562. key_read(c, &dent->key, &key);
  563. dent = ubifs_tnc_next_ent(c, &key, &nm);
  564. if (IS_ERR(dent)) {
  565. err = PTR_ERR(dent);
  566. goto out;
  567. }
  568. kfree(file->private_data);
  569. ctx->pos = key_hash_flash(c, &dent->key);
  570. file->private_data = dent;
  571. cond_resched();
  572. }
  573. out:
  574. kfree(file->private_data);
  575. file->private_data = NULL;
  576. if (encrypted)
  577. fscrypt_fname_free_buffer(&fstr);
  578. if (err != -ENOENT)
  579. ubifs_err(c, "cannot find next direntry, error %d", err);
  580. else
  581. /*
  582. * -ENOENT is a non-fatal error in this context, the TNC uses
  583. * it to indicate that the cursor moved past the current directory
  584. * and readdir() has to stop.
  585. */
  586. err = 0;
  587. /* 2 is a special value indicating that there are no more direntries */
  588. ctx->pos = 2;
  589. return err;
  590. }
  591. /* Free saved readdir() state when the directory is closed */
  592. static int ubifs_dir_release(struct inode *dir, struct file *file)
  593. {
  594. kfree(file->private_data);
  595. file->private_data = NULL;
  596. return 0;
  597. }
  598. /**
  599. * lock_2_inodes - a wrapper for locking two UBIFS inodes.
  600. * @inode1: first inode
  601. * @inode2: second inode
  602. *
  603. * We do not implement any tricks to guarantee strict lock ordering, because
  604. * VFS has already done it for us on the @i_mutex. So this is just a simple
  605. * wrapper function.
  606. */
  607. static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
  608. {
  609. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  610. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  611. }
  612. /**
  613. * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
  614. * @inode1: first inode
  615. * @inode2: second inode
  616. */
  617. static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
  618. {
  619. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  620. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  621. }
  622. static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
  623. struct dentry *dentry)
  624. {
  625. struct ubifs_info *c = dir->i_sb->s_fs_info;
  626. struct inode *inode = d_inode(old_dentry);
  627. struct ubifs_inode *ui = ubifs_inode(inode);
  628. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  629. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  630. struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
  631. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  632. struct fscrypt_name nm;
  633. /*
  634. * Budget request settings: new direntry, changing the target inode,
  635. * changing the parent inode.
  636. */
  637. dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
  638. dentry, inode->i_ino,
  639. inode->i_nlink, dir->i_ino);
  640. ubifs_assert(inode_is_locked(dir));
  641. ubifs_assert(inode_is_locked(inode));
  642. if (ubifs_crypt_is_encrypted(dir) &&
  643. !fscrypt_has_permitted_context(dir, inode))
  644. return -EPERM;
  645. err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
  646. if (err)
  647. return err;
  648. err = dbg_check_synced_i_size(c, inode);
  649. if (err)
  650. goto out_fname;
  651. err = ubifs_budget_space(c, &req);
  652. if (err)
  653. goto out_fname;
  654. lock_2_inodes(dir, inode);
  655. inc_nlink(inode);
  656. ihold(inode);
  657. inode->i_ctime = ubifs_current_time(inode);
  658. dir->i_size += sz_change;
  659. dir_ui->ui_size = dir->i_size;
  660. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  661. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  662. if (err)
  663. goto out_cancel;
  664. unlock_2_inodes(dir, inode);
  665. ubifs_release_budget(c, &req);
  666. d_instantiate(dentry, inode);
  667. fscrypt_free_filename(&nm);
  668. return 0;
  669. out_cancel:
  670. dir->i_size -= sz_change;
  671. dir_ui->ui_size = dir->i_size;
  672. drop_nlink(inode);
  673. unlock_2_inodes(dir, inode);
  674. ubifs_release_budget(c, &req);
  675. iput(inode);
  676. out_fname:
  677. fscrypt_free_filename(&nm);
  678. return err;
  679. }
  680. static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
  681. {
  682. struct ubifs_info *c = dir->i_sb->s_fs_info;
  683. struct inode *inode = d_inode(dentry);
  684. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  685. int err, sz_change, budgeted = 1;
  686. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  687. unsigned int saved_nlink = inode->i_nlink;
  688. struct fscrypt_name nm;
  689. /*
  690. * Budget request settings: deletion direntry, deletion inode (+1 for
  691. * @dirtied_ino), changing the parent directory inode. If budgeting
  692. * fails, go ahead anyway because we have extra space reserved for
  693. * deletions.
  694. */
  695. dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
  696. dentry, inode->i_ino,
  697. inode->i_nlink, dir->i_ino);
  698. if (ubifs_crypt_is_encrypted(dir)) {
  699. err = fscrypt_get_encryption_info(dir);
  700. if (err && err != -ENOKEY)
  701. return err;
  702. }
  703. err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
  704. if (err)
  705. return err;
  706. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  707. ubifs_assert(inode_is_locked(dir));
  708. ubifs_assert(inode_is_locked(inode));
  709. err = dbg_check_synced_i_size(c, inode);
  710. if (err)
  711. goto out_fname;
  712. err = ubifs_budget_space(c, &req);
  713. if (err) {
  714. if (err != -ENOSPC)
  715. goto out_fname;
  716. budgeted = 0;
  717. }
  718. lock_2_inodes(dir, inode);
  719. inode->i_ctime = ubifs_current_time(dir);
  720. drop_nlink(inode);
  721. dir->i_size -= sz_change;
  722. dir_ui->ui_size = dir->i_size;
  723. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  724. err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
  725. if (err)
  726. goto out_cancel;
  727. unlock_2_inodes(dir, inode);
  728. if (budgeted)
  729. ubifs_release_budget(c, &req);
  730. else {
  731. /* We've deleted something - clean the "no space" flags */
  732. c->bi.nospace = c->bi.nospace_rp = 0;
  733. smp_wmb();
  734. }
  735. fscrypt_free_filename(&nm);
  736. return 0;
  737. out_cancel:
  738. dir->i_size += sz_change;
  739. dir_ui->ui_size = dir->i_size;
  740. set_nlink(inode, saved_nlink);
  741. unlock_2_inodes(dir, inode);
  742. if (budgeted)
  743. ubifs_release_budget(c, &req);
  744. out_fname:
  745. fscrypt_free_filename(&nm);
  746. return err;
  747. }
  748. /**
  749. * check_dir_empty - check if a directory is empty or not.
  750. * @dir: VFS inode object of the directory to check
  751. *
  752. * This function checks if directory @dir is empty. Returns zero if the
  753. * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
  754. * in case of of errors.
  755. */
  756. int ubifs_check_dir_empty(struct inode *dir)
  757. {
  758. struct ubifs_info *c = dir->i_sb->s_fs_info;
  759. struct fscrypt_name nm = { 0 };
  760. struct ubifs_dent_node *dent;
  761. union ubifs_key key;
  762. int err;
  763. lowest_dent_key(c, &key, dir->i_ino);
  764. dent = ubifs_tnc_next_ent(c, &key, &nm);
  765. if (IS_ERR(dent)) {
  766. err = PTR_ERR(dent);
  767. if (err == -ENOENT)
  768. err = 0;
  769. } else {
  770. kfree(dent);
  771. err = -ENOTEMPTY;
  772. }
  773. return err;
  774. }
  775. static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
  776. {
  777. struct ubifs_info *c = dir->i_sb->s_fs_info;
  778. struct inode *inode = d_inode(dentry);
  779. int err, sz_change, budgeted = 1;
  780. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  781. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  782. struct fscrypt_name nm;
  783. /*
  784. * Budget request settings: deletion direntry, deletion inode and
  785. * changing the parent inode. If budgeting fails, go ahead anyway
  786. * because we have extra space reserved for deletions.
  787. */
  788. dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
  789. inode->i_ino, dir->i_ino);
  790. ubifs_assert(inode_is_locked(dir));
  791. ubifs_assert(inode_is_locked(inode));
  792. err = ubifs_check_dir_empty(d_inode(dentry));
  793. if (err)
  794. return err;
  795. if (ubifs_crypt_is_encrypted(dir)) {
  796. err = fscrypt_get_encryption_info(dir);
  797. if (err && err != -ENOKEY)
  798. return err;
  799. }
  800. err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
  801. if (err)
  802. return err;
  803. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  804. err = ubifs_budget_space(c, &req);
  805. if (err) {
  806. if (err != -ENOSPC)
  807. goto out_fname;
  808. budgeted = 0;
  809. }
  810. lock_2_inodes(dir, inode);
  811. inode->i_ctime = ubifs_current_time(dir);
  812. clear_nlink(inode);
  813. drop_nlink(dir);
  814. dir->i_size -= sz_change;
  815. dir_ui->ui_size = dir->i_size;
  816. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  817. err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
  818. if (err)
  819. goto out_cancel;
  820. unlock_2_inodes(dir, inode);
  821. if (budgeted)
  822. ubifs_release_budget(c, &req);
  823. else {
  824. /* We've deleted something - clean the "no space" flags */
  825. c->bi.nospace = c->bi.nospace_rp = 0;
  826. smp_wmb();
  827. }
  828. fscrypt_free_filename(&nm);
  829. return 0;
  830. out_cancel:
  831. dir->i_size += sz_change;
  832. dir_ui->ui_size = dir->i_size;
  833. inc_nlink(dir);
  834. set_nlink(inode, 2);
  835. unlock_2_inodes(dir, inode);
  836. if (budgeted)
  837. ubifs_release_budget(c, &req);
  838. out_fname:
  839. fscrypt_free_filename(&nm);
  840. return err;
  841. }
  842. static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  843. {
  844. struct inode *inode;
  845. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  846. struct ubifs_info *c = dir->i_sb->s_fs_info;
  847. int err, sz_change;
  848. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
  849. struct fscrypt_name nm;
  850. /*
  851. * Budget request settings: new inode, new direntry and changing parent
  852. * directory inode.
  853. */
  854. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  855. dentry, mode, dir->i_ino);
  856. err = ubifs_budget_space(c, &req);
  857. if (err)
  858. return err;
  859. err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
  860. if (err)
  861. goto out_budg;
  862. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  863. inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
  864. if (IS_ERR(inode)) {
  865. err = PTR_ERR(inode);
  866. goto out_fname;
  867. }
  868. err = ubifs_init_security(dir, inode, &dentry->d_name);
  869. if (err)
  870. goto out_inode;
  871. mutex_lock(&dir_ui->ui_mutex);
  872. insert_inode_hash(inode);
  873. inc_nlink(inode);
  874. inc_nlink(dir);
  875. dir->i_size += sz_change;
  876. dir_ui->ui_size = dir->i_size;
  877. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  878. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  879. if (err) {
  880. ubifs_err(c, "cannot create directory, error %d", err);
  881. goto out_cancel;
  882. }
  883. mutex_unlock(&dir_ui->ui_mutex);
  884. ubifs_release_budget(c, &req);
  885. d_instantiate(dentry, inode);
  886. fscrypt_free_filename(&nm);
  887. return 0;
  888. out_cancel:
  889. dir->i_size -= sz_change;
  890. dir_ui->ui_size = dir->i_size;
  891. drop_nlink(dir);
  892. mutex_unlock(&dir_ui->ui_mutex);
  893. out_inode:
  894. make_bad_inode(inode);
  895. iput(inode);
  896. out_fname:
  897. fscrypt_free_filename(&nm);
  898. out_budg:
  899. ubifs_release_budget(c, &req);
  900. return err;
  901. }
  902. static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
  903. umode_t mode, dev_t rdev)
  904. {
  905. struct inode *inode;
  906. struct ubifs_inode *ui;
  907. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  908. struct ubifs_info *c = dir->i_sb->s_fs_info;
  909. union ubifs_dev_desc *dev = NULL;
  910. int sz_change;
  911. int err, devlen = 0;
  912. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  913. .new_ino_d = ALIGN(devlen, 8),
  914. .dirtied_ino = 1 };
  915. struct fscrypt_name nm;
  916. /*
  917. * Budget request settings: new inode, new direntry and changing parent
  918. * directory inode.
  919. */
  920. dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
  921. if (S_ISBLK(mode) || S_ISCHR(mode)) {
  922. dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  923. if (!dev)
  924. return -ENOMEM;
  925. devlen = ubifs_encode_dev(dev, rdev);
  926. }
  927. err = ubifs_budget_space(c, &req);
  928. if (err) {
  929. kfree(dev);
  930. return err;
  931. }
  932. err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
  933. if (err)
  934. goto out_budg;
  935. sz_change = CALC_DENT_SIZE(fname_len(&nm));
  936. inode = ubifs_new_inode(c, dir, mode);
  937. if (IS_ERR(inode)) {
  938. kfree(dev);
  939. err = PTR_ERR(inode);
  940. goto out_fname;
  941. }
  942. init_special_inode(inode, inode->i_mode, rdev);
  943. inode->i_size = ubifs_inode(inode)->ui_size = devlen;
  944. ui = ubifs_inode(inode);
  945. ui->data = dev;
  946. ui->data_len = devlen;
  947. err = ubifs_init_security(dir, inode, &dentry->d_name);
  948. if (err)
  949. goto out_inode;
  950. mutex_lock(&dir_ui->ui_mutex);
  951. dir->i_size += sz_change;
  952. dir_ui->ui_size = dir->i_size;
  953. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  954. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  955. if (err)
  956. goto out_cancel;
  957. mutex_unlock(&dir_ui->ui_mutex);
  958. ubifs_release_budget(c, &req);
  959. insert_inode_hash(inode);
  960. d_instantiate(dentry, inode);
  961. fscrypt_free_filename(&nm);
  962. return 0;
  963. out_cancel:
  964. dir->i_size -= sz_change;
  965. dir_ui->ui_size = dir->i_size;
  966. mutex_unlock(&dir_ui->ui_mutex);
  967. out_inode:
  968. make_bad_inode(inode);
  969. iput(inode);
  970. out_fname:
  971. fscrypt_free_filename(&nm);
  972. out_budg:
  973. ubifs_release_budget(c, &req);
  974. return err;
  975. }
  976. static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
  977. const char *symname)
  978. {
  979. struct inode *inode;
  980. struct ubifs_inode *ui;
  981. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  982. struct ubifs_info *c = dir->i_sb->s_fs_info;
  983. int err, len = strlen(symname);
  984. int sz_change = CALC_DENT_SIZE(len);
  985. struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
  986. struct fscrypt_symlink_data *sd = NULL;
  987. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  988. .new_ino_d = ALIGN(len, 8),
  989. .dirtied_ino = 1 };
  990. struct fscrypt_name nm;
  991. if (ubifs_crypt_is_encrypted(dir)) {
  992. err = fscrypt_get_encryption_info(dir);
  993. if (err)
  994. goto out_budg;
  995. if (!fscrypt_has_encryption_key(dir)) {
  996. err = -EPERM;
  997. goto out_budg;
  998. }
  999. disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
  1000. sizeof(struct fscrypt_symlink_data));
  1001. }
  1002. /*
  1003. * Budget request settings: new inode, new direntry and changing parent
  1004. * directory inode.
  1005. */
  1006. dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
  1007. symname, dir->i_ino);
  1008. if (disk_link.len > UBIFS_MAX_INO_DATA)
  1009. return -ENAMETOOLONG;
  1010. err = ubifs_budget_space(c, &req);
  1011. if (err)
  1012. return err;
  1013. err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
  1014. if (err)
  1015. goto out_budg;
  1016. inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
  1017. if (IS_ERR(inode)) {
  1018. err = PTR_ERR(inode);
  1019. goto out_fname;
  1020. }
  1021. ui = ubifs_inode(inode);
  1022. ui->data = kmalloc(disk_link.len, GFP_NOFS);
  1023. if (!ui->data) {
  1024. err = -ENOMEM;
  1025. goto out_inode;
  1026. }
  1027. if (ubifs_crypt_is_encrypted(dir)) {
  1028. struct qstr istr = QSTR_INIT(symname, len);
  1029. struct fscrypt_str ostr;
  1030. sd = kzalloc(disk_link.len, GFP_NOFS);
  1031. if (!sd) {
  1032. err = -ENOMEM;
  1033. goto out_inode;
  1034. }
  1035. ostr.name = sd->encrypted_path;
  1036. ostr.len = disk_link.len;
  1037. err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
  1038. if (err) {
  1039. kfree(sd);
  1040. goto out_inode;
  1041. }
  1042. sd->len = cpu_to_le16(ostr.len);
  1043. disk_link.name = (char *)sd;
  1044. } else {
  1045. inode->i_link = ui->data;
  1046. }
  1047. memcpy(ui->data, disk_link.name, disk_link.len);
  1048. ((char *)ui->data)[disk_link.len - 1] = '\0';
  1049. /*
  1050. * The terminating zero byte is not written to the flash media and it
  1051. * is put just to make later in-memory string processing simpler. Thus,
  1052. * data length is @len, not @len + %1.
  1053. */
  1054. ui->data_len = disk_link.len - 1;
  1055. inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
  1056. err = ubifs_init_security(dir, inode, &dentry->d_name);
  1057. if (err)
  1058. goto out_inode;
  1059. mutex_lock(&dir_ui->ui_mutex);
  1060. dir->i_size += sz_change;
  1061. dir_ui->ui_size = dir->i_size;
  1062. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  1063. err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
  1064. if (err)
  1065. goto out_cancel;
  1066. mutex_unlock(&dir_ui->ui_mutex);
  1067. ubifs_release_budget(c, &req);
  1068. insert_inode_hash(inode);
  1069. d_instantiate(dentry, inode);
  1070. fscrypt_free_filename(&nm);
  1071. return 0;
  1072. out_cancel:
  1073. dir->i_size -= sz_change;
  1074. dir_ui->ui_size = dir->i_size;
  1075. mutex_unlock(&dir_ui->ui_mutex);
  1076. out_inode:
  1077. make_bad_inode(inode);
  1078. iput(inode);
  1079. out_fname:
  1080. fscrypt_free_filename(&nm);
  1081. out_budg:
  1082. ubifs_release_budget(c, &req);
  1083. return err;
  1084. }
  1085. /**
  1086. * lock_4_inodes - a wrapper for locking three UBIFS inodes.
  1087. * @inode1: first inode
  1088. * @inode2: second inode
  1089. * @inode3: third inode
  1090. * @inode4: fouth inode
  1091. *
  1092. * This function is used for 'ubifs_rename()' and @inode1 may be the same as
  1093. * @inode2 whereas @inode3 and @inode4 may be %NULL.
  1094. *
  1095. * We do not implement any tricks to guarantee strict lock ordering, because
  1096. * VFS has already done it for us on the @i_mutex. So this is just a simple
  1097. * wrapper function.
  1098. */
  1099. static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
  1100. struct inode *inode3, struct inode *inode4)
  1101. {
  1102. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  1103. if (inode2 != inode1)
  1104. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  1105. if (inode3)
  1106. mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
  1107. if (inode4)
  1108. mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
  1109. }
  1110. /**
  1111. * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
  1112. * @inode1: first inode
  1113. * @inode2: second inode
  1114. * @inode3: third inode
  1115. * @inode4: fouth inode
  1116. */
  1117. static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
  1118. struct inode *inode3, struct inode *inode4)
  1119. {
  1120. if (inode4)
  1121. mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
  1122. if (inode3)
  1123. mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
  1124. if (inode1 != inode2)
  1125. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  1126. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  1127. }
  1128. static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
  1129. struct inode *new_dir, struct dentry *new_dentry,
  1130. unsigned int flags)
  1131. {
  1132. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  1133. struct inode *old_inode = d_inode(old_dentry);
  1134. struct inode *new_inode = d_inode(new_dentry);
  1135. struct inode *whiteout = NULL;
  1136. struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
  1137. struct ubifs_inode *whiteout_ui = NULL;
  1138. int err, release, sync = 0, move = (new_dir != old_dir);
  1139. int is_dir = S_ISDIR(old_inode->i_mode);
  1140. int unlink = !!new_inode, new_sz, old_sz;
  1141. struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  1142. .dirtied_ino = 3 };
  1143. struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
  1144. .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
  1145. struct timespec time;
  1146. unsigned int uninitialized_var(saved_nlink);
  1147. struct fscrypt_name old_nm, new_nm;
  1148. if (flags & ~RENAME_NOREPLACE)
  1149. return -EINVAL;
  1150. /*
  1151. * Budget request settings: deletion direntry, new direntry, removing
  1152. * the old inode, and changing old and new parent directory inodes.
  1153. *
  1154. * However, this operation also marks the target inode as dirty and
  1155. * does not write it, so we allocate budget for the target inode
  1156. * separately.
  1157. */
  1158. dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
  1159. old_dentry, old_inode->i_ino, old_dir->i_ino,
  1160. new_dentry, new_dir->i_ino, flags);
  1161. if (unlink)
  1162. ubifs_assert(inode_is_locked(new_inode));
  1163. if (old_dir != new_dir) {
  1164. if (ubifs_crypt_is_encrypted(new_dir) &&
  1165. !fscrypt_has_permitted_context(new_dir, old_inode))
  1166. return -EPERM;
  1167. }
  1168. if (unlink && is_dir) {
  1169. err = ubifs_check_dir_empty(new_inode);
  1170. if (err)
  1171. return err;
  1172. }
  1173. err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
  1174. if (err)
  1175. return err;
  1176. err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
  1177. if (err) {
  1178. fscrypt_free_filename(&old_nm);
  1179. return err;
  1180. }
  1181. new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
  1182. old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
  1183. err = ubifs_budget_space(c, &req);
  1184. if (err) {
  1185. fscrypt_free_filename(&old_nm);
  1186. fscrypt_free_filename(&new_nm);
  1187. return err;
  1188. }
  1189. err = ubifs_budget_space(c, &ino_req);
  1190. if (err) {
  1191. fscrypt_free_filename(&old_nm);
  1192. fscrypt_free_filename(&new_nm);
  1193. ubifs_release_budget(c, &req);
  1194. return err;
  1195. }
  1196. if (flags & RENAME_WHITEOUT) {
  1197. union ubifs_dev_desc *dev = NULL;
  1198. dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  1199. if (!dev) {
  1200. ubifs_release_budget(c, &req);
  1201. ubifs_release_budget(c, &ino_req);
  1202. return -ENOMEM;
  1203. }
  1204. err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
  1205. if (err) {
  1206. ubifs_release_budget(c, &req);
  1207. ubifs_release_budget(c, &ino_req);
  1208. kfree(dev);
  1209. return err;
  1210. }
  1211. whiteout->i_state |= I_LINKABLE;
  1212. whiteout_ui = ubifs_inode(whiteout);
  1213. whiteout_ui->data = dev;
  1214. whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
  1215. ubifs_assert(!whiteout_ui->dirty);
  1216. }
  1217. lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1218. /*
  1219. * Like most other Unix systems, set the @i_ctime for inodes on a
  1220. * rename.
  1221. */
  1222. time = ubifs_current_time(old_dir);
  1223. old_inode->i_ctime = time;
  1224. /* We must adjust parent link count when renaming directories */
  1225. if (is_dir) {
  1226. if (move) {
  1227. /*
  1228. * @old_dir loses a link because we are moving
  1229. * @old_inode to a different directory.
  1230. */
  1231. drop_nlink(old_dir);
  1232. /*
  1233. * @new_dir only gains a link if we are not also
  1234. * overwriting an existing directory.
  1235. */
  1236. if (!unlink)
  1237. inc_nlink(new_dir);
  1238. } else {
  1239. /*
  1240. * @old_inode is not moving to a different directory,
  1241. * but @old_dir still loses a link if we are
  1242. * overwriting an existing directory.
  1243. */
  1244. if (unlink)
  1245. drop_nlink(old_dir);
  1246. }
  1247. }
  1248. old_dir->i_size -= old_sz;
  1249. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  1250. old_dir->i_mtime = old_dir->i_ctime = time;
  1251. new_dir->i_mtime = new_dir->i_ctime = time;
  1252. /*
  1253. * And finally, if we unlinked a direntry which happened to have the
  1254. * same name as the moved direntry, we have to decrement @i_nlink of
  1255. * the unlinked inode and change its ctime.
  1256. */
  1257. if (unlink) {
  1258. /*
  1259. * Directories cannot have hard-links, so if this is a
  1260. * directory, just clear @i_nlink.
  1261. */
  1262. saved_nlink = new_inode->i_nlink;
  1263. if (is_dir)
  1264. clear_nlink(new_inode);
  1265. else
  1266. drop_nlink(new_inode);
  1267. new_inode->i_ctime = time;
  1268. } else {
  1269. new_dir->i_size += new_sz;
  1270. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  1271. }
  1272. /*
  1273. * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
  1274. * is dirty, because this will be done later on at the end of
  1275. * 'ubifs_rename()'.
  1276. */
  1277. if (IS_SYNC(old_inode)) {
  1278. sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  1279. if (unlink && IS_SYNC(new_inode))
  1280. sync = 1;
  1281. }
  1282. if (whiteout) {
  1283. struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
  1284. .dirtied_ino_d = \
  1285. ALIGN(ubifs_inode(whiteout)->data_len, 8) };
  1286. err = ubifs_budget_space(c, &wht_req);
  1287. if (err) {
  1288. ubifs_release_budget(c, &req);
  1289. ubifs_release_budget(c, &ino_req);
  1290. kfree(whiteout_ui->data);
  1291. whiteout_ui->data_len = 0;
  1292. iput(whiteout);
  1293. return err;
  1294. }
  1295. inc_nlink(whiteout);
  1296. mark_inode_dirty(whiteout);
  1297. whiteout->i_state &= ~I_LINKABLE;
  1298. iput(whiteout);
  1299. }
  1300. err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
  1301. new_inode, &new_nm, whiteout, sync);
  1302. if (err)
  1303. goto out_cancel;
  1304. unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1305. ubifs_release_budget(c, &req);
  1306. mutex_lock(&old_inode_ui->ui_mutex);
  1307. release = old_inode_ui->dirty;
  1308. mark_inode_dirty_sync(old_inode);
  1309. mutex_unlock(&old_inode_ui->ui_mutex);
  1310. if (release)
  1311. ubifs_release_budget(c, &ino_req);
  1312. if (IS_SYNC(old_inode))
  1313. err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
  1314. fscrypt_free_filename(&old_nm);
  1315. fscrypt_free_filename(&new_nm);
  1316. return err;
  1317. out_cancel:
  1318. if (unlink) {
  1319. set_nlink(new_inode, saved_nlink);
  1320. } else {
  1321. new_dir->i_size -= new_sz;
  1322. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  1323. }
  1324. old_dir->i_size += old_sz;
  1325. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  1326. if (is_dir) {
  1327. if (move) {
  1328. inc_nlink(old_dir);
  1329. if (!unlink)
  1330. drop_nlink(new_dir);
  1331. } else {
  1332. if (unlink)
  1333. inc_nlink(old_dir);
  1334. }
  1335. }
  1336. if (whiteout) {
  1337. drop_nlink(whiteout);
  1338. iput(whiteout);
  1339. }
  1340. unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1341. ubifs_release_budget(c, &ino_req);
  1342. ubifs_release_budget(c, &req);
  1343. fscrypt_free_filename(&old_nm);
  1344. fscrypt_free_filename(&new_nm);
  1345. return err;
  1346. }
  1347. static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
  1348. struct inode *new_dir, struct dentry *new_dentry)
  1349. {
  1350. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  1351. struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  1352. .dirtied_ino = 2 };
  1353. int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  1354. struct inode *fst_inode = d_inode(old_dentry);
  1355. struct inode *snd_inode = d_inode(new_dentry);
  1356. struct timespec time;
  1357. int err;
  1358. struct fscrypt_name fst_nm, snd_nm;
  1359. ubifs_assert(fst_inode && snd_inode);
  1360. if ((ubifs_crypt_is_encrypted(old_dir) ||
  1361. ubifs_crypt_is_encrypted(new_dir)) &&
  1362. (old_dir != new_dir) &&
  1363. (!fscrypt_has_permitted_context(new_dir, fst_inode) ||
  1364. !fscrypt_has_permitted_context(old_dir, snd_inode)))
  1365. return -EPERM;
  1366. err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
  1367. if (err)
  1368. return err;
  1369. err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
  1370. if (err) {
  1371. fscrypt_free_filename(&fst_nm);
  1372. return err;
  1373. }
  1374. lock_4_inodes(old_dir, new_dir, NULL, NULL);
  1375. time = ubifs_current_time(old_dir);
  1376. fst_inode->i_ctime = time;
  1377. snd_inode->i_ctime = time;
  1378. old_dir->i_mtime = old_dir->i_ctime = time;
  1379. new_dir->i_mtime = new_dir->i_ctime = time;
  1380. if (old_dir != new_dir) {
  1381. if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
  1382. inc_nlink(new_dir);
  1383. drop_nlink(old_dir);
  1384. }
  1385. else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
  1386. drop_nlink(new_dir);
  1387. inc_nlink(old_dir);
  1388. }
  1389. }
  1390. err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
  1391. snd_inode, &snd_nm, sync);
  1392. unlock_4_inodes(old_dir, new_dir, NULL, NULL);
  1393. ubifs_release_budget(c, &req);
  1394. fscrypt_free_filename(&fst_nm);
  1395. fscrypt_free_filename(&snd_nm);
  1396. return err;
  1397. }
  1398. static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1399. struct inode *new_dir, struct dentry *new_dentry,
  1400. unsigned int flags)
  1401. {
  1402. if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
  1403. return -EINVAL;
  1404. ubifs_assert(inode_is_locked(old_dir));
  1405. ubifs_assert(inode_is_locked(new_dir));
  1406. if (flags & RENAME_EXCHANGE)
  1407. return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
  1408. return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  1409. }
  1410. int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1411. struct kstat *stat)
  1412. {
  1413. loff_t size;
  1414. struct inode *inode = d_inode(dentry);
  1415. struct ubifs_inode *ui = ubifs_inode(inode);
  1416. mutex_lock(&ui->ui_mutex);
  1417. generic_fillattr(inode, stat);
  1418. stat->blksize = UBIFS_BLOCK_SIZE;
  1419. stat->size = ui->ui_size;
  1420. /*
  1421. * Unfortunately, the 'stat()' system call was designed for block
  1422. * device based file systems, and it is not appropriate for UBIFS,
  1423. * because UBIFS does not have notion of "block". For example, it is
  1424. * difficult to tell how many block a directory takes - it actually
  1425. * takes less than 300 bytes, but we have to round it to block size,
  1426. * which introduces large mistake. This makes utilities like 'du' to
  1427. * report completely senseless numbers. This is the reason why UBIFS
  1428. * goes the same way as JFFS2 - it reports zero blocks for everything
  1429. * but regular files, which makes more sense than reporting completely
  1430. * wrong sizes.
  1431. */
  1432. if (S_ISREG(inode->i_mode)) {
  1433. size = ui->xattr_size;
  1434. size += stat->size;
  1435. size = ALIGN(size, UBIFS_BLOCK_SIZE);
  1436. /*
  1437. * Note, user-space expects 512-byte blocks count irrespectively
  1438. * of what was reported in @stat->size.
  1439. */
  1440. stat->blocks = size >> 9;
  1441. } else
  1442. stat->blocks = 0;
  1443. mutex_unlock(&ui->ui_mutex);
  1444. return 0;
  1445. }
  1446. static int ubifs_dir_open(struct inode *dir, struct file *file)
  1447. {
  1448. if (ubifs_crypt_is_encrypted(dir))
  1449. return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
  1450. return 0;
  1451. }
  1452. const struct inode_operations ubifs_dir_inode_operations = {
  1453. .lookup = ubifs_lookup,
  1454. .create = ubifs_create,
  1455. .link = ubifs_link,
  1456. .symlink = ubifs_symlink,
  1457. .unlink = ubifs_unlink,
  1458. .mkdir = ubifs_mkdir,
  1459. .rmdir = ubifs_rmdir,
  1460. .mknod = ubifs_mknod,
  1461. .rename = ubifs_rename,
  1462. .setattr = ubifs_setattr,
  1463. .getattr = ubifs_getattr,
  1464. .listxattr = ubifs_listxattr,
  1465. #ifdef CONFIG_UBIFS_ATIME_SUPPORT
  1466. .update_time = ubifs_update_time,
  1467. #endif
  1468. .tmpfile = ubifs_tmpfile,
  1469. };
  1470. const struct file_operations ubifs_dir_operations = {
  1471. .llseek = generic_file_llseek,
  1472. .release = ubifs_dir_release,
  1473. .read = generic_read_dir,
  1474. .iterate_shared = ubifs_readdir,
  1475. .fsync = ubifs_fsync,
  1476. .unlocked_ioctl = ubifs_ioctl,
  1477. .open = ubifs_dir_open,
  1478. #ifdef CONFIG_COMPAT
  1479. .compat_ioctl = ubifs_compat_ioctl,
  1480. #endif
  1481. };