dir.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. /* * This file is part of UBIFS.
  2. *
  3. * Copyright (C) 2006-2008 Nokia Corporation.
  4. * Copyright (C) 2006, 2007 University of Szeged, Hungary
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. * Zoltan Sogor
  22. */
  23. /*
  24. * This file implements directory operations.
  25. *
  26. * All FS operations in this file allocate budget before writing anything to the
  27. * media. If they fail to allocate it, the error is returned. The only
  28. * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
  29. * if they unable to allocate the budget, because deletion %-ENOSPC failure is
  30. * not what users are usually ready to get. UBIFS budgeting subsystem has some
  31. * space reserved for these purposes.
  32. *
  33. * All operations in this file write all inodes which they change straight
  34. * away, instead of marking them dirty. For example, 'ubifs_link()' changes
  35. * @i_size of the parent inode and writes the parent inode together with the
  36. * target inode. This was done to simplify file-system recovery which would
  37. * otherwise be very difficult to do. The only exception is rename which marks
  38. * the re-named inode dirty (because its @i_ctime is updated) but does not
  39. * write it, but just marks it as dirty.
  40. */
  41. #include "ubifs.h"
  42. /**
  43. * inherit_flags - inherit flags of the parent inode.
  44. * @dir: parent inode
  45. * @mode: new inode mode flags
  46. *
  47. * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
  48. * parent directory inode @dir. UBIFS inodes inherit the following flags:
  49. * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
  50. * sub-directory basis;
  51. * o %UBIFS_SYNC_FL - useful for the same reasons;
  52. * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
  53. *
  54. * This function returns the inherited flags.
  55. */
  56. static int inherit_flags(const struct inode *dir, umode_t mode)
  57. {
  58. int flags;
  59. const struct ubifs_inode *ui = ubifs_inode(dir);
  60. if (!S_ISDIR(dir->i_mode))
  61. /*
  62. * The parent is not a directory, which means that an extended
  63. * attribute inode is being created. No flags.
  64. */
  65. return 0;
  66. flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
  67. if (!S_ISDIR(mode))
  68. /* The "DIRSYNC" flag only applies to directories */
  69. flags &= ~UBIFS_DIRSYNC_FL;
  70. return flags;
  71. }
  72. /**
  73. * ubifs_new_inode - allocate new UBIFS inode object.
  74. * @c: UBIFS file-system description object
  75. * @dir: parent directory inode
  76. * @mode: inode mode flags
  77. *
  78. * This function finds an unused inode number, allocates new inode and
  79. * initializes it. Returns new inode in case of success and an error code in
  80. * case of failure.
  81. */
  82. struct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir,
  83. umode_t mode)
  84. {
  85. struct inode *inode;
  86. struct ubifs_inode *ui;
  87. inode = new_inode(c->vfs_sb);
  88. ui = ubifs_inode(inode);
  89. if (!inode)
  90. return ERR_PTR(-ENOMEM);
  91. /*
  92. * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
  93. * marking them dirty in file write path (see 'file_update_time()').
  94. * UBIFS has to fully control "clean <-> dirty" transitions of inodes
  95. * to make budgeting work.
  96. */
  97. inode->i_flags |= S_NOCMTIME;
  98. inode_init_owner(inode, dir, mode);
  99. inode->i_mtime = inode->i_atime = inode->i_ctime =
  100. ubifs_current_time(inode);
  101. inode->i_mapping->nrpages = 0;
  102. switch (mode & S_IFMT) {
  103. case S_IFREG:
  104. inode->i_mapping->a_ops = &ubifs_file_address_operations;
  105. inode->i_op = &ubifs_file_inode_operations;
  106. inode->i_fop = &ubifs_file_operations;
  107. break;
  108. case S_IFDIR:
  109. inode->i_op = &ubifs_dir_inode_operations;
  110. inode->i_fop = &ubifs_dir_operations;
  111. inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
  112. break;
  113. case S_IFLNK:
  114. inode->i_op = &ubifs_symlink_inode_operations;
  115. break;
  116. case S_IFSOCK:
  117. case S_IFIFO:
  118. case S_IFBLK:
  119. case S_IFCHR:
  120. inode->i_op = &ubifs_file_inode_operations;
  121. break;
  122. default:
  123. BUG();
  124. }
  125. ui->flags = inherit_flags(dir, mode);
  126. ubifs_set_inode_flags(inode);
  127. if (S_ISREG(mode))
  128. ui->compr_type = c->default_compr;
  129. else
  130. ui->compr_type = UBIFS_COMPR_NONE;
  131. ui->synced_i_size = 0;
  132. spin_lock(&c->cnt_lock);
  133. /* Inode number overflow is currently not supported */
  134. if (c->highest_inum >= INUM_WARN_WATERMARK) {
  135. if (c->highest_inum >= INUM_WATERMARK) {
  136. spin_unlock(&c->cnt_lock);
  137. ubifs_err(c, "out of inode numbers");
  138. make_bad_inode(inode);
  139. iput(inode);
  140. return ERR_PTR(-EINVAL);
  141. }
  142. ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
  143. (unsigned long)c->highest_inum, INUM_WATERMARK);
  144. }
  145. inode->i_ino = ++c->highest_inum;
  146. /*
  147. * The creation sequence number remains with this inode for its
  148. * lifetime. All nodes for this inode have a greater sequence number,
  149. * and so it is possible to distinguish obsolete nodes belonging to a
  150. * previous incarnation of the same inode number - for example, for the
  151. * purpose of rebuilding the index.
  152. */
  153. ui->creat_sqnum = ++c->max_sqnum;
  154. spin_unlock(&c->cnt_lock);
  155. return inode;
  156. }
  157. static int dbg_check_name(const struct ubifs_info *c,
  158. const struct ubifs_dent_node *dent,
  159. const struct qstr *nm)
  160. {
  161. if (!dbg_is_chk_gen(c))
  162. return 0;
  163. if (le16_to_cpu(dent->nlen) != nm->len)
  164. return -EINVAL;
  165. if (memcmp(dent->name, nm->name, nm->len))
  166. return -EINVAL;
  167. return 0;
  168. }
  169. static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
  170. unsigned int flags)
  171. {
  172. int err;
  173. union ubifs_key key;
  174. struct inode *inode = NULL;
  175. struct ubifs_dent_node *dent;
  176. struct ubifs_info *c = dir->i_sb->s_fs_info;
  177. dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
  178. if (dentry->d_name.len > UBIFS_MAX_NLEN)
  179. return ERR_PTR(-ENAMETOOLONG);
  180. dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  181. if (!dent)
  182. return ERR_PTR(-ENOMEM);
  183. dent_key_init(c, &key, dir->i_ino, &dentry->d_name);
  184. err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name);
  185. if (err) {
  186. if (err == -ENOENT) {
  187. dbg_gen("not found");
  188. goto done;
  189. }
  190. goto out;
  191. }
  192. if (dbg_check_name(c, dent, &dentry->d_name)) {
  193. err = -EINVAL;
  194. goto out;
  195. }
  196. inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
  197. if (IS_ERR(inode)) {
  198. /*
  199. * This should not happen. Probably the file-system needs
  200. * checking.
  201. */
  202. err = PTR_ERR(inode);
  203. ubifs_err(c, "dead directory entry '%pd', error %d",
  204. dentry, err);
  205. ubifs_ro_mode(c, err);
  206. goto out;
  207. }
  208. done:
  209. kfree(dent);
  210. /*
  211. * Note, d_splice_alias() would be required instead if we supported
  212. * NFS.
  213. */
  214. d_add(dentry, inode);
  215. return NULL;
  216. out:
  217. kfree(dent);
  218. return ERR_PTR(err);
  219. }
  220. static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  221. bool excl)
  222. {
  223. struct inode *inode;
  224. struct ubifs_info *c = dir->i_sb->s_fs_info;
  225. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  226. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  227. .dirtied_ino = 1 };
  228. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  229. /*
  230. * Budget request settings: new inode, new direntry, changing the
  231. * parent directory inode.
  232. */
  233. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  234. dentry, mode, dir->i_ino);
  235. err = ubifs_budget_space(c, &req);
  236. if (err)
  237. return err;
  238. inode = ubifs_new_inode(c, dir, mode);
  239. if (IS_ERR(inode)) {
  240. err = PTR_ERR(inode);
  241. goto out_budg;
  242. }
  243. err = ubifs_init_security(dir, inode, &dentry->d_name);
  244. if (err)
  245. goto out_inode;
  246. mutex_lock(&dir_ui->ui_mutex);
  247. dir->i_size += sz_change;
  248. dir_ui->ui_size = dir->i_size;
  249. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  250. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  251. if (err)
  252. goto out_cancel;
  253. mutex_unlock(&dir_ui->ui_mutex);
  254. ubifs_release_budget(c, &req);
  255. insert_inode_hash(inode);
  256. d_instantiate(dentry, inode);
  257. return 0;
  258. out_cancel:
  259. dir->i_size -= sz_change;
  260. dir_ui->ui_size = dir->i_size;
  261. mutex_unlock(&dir_ui->ui_mutex);
  262. out_inode:
  263. make_bad_inode(inode);
  264. iput(inode);
  265. out_budg:
  266. ubifs_release_budget(c, &req);
  267. ubifs_err(c, "cannot create regular file, error %d", err);
  268. return err;
  269. }
  270. static int do_tmpfile(struct inode *dir, struct dentry *dentry,
  271. umode_t mode, struct inode **whiteout)
  272. {
  273. struct inode *inode;
  274. struct ubifs_info *c = dir->i_sb->s_fs_info;
  275. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
  276. struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
  277. struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
  278. int err, instantiated = 0;
  279. /*
  280. * Budget request settings: new dirty inode, new direntry,
  281. * budget for dirtied inode will be released via writeback.
  282. */
  283. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  284. dentry, mode, dir->i_ino);
  285. err = ubifs_budget_space(c, &req);
  286. if (err)
  287. return err;
  288. err = ubifs_budget_space(c, &ino_req);
  289. if (err) {
  290. ubifs_release_budget(c, &req);
  291. return err;
  292. }
  293. inode = ubifs_new_inode(c, dir, mode);
  294. if (IS_ERR(inode)) {
  295. err = PTR_ERR(inode);
  296. goto out_budg;
  297. }
  298. ui = ubifs_inode(inode);
  299. if (whiteout) {
  300. init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
  301. ubifs_assert(inode->i_op == &ubifs_file_inode_operations);
  302. }
  303. err = ubifs_init_security(dir, inode, &dentry->d_name);
  304. if (err)
  305. goto out_inode;
  306. mutex_lock(&ui->ui_mutex);
  307. insert_inode_hash(inode);
  308. if (whiteout) {
  309. mark_inode_dirty(inode);
  310. drop_nlink(inode);
  311. *whiteout = inode;
  312. } else {
  313. d_tmpfile(dentry, inode);
  314. }
  315. ubifs_assert(ui->dirty);
  316. instantiated = 1;
  317. mutex_unlock(&ui->ui_mutex);
  318. mutex_lock(&dir_ui->ui_mutex);
  319. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
  320. if (err)
  321. goto out_cancel;
  322. mutex_unlock(&dir_ui->ui_mutex);
  323. ubifs_release_budget(c, &req);
  324. return 0;
  325. out_cancel:
  326. mutex_unlock(&dir_ui->ui_mutex);
  327. out_inode:
  328. make_bad_inode(inode);
  329. if (!instantiated)
  330. iput(inode);
  331. out_budg:
  332. ubifs_release_budget(c, &req);
  333. if (!instantiated)
  334. ubifs_release_budget(c, &ino_req);
  335. ubifs_err(c, "cannot create temporary file, error %d", err);
  336. return err;
  337. }
  338. static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
  339. umode_t mode)
  340. {
  341. return do_tmpfile(dir, dentry, mode, NULL);
  342. }
  343. /**
  344. * vfs_dent_type - get VFS directory entry type.
  345. * @type: UBIFS directory entry type
  346. *
  347. * This function converts UBIFS directory entry type into VFS directory entry
  348. * type.
  349. */
  350. static unsigned int vfs_dent_type(uint8_t type)
  351. {
  352. switch (type) {
  353. case UBIFS_ITYPE_REG:
  354. return DT_REG;
  355. case UBIFS_ITYPE_DIR:
  356. return DT_DIR;
  357. case UBIFS_ITYPE_LNK:
  358. return DT_LNK;
  359. case UBIFS_ITYPE_BLK:
  360. return DT_BLK;
  361. case UBIFS_ITYPE_CHR:
  362. return DT_CHR;
  363. case UBIFS_ITYPE_FIFO:
  364. return DT_FIFO;
  365. case UBIFS_ITYPE_SOCK:
  366. return DT_SOCK;
  367. default:
  368. BUG();
  369. }
  370. return 0;
  371. }
  372. /*
  373. * The classical Unix view for directory is that it is a linear array of
  374. * (name, inode number) entries. Linux/VFS assumes this model as well.
  375. * Particularly, 'readdir()' call wants us to return a directory entry offset
  376. * which later may be used to continue 'readdir()'ing the directory or to
  377. * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
  378. * model because directory entries are identified by keys, which may collide.
  379. *
  380. * UBIFS uses directory entry hash value for directory offsets, so
  381. * 'seekdir()'/'telldir()' may not always work because of possible key
  382. * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
  383. * properly by means of saving full directory entry name in the private field
  384. * of the file description object.
  385. *
  386. * This means that UBIFS cannot support NFS which requires full
  387. * 'seekdir()'/'telldir()' support.
  388. */
  389. static int ubifs_readdir(struct file *file, struct dir_context *ctx)
  390. {
  391. int err = 0;
  392. struct qstr nm;
  393. union ubifs_key key;
  394. struct ubifs_dent_node *dent;
  395. struct inode *dir = file_inode(file);
  396. struct ubifs_info *c = dir->i_sb->s_fs_info;
  397. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
  398. if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
  399. /*
  400. * The directory was seek'ed to a senseless position or there
  401. * are no more entries.
  402. */
  403. return 0;
  404. if (file->f_version == 0) {
  405. /*
  406. * The file was seek'ed, which means that @file->private_data
  407. * is now invalid. This may also be just the first
  408. * 'ubifs_readdir()' invocation, in which case
  409. * @file->private_data is NULL, and the below code is
  410. * basically a no-op.
  411. */
  412. kfree(file->private_data);
  413. file->private_data = NULL;
  414. }
  415. /*
  416. * 'generic_file_llseek()' unconditionally sets @file->f_version to
  417. * zero, and we use this for detecting whether the file was seek'ed.
  418. */
  419. file->f_version = 1;
  420. /* File positions 0 and 1 correspond to "." and ".." */
  421. if (ctx->pos < 2) {
  422. ubifs_assert(!file->private_data);
  423. if (!dir_emit_dots(file, ctx))
  424. return 0;
  425. /* Find the first entry in TNC and save it */
  426. lowest_dent_key(c, &key, dir->i_ino);
  427. nm.name = NULL;
  428. dent = ubifs_tnc_next_ent(c, &key, &nm);
  429. if (IS_ERR(dent)) {
  430. err = PTR_ERR(dent);
  431. goto out;
  432. }
  433. ctx->pos = key_hash_flash(c, &dent->key);
  434. file->private_data = dent;
  435. }
  436. dent = file->private_data;
  437. if (!dent) {
  438. /*
  439. * The directory was seek'ed to and is now readdir'ed.
  440. * Find the entry corresponding to @ctx->pos or the closest one.
  441. */
  442. dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
  443. nm.name = NULL;
  444. dent = ubifs_tnc_next_ent(c, &key, &nm);
  445. if (IS_ERR(dent)) {
  446. err = PTR_ERR(dent);
  447. goto out;
  448. }
  449. ctx->pos = key_hash_flash(c, &dent->key);
  450. file->private_data = dent;
  451. }
  452. while (1) {
  453. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  454. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  455. key_hash_flash(c, &dent->key));
  456. ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
  457. ubifs_inode(dir)->creat_sqnum);
  458. nm.len = le16_to_cpu(dent->nlen);
  459. if (!dir_emit(ctx, dent->name, nm.len,
  460. le64_to_cpu(dent->inum),
  461. vfs_dent_type(dent->type)))
  462. return 0;
  463. /* Switch to the next entry */
  464. key_read(c, &dent->key, &key);
  465. nm.name = dent->name;
  466. dent = ubifs_tnc_next_ent(c, &key, &nm);
  467. if (IS_ERR(dent)) {
  468. err = PTR_ERR(dent);
  469. goto out;
  470. }
  471. kfree(file->private_data);
  472. ctx->pos = key_hash_flash(c, &dent->key);
  473. file->private_data = dent;
  474. cond_resched();
  475. }
  476. out:
  477. kfree(file->private_data);
  478. file->private_data = NULL;
  479. if (err != -ENOENT)
  480. ubifs_err(c, "cannot find next direntry, error %d", err);
  481. else
  482. /*
  483. * -ENOENT is a non-fatal error in this context, the TNC uses
  484. * it to indicate that the cursor moved past the current directory
  485. * and readdir() has to stop.
  486. */
  487. err = 0;
  488. /* 2 is a special value indicating that there are no more direntries */
  489. ctx->pos = 2;
  490. return err;
  491. }
  492. /* Free saved readdir() state when the directory is closed */
  493. static int ubifs_dir_release(struct inode *dir, struct file *file)
  494. {
  495. kfree(file->private_data);
  496. file->private_data = NULL;
  497. return 0;
  498. }
  499. /**
  500. * lock_2_inodes - a wrapper for locking two UBIFS inodes.
  501. * @inode1: first inode
  502. * @inode2: second inode
  503. *
  504. * We do not implement any tricks to guarantee strict lock ordering, because
  505. * VFS has already done it for us on the @i_mutex. So this is just a simple
  506. * wrapper function.
  507. */
  508. static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
  509. {
  510. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  511. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  512. }
  513. /**
  514. * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
  515. * @inode1: first inode
  516. * @inode2: second inode
  517. */
  518. static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
  519. {
  520. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  521. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  522. }
  523. static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
  524. struct dentry *dentry)
  525. {
  526. struct ubifs_info *c = dir->i_sb->s_fs_info;
  527. struct inode *inode = d_inode(old_dentry);
  528. struct ubifs_inode *ui = ubifs_inode(inode);
  529. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  530. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  531. struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
  532. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  533. /*
  534. * Budget request settings: new direntry, changing the target inode,
  535. * changing the parent inode.
  536. */
  537. dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
  538. dentry, inode->i_ino,
  539. inode->i_nlink, dir->i_ino);
  540. ubifs_assert(inode_is_locked(dir));
  541. ubifs_assert(inode_is_locked(inode));
  542. err = dbg_check_synced_i_size(c, inode);
  543. if (err)
  544. return err;
  545. err = ubifs_budget_space(c, &req);
  546. if (err)
  547. return err;
  548. lock_2_inodes(dir, inode);
  549. /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
  550. if (inode->i_nlink == 0)
  551. ubifs_delete_orphan(c, inode->i_ino);
  552. inc_nlink(inode);
  553. ihold(inode);
  554. inode->i_ctime = ubifs_current_time(inode);
  555. dir->i_size += sz_change;
  556. dir_ui->ui_size = dir->i_size;
  557. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  558. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  559. if (err)
  560. goto out_cancel;
  561. unlock_2_inodes(dir, inode);
  562. ubifs_release_budget(c, &req);
  563. d_instantiate(dentry, inode);
  564. return 0;
  565. out_cancel:
  566. dir->i_size -= sz_change;
  567. dir_ui->ui_size = dir->i_size;
  568. drop_nlink(inode);
  569. if (inode->i_nlink == 0)
  570. ubifs_add_orphan(c, inode->i_ino);
  571. unlock_2_inodes(dir, inode);
  572. ubifs_release_budget(c, &req);
  573. iput(inode);
  574. return err;
  575. }
  576. static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
  577. {
  578. struct ubifs_info *c = dir->i_sb->s_fs_info;
  579. struct inode *inode = d_inode(dentry);
  580. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  581. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  582. int err, budgeted = 1;
  583. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  584. unsigned int saved_nlink = inode->i_nlink;
  585. /*
  586. * Budget request settings: deletion direntry, deletion inode (+1 for
  587. * @dirtied_ino), changing the parent directory inode. If budgeting
  588. * fails, go ahead anyway because we have extra space reserved for
  589. * deletions.
  590. */
  591. dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
  592. dentry, inode->i_ino,
  593. inode->i_nlink, dir->i_ino);
  594. ubifs_assert(inode_is_locked(dir));
  595. ubifs_assert(inode_is_locked(inode));
  596. err = dbg_check_synced_i_size(c, inode);
  597. if (err)
  598. return err;
  599. err = ubifs_budget_space(c, &req);
  600. if (err) {
  601. if (err != -ENOSPC)
  602. return err;
  603. budgeted = 0;
  604. }
  605. lock_2_inodes(dir, inode);
  606. inode->i_ctime = ubifs_current_time(dir);
  607. drop_nlink(inode);
  608. dir->i_size -= sz_change;
  609. dir_ui->ui_size = dir->i_size;
  610. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  611. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
  612. if (err)
  613. goto out_cancel;
  614. unlock_2_inodes(dir, inode);
  615. if (budgeted)
  616. ubifs_release_budget(c, &req);
  617. else {
  618. /* We've deleted something - clean the "no space" flags */
  619. c->bi.nospace = c->bi.nospace_rp = 0;
  620. smp_wmb();
  621. }
  622. return 0;
  623. out_cancel:
  624. dir->i_size += sz_change;
  625. dir_ui->ui_size = dir->i_size;
  626. set_nlink(inode, saved_nlink);
  627. unlock_2_inodes(dir, inode);
  628. if (budgeted)
  629. ubifs_release_budget(c, &req);
  630. return err;
  631. }
  632. /**
  633. * check_dir_empty - check if a directory is empty or not.
  634. * @c: UBIFS file-system description object
  635. * @dir: VFS inode object of the directory to check
  636. *
  637. * This function checks if directory @dir is empty. Returns zero if the
  638. * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
  639. * in case of of errors.
  640. */
  641. static int check_dir_empty(struct ubifs_info *c, struct inode *dir)
  642. {
  643. struct qstr nm = { .name = NULL };
  644. struct ubifs_dent_node *dent;
  645. union ubifs_key key;
  646. int err;
  647. lowest_dent_key(c, &key, dir->i_ino);
  648. dent = ubifs_tnc_next_ent(c, &key, &nm);
  649. if (IS_ERR(dent)) {
  650. err = PTR_ERR(dent);
  651. if (err == -ENOENT)
  652. err = 0;
  653. } else {
  654. kfree(dent);
  655. err = -ENOTEMPTY;
  656. }
  657. return err;
  658. }
  659. static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
  660. {
  661. struct ubifs_info *c = dir->i_sb->s_fs_info;
  662. struct inode *inode = d_inode(dentry);
  663. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  664. int err, budgeted = 1;
  665. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  666. struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
  667. /*
  668. * Budget request settings: deletion direntry, deletion inode and
  669. * changing the parent inode. If budgeting fails, go ahead anyway
  670. * because we have extra space reserved for deletions.
  671. */
  672. dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
  673. inode->i_ino, dir->i_ino);
  674. ubifs_assert(inode_is_locked(dir));
  675. ubifs_assert(inode_is_locked(inode));
  676. err = check_dir_empty(c, d_inode(dentry));
  677. if (err)
  678. return err;
  679. err = ubifs_budget_space(c, &req);
  680. if (err) {
  681. if (err != -ENOSPC)
  682. return err;
  683. budgeted = 0;
  684. }
  685. lock_2_inodes(dir, inode);
  686. inode->i_ctime = ubifs_current_time(dir);
  687. clear_nlink(inode);
  688. drop_nlink(dir);
  689. dir->i_size -= sz_change;
  690. dir_ui->ui_size = dir->i_size;
  691. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  692. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
  693. if (err)
  694. goto out_cancel;
  695. unlock_2_inodes(dir, inode);
  696. if (budgeted)
  697. ubifs_release_budget(c, &req);
  698. else {
  699. /* We've deleted something - clean the "no space" flags */
  700. c->bi.nospace = c->bi.nospace_rp = 0;
  701. smp_wmb();
  702. }
  703. return 0;
  704. out_cancel:
  705. dir->i_size += sz_change;
  706. dir_ui->ui_size = dir->i_size;
  707. inc_nlink(dir);
  708. set_nlink(inode, 2);
  709. unlock_2_inodes(dir, inode);
  710. if (budgeted)
  711. ubifs_release_budget(c, &req);
  712. return err;
  713. }
  714. static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  715. {
  716. struct inode *inode;
  717. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  718. struct ubifs_info *c = dir->i_sb->s_fs_info;
  719. int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  720. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
  721. /*
  722. * Budget request settings: new inode, new direntry and changing parent
  723. * directory inode.
  724. */
  725. dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
  726. dentry, mode, dir->i_ino);
  727. err = ubifs_budget_space(c, &req);
  728. if (err)
  729. return err;
  730. inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
  731. if (IS_ERR(inode)) {
  732. err = PTR_ERR(inode);
  733. goto out_budg;
  734. }
  735. err = ubifs_init_security(dir, inode, &dentry->d_name);
  736. if (err)
  737. goto out_inode;
  738. mutex_lock(&dir_ui->ui_mutex);
  739. insert_inode_hash(inode);
  740. inc_nlink(inode);
  741. inc_nlink(dir);
  742. dir->i_size += sz_change;
  743. dir_ui->ui_size = dir->i_size;
  744. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  745. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  746. if (err) {
  747. ubifs_err(c, "cannot create directory, error %d", err);
  748. goto out_cancel;
  749. }
  750. mutex_unlock(&dir_ui->ui_mutex);
  751. ubifs_release_budget(c, &req);
  752. d_instantiate(dentry, inode);
  753. return 0;
  754. out_cancel:
  755. dir->i_size -= sz_change;
  756. dir_ui->ui_size = dir->i_size;
  757. drop_nlink(dir);
  758. mutex_unlock(&dir_ui->ui_mutex);
  759. out_inode:
  760. make_bad_inode(inode);
  761. iput(inode);
  762. out_budg:
  763. ubifs_release_budget(c, &req);
  764. return err;
  765. }
  766. static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
  767. umode_t mode, dev_t rdev)
  768. {
  769. struct inode *inode;
  770. struct ubifs_inode *ui;
  771. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  772. struct ubifs_info *c = dir->i_sb->s_fs_info;
  773. union ubifs_dev_desc *dev = NULL;
  774. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  775. int err, devlen = 0;
  776. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  777. .new_ino_d = ALIGN(devlen, 8),
  778. .dirtied_ino = 1 };
  779. /*
  780. * Budget request settings: new inode, new direntry and changing parent
  781. * directory inode.
  782. */
  783. dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
  784. if (S_ISBLK(mode) || S_ISCHR(mode)) {
  785. dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  786. if (!dev)
  787. return -ENOMEM;
  788. devlen = ubifs_encode_dev(dev, rdev);
  789. }
  790. err = ubifs_budget_space(c, &req);
  791. if (err) {
  792. kfree(dev);
  793. return err;
  794. }
  795. inode = ubifs_new_inode(c, dir, mode);
  796. if (IS_ERR(inode)) {
  797. kfree(dev);
  798. err = PTR_ERR(inode);
  799. goto out_budg;
  800. }
  801. init_special_inode(inode, inode->i_mode, rdev);
  802. inode->i_size = ubifs_inode(inode)->ui_size = devlen;
  803. ui = ubifs_inode(inode);
  804. ui->data = dev;
  805. ui->data_len = devlen;
  806. err = ubifs_init_security(dir, inode, &dentry->d_name);
  807. if (err)
  808. goto out_inode;
  809. mutex_lock(&dir_ui->ui_mutex);
  810. dir->i_size += sz_change;
  811. dir_ui->ui_size = dir->i_size;
  812. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  813. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  814. if (err)
  815. goto out_cancel;
  816. mutex_unlock(&dir_ui->ui_mutex);
  817. ubifs_release_budget(c, &req);
  818. insert_inode_hash(inode);
  819. d_instantiate(dentry, inode);
  820. return 0;
  821. out_cancel:
  822. dir->i_size -= sz_change;
  823. dir_ui->ui_size = dir->i_size;
  824. mutex_unlock(&dir_ui->ui_mutex);
  825. out_inode:
  826. make_bad_inode(inode);
  827. iput(inode);
  828. out_budg:
  829. ubifs_release_budget(c, &req);
  830. return err;
  831. }
  832. static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
  833. const char *symname)
  834. {
  835. struct inode *inode;
  836. struct ubifs_inode *ui;
  837. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  838. struct ubifs_info *c = dir->i_sb->s_fs_info;
  839. int err, len = strlen(symname);
  840. int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
  841. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  842. .new_ino_d = ALIGN(len, 8),
  843. .dirtied_ino = 1 };
  844. /*
  845. * Budget request settings: new inode, new direntry and changing parent
  846. * directory inode.
  847. */
  848. dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
  849. symname, dir->i_ino);
  850. if (len > UBIFS_MAX_INO_DATA)
  851. return -ENAMETOOLONG;
  852. err = ubifs_budget_space(c, &req);
  853. if (err)
  854. return err;
  855. inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
  856. if (IS_ERR(inode)) {
  857. err = PTR_ERR(inode);
  858. goto out_budg;
  859. }
  860. ui = ubifs_inode(inode);
  861. ui->data = kmalloc(len + 1, GFP_NOFS);
  862. if (!ui->data) {
  863. err = -ENOMEM;
  864. goto out_inode;
  865. }
  866. memcpy(ui->data, symname, len);
  867. ((char *)ui->data)[len] = '\0';
  868. inode->i_link = ui->data;
  869. /*
  870. * The terminating zero byte is not written to the flash media and it
  871. * is put just to make later in-memory string processing simpler. Thus,
  872. * data length is @len, not @len + %1.
  873. */
  874. ui->data_len = len;
  875. inode->i_size = ubifs_inode(inode)->ui_size = len;
  876. err = ubifs_init_security(dir, inode, &dentry->d_name);
  877. if (err)
  878. goto out_inode;
  879. mutex_lock(&dir_ui->ui_mutex);
  880. dir->i_size += sz_change;
  881. dir_ui->ui_size = dir->i_size;
  882. dir->i_mtime = dir->i_ctime = inode->i_ctime;
  883. err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
  884. if (err)
  885. goto out_cancel;
  886. mutex_unlock(&dir_ui->ui_mutex);
  887. ubifs_release_budget(c, &req);
  888. insert_inode_hash(inode);
  889. d_instantiate(dentry, inode);
  890. return 0;
  891. out_cancel:
  892. dir->i_size -= sz_change;
  893. dir_ui->ui_size = dir->i_size;
  894. mutex_unlock(&dir_ui->ui_mutex);
  895. out_inode:
  896. make_bad_inode(inode);
  897. iput(inode);
  898. out_budg:
  899. ubifs_release_budget(c, &req);
  900. return err;
  901. }
  902. /**
  903. * lock_4_inodes - a wrapper for locking three UBIFS inodes.
  904. * @inode1: first inode
  905. * @inode2: second inode
  906. * @inode3: third inode
  907. * @inode4: fouth inode
  908. *
  909. * This function is used for 'ubifs_rename()' and @inode1 may be the same as
  910. * @inode2 whereas @inode3 and @inode4 may be %NULL.
  911. *
  912. * We do not implement any tricks to guarantee strict lock ordering, because
  913. * VFS has already done it for us on the @i_mutex. So this is just a simple
  914. * wrapper function.
  915. */
  916. static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
  917. struct inode *inode3, struct inode *inode4)
  918. {
  919. mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
  920. if (inode2 != inode1)
  921. mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
  922. if (inode3)
  923. mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
  924. if (inode4)
  925. mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
  926. }
  927. /**
  928. * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
  929. * @inode1: first inode
  930. * @inode2: second inode
  931. * @inode3: third inode
  932. * @inode4: fouth inode
  933. */
  934. static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
  935. struct inode *inode3, struct inode *inode4)
  936. {
  937. if (inode4)
  938. mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
  939. if (inode3)
  940. mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
  941. if (inode1 != inode2)
  942. mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
  943. mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
  944. }
  945. static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
  946. struct inode *new_dir, struct dentry *new_dentry,
  947. unsigned int flags)
  948. {
  949. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  950. struct inode *old_inode = d_inode(old_dentry);
  951. struct inode *new_inode = d_inode(new_dentry);
  952. struct inode *whiteout = NULL;
  953. struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
  954. struct ubifs_inode *whiteout_ui = NULL;
  955. int err, release, sync = 0, move = (new_dir != old_dir);
  956. int is_dir = S_ISDIR(old_inode->i_mode);
  957. int unlink = !!new_inode;
  958. int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
  959. int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
  960. struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  961. .dirtied_ino = 3 };
  962. struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
  963. .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
  964. struct timespec time;
  965. unsigned int uninitialized_var(saved_nlink);
  966. /*
  967. * Budget request settings: deletion direntry, new direntry, removing
  968. * the old inode, and changing old and new parent directory inodes.
  969. *
  970. * However, this operation also marks the target inode as dirty and
  971. * does not write it, so we allocate budget for the target inode
  972. * separately.
  973. */
  974. dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
  975. old_dentry, old_inode->i_ino, old_dir->i_ino,
  976. new_dentry, new_dir->i_ino, flags);
  977. if (unlink)
  978. ubifs_assert(inode_is_locked(new_inode));
  979. if (unlink && is_dir) {
  980. err = check_dir_empty(c, new_inode);
  981. if (err)
  982. return err;
  983. }
  984. err = ubifs_budget_space(c, &req);
  985. if (err)
  986. return err;
  987. err = ubifs_budget_space(c, &ino_req);
  988. if (err) {
  989. ubifs_release_budget(c, &req);
  990. return err;
  991. }
  992. if (flags & RENAME_WHITEOUT) {
  993. union ubifs_dev_desc *dev = NULL;
  994. dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  995. if (!dev) {
  996. ubifs_release_budget(c, &req);
  997. ubifs_release_budget(c, &ino_req);
  998. return -ENOMEM;
  999. }
  1000. err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
  1001. if (err) {
  1002. ubifs_release_budget(c, &req);
  1003. ubifs_release_budget(c, &ino_req);
  1004. kfree(dev);
  1005. return err;
  1006. }
  1007. whiteout->i_state |= I_LINKABLE;
  1008. whiteout_ui = ubifs_inode(whiteout);
  1009. whiteout_ui->data = dev;
  1010. whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
  1011. ubifs_assert(!whiteout_ui->dirty);
  1012. }
  1013. lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1014. /*
  1015. * Like most other Unix systems, set the @i_ctime for inodes on a
  1016. * rename.
  1017. */
  1018. time = ubifs_current_time(old_dir);
  1019. old_inode->i_ctime = time;
  1020. /* We must adjust parent link count when renaming directories */
  1021. if (is_dir) {
  1022. if (move) {
  1023. /*
  1024. * @old_dir loses a link because we are moving
  1025. * @old_inode to a different directory.
  1026. */
  1027. drop_nlink(old_dir);
  1028. /*
  1029. * @new_dir only gains a link if we are not also
  1030. * overwriting an existing directory.
  1031. */
  1032. if (!unlink)
  1033. inc_nlink(new_dir);
  1034. } else {
  1035. /*
  1036. * @old_inode is not moving to a different directory,
  1037. * but @old_dir still loses a link if we are
  1038. * overwriting an existing directory.
  1039. */
  1040. if (unlink)
  1041. drop_nlink(old_dir);
  1042. }
  1043. }
  1044. old_dir->i_size -= old_sz;
  1045. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  1046. old_dir->i_mtime = old_dir->i_ctime = time;
  1047. new_dir->i_mtime = new_dir->i_ctime = time;
  1048. /*
  1049. * And finally, if we unlinked a direntry which happened to have the
  1050. * same name as the moved direntry, we have to decrement @i_nlink of
  1051. * the unlinked inode and change its ctime.
  1052. */
  1053. if (unlink) {
  1054. /*
  1055. * Directories cannot have hard-links, so if this is a
  1056. * directory, just clear @i_nlink.
  1057. */
  1058. saved_nlink = new_inode->i_nlink;
  1059. if (is_dir)
  1060. clear_nlink(new_inode);
  1061. else
  1062. drop_nlink(new_inode);
  1063. new_inode->i_ctime = time;
  1064. } else {
  1065. new_dir->i_size += new_sz;
  1066. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  1067. }
  1068. /*
  1069. * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
  1070. * is dirty, because this will be done later on at the end of
  1071. * 'ubifs_rename()'.
  1072. */
  1073. if (IS_SYNC(old_inode)) {
  1074. sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  1075. if (unlink && IS_SYNC(new_inode))
  1076. sync = 1;
  1077. }
  1078. if (whiteout) {
  1079. struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
  1080. .dirtied_ino_d = \
  1081. ALIGN(ubifs_inode(whiteout)->data_len, 8) };
  1082. err = ubifs_budget_space(c, &wht_req);
  1083. if (err) {
  1084. ubifs_release_budget(c, &req);
  1085. ubifs_release_budget(c, &ino_req);
  1086. kfree(whiteout_ui->data);
  1087. whiteout_ui->data_len = 0;
  1088. iput(whiteout);
  1089. return err;
  1090. }
  1091. inc_nlink(whiteout);
  1092. mark_inode_dirty(whiteout);
  1093. whiteout->i_state &= ~I_LINKABLE;
  1094. iput(whiteout);
  1095. }
  1096. err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry, whiteout,
  1097. sync);
  1098. if (err)
  1099. goto out_cancel;
  1100. unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1101. ubifs_release_budget(c, &req);
  1102. mutex_lock(&old_inode_ui->ui_mutex);
  1103. release = old_inode_ui->dirty;
  1104. mark_inode_dirty_sync(old_inode);
  1105. mutex_unlock(&old_inode_ui->ui_mutex);
  1106. if (release)
  1107. ubifs_release_budget(c, &ino_req);
  1108. if (IS_SYNC(old_inode))
  1109. err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
  1110. return err;
  1111. out_cancel:
  1112. if (unlink) {
  1113. set_nlink(new_inode, saved_nlink);
  1114. } else {
  1115. new_dir->i_size -= new_sz;
  1116. ubifs_inode(new_dir)->ui_size = new_dir->i_size;
  1117. }
  1118. old_dir->i_size += old_sz;
  1119. ubifs_inode(old_dir)->ui_size = old_dir->i_size;
  1120. if (is_dir) {
  1121. if (move) {
  1122. inc_nlink(old_dir);
  1123. if (!unlink)
  1124. drop_nlink(new_dir);
  1125. } else {
  1126. if (unlink)
  1127. inc_nlink(old_dir);
  1128. }
  1129. }
  1130. if (whiteout) {
  1131. drop_nlink(whiteout);
  1132. iput(whiteout);
  1133. }
  1134. unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
  1135. ubifs_release_budget(c, &ino_req);
  1136. ubifs_release_budget(c, &req);
  1137. return err;
  1138. }
  1139. static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
  1140. struct inode *new_dir, struct dentry *new_dentry)
  1141. {
  1142. struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  1143. struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  1144. .dirtied_ino = 2 };
  1145. int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  1146. struct inode *fst_inode = d_inode(old_dentry);
  1147. struct inode *snd_inode = d_inode(new_dentry);
  1148. struct timespec time;
  1149. int err;
  1150. ubifs_assert(fst_inode && snd_inode);
  1151. lock_4_inodes(old_dir, new_dir, NULL, NULL);
  1152. time = ubifs_current_time(old_dir);
  1153. fst_inode->i_ctime = time;
  1154. snd_inode->i_ctime = time;
  1155. old_dir->i_mtime = old_dir->i_ctime = time;
  1156. new_dir->i_mtime = new_dir->i_ctime = time;
  1157. if (old_dir != new_dir) {
  1158. if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
  1159. inc_nlink(new_dir);
  1160. drop_nlink(old_dir);
  1161. }
  1162. else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
  1163. drop_nlink(new_dir);
  1164. inc_nlink(old_dir);
  1165. }
  1166. }
  1167. err = ubifs_jnl_xrename(c, old_dir, old_dentry, new_dir, new_dentry,
  1168. sync);
  1169. unlock_4_inodes(old_dir, new_dir, NULL, NULL);
  1170. ubifs_release_budget(c, &req);
  1171. return err;
  1172. }
  1173. static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1174. struct inode *new_dir, struct dentry *new_dentry,
  1175. unsigned int flags)
  1176. {
  1177. if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
  1178. return -EINVAL;
  1179. ubifs_assert(inode_is_locked(old_dir));
  1180. ubifs_assert(inode_is_locked(new_dir));
  1181. if (flags & RENAME_EXCHANGE)
  1182. return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
  1183. return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  1184. }
  1185. int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1186. struct kstat *stat)
  1187. {
  1188. loff_t size;
  1189. struct inode *inode = d_inode(dentry);
  1190. struct ubifs_inode *ui = ubifs_inode(inode);
  1191. mutex_lock(&ui->ui_mutex);
  1192. generic_fillattr(inode, stat);
  1193. stat->blksize = UBIFS_BLOCK_SIZE;
  1194. stat->size = ui->ui_size;
  1195. /*
  1196. * Unfortunately, the 'stat()' system call was designed for block
  1197. * device based file systems, and it is not appropriate for UBIFS,
  1198. * because UBIFS does not have notion of "block". For example, it is
  1199. * difficult to tell how many block a directory takes - it actually
  1200. * takes less than 300 bytes, but we have to round it to block size,
  1201. * which introduces large mistake. This makes utilities like 'du' to
  1202. * report completely senseless numbers. This is the reason why UBIFS
  1203. * goes the same way as JFFS2 - it reports zero blocks for everything
  1204. * but regular files, which makes more sense than reporting completely
  1205. * wrong sizes.
  1206. */
  1207. if (S_ISREG(inode->i_mode)) {
  1208. size = ui->xattr_size;
  1209. size += stat->size;
  1210. size = ALIGN(size, UBIFS_BLOCK_SIZE);
  1211. /*
  1212. * Note, user-space expects 512-byte blocks count irrespectively
  1213. * of what was reported in @stat->size.
  1214. */
  1215. stat->blocks = size >> 9;
  1216. } else
  1217. stat->blocks = 0;
  1218. mutex_unlock(&ui->ui_mutex);
  1219. return 0;
  1220. }
  1221. const struct inode_operations ubifs_dir_inode_operations = {
  1222. .lookup = ubifs_lookup,
  1223. .create = ubifs_create,
  1224. .link = ubifs_link,
  1225. .symlink = ubifs_symlink,
  1226. .unlink = ubifs_unlink,
  1227. .mkdir = ubifs_mkdir,
  1228. .rmdir = ubifs_rmdir,
  1229. .mknod = ubifs_mknod,
  1230. .rename = ubifs_rename,
  1231. .setattr = ubifs_setattr,
  1232. .getattr = ubifs_getattr,
  1233. .listxattr = ubifs_listxattr,
  1234. #ifdef CONFIG_UBIFS_ATIME_SUPPORT
  1235. .update_time = ubifs_update_time,
  1236. #endif
  1237. .tmpfile = ubifs_tmpfile,
  1238. };
  1239. const struct file_operations ubifs_dir_operations = {
  1240. .llseek = generic_file_llseek,
  1241. .release = ubifs_dir_release,
  1242. .read = generic_read_dir,
  1243. .iterate_shared = ubifs_readdir,
  1244. .fsync = ubifs_fsync,
  1245. .unlocked_ioctl = ubifs_ioctl,
  1246. #ifdef CONFIG_COMPAT
  1247. .compat_ioctl = ubifs_compat_ioctl,
  1248. #endif
  1249. };