inode.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/posix_acl.h>
  15. #include <linux/sort.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include <linux/crc32.h>
  18. #include <linux/security.h>
  19. #include <linux/time.h>
  20. #include "gfs2.h"
  21. #include "incore.h"
  22. #include "acl.h"
  23. #include "bmap.h"
  24. #include "dir.h"
  25. #include "xattr.h"
  26. #include "glock.h"
  27. #include "glops.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. struct gfs2_inum_range_host {
  36. u64 ir_start;
  37. u64 ir_length;
  38. };
  39. static int iget_test(struct inode *inode, void *opaque)
  40. {
  41. struct gfs2_inode *ip = GFS2_I(inode);
  42. u64 *no_addr = opaque;
  43. if (ip->i_no_addr == *no_addr)
  44. return 1;
  45. return 0;
  46. }
  47. static int iget_set(struct inode *inode, void *opaque)
  48. {
  49. struct gfs2_inode *ip = GFS2_I(inode);
  50. u64 *no_addr = opaque;
  51. inode->i_ino = (unsigned long)*no_addr;
  52. ip->i_no_addr = *no_addr;
  53. return 0;
  54. }
  55. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
  56. {
  57. unsigned long hash = (unsigned long)no_addr;
  58. return ilookup5(sb, hash, iget_test, &no_addr);
  59. }
  60. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  61. {
  62. unsigned long hash = (unsigned long)no_addr;
  63. return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
  64. }
  65. struct gfs2_skip_data {
  66. u64 no_addr;
  67. int skipped;
  68. };
  69. static int iget_skip_test(struct inode *inode, void *opaque)
  70. {
  71. struct gfs2_inode *ip = GFS2_I(inode);
  72. struct gfs2_skip_data *data = opaque;
  73. if (ip->i_no_addr == data->no_addr) {
  74. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)){
  75. data->skipped = 1;
  76. return 0;
  77. }
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static int iget_skip_set(struct inode *inode, void *opaque)
  83. {
  84. struct gfs2_inode *ip = GFS2_I(inode);
  85. struct gfs2_skip_data *data = opaque;
  86. if (data->skipped)
  87. return 1;
  88. inode->i_ino = (unsigned long)(data->no_addr);
  89. ip->i_no_addr = data->no_addr;
  90. return 0;
  91. }
  92. static struct inode *gfs2_iget_skip(struct super_block *sb,
  93. u64 no_addr)
  94. {
  95. struct gfs2_skip_data data;
  96. unsigned long hash = (unsigned long)no_addr;
  97. data.no_addr = no_addr;
  98. data.skipped = 0;
  99. return iget5_locked(sb, hash, iget_skip_test, iget_skip_set, &data);
  100. }
  101. /**
  102. * GFS2 lookup code fills in vfs inode contents based on info obtained
  103. * from directory entry inside gfs2_inode_lookup(). This has caused issues
  104. * with NFS code path since its get_dentry routine doesn't have the relevant
  105. * directory entry when gfs2_inode_lookup() is invoked. Part of the code
  106. * segment inside gfs2_inode_lookup code needs to get moved around.
  107. *
  108. * Clears I_NEW as well.
  109. **/
  110. void gfs2_set_iop(struct inode *inode)
  111. {
  112. struct gfs2_sbd *sdp = GFS2_SB(inode);
  113. umode_t mode = inode->i_mode;
  114. if (S_ISREG(mode)) {
  115. inode->i_op = &gfs2_file_iops;
  116. if (gfs2_localflocks(sdp))
  117. inode->i_fop = &gfs2_file_fops_nolock;
  118. else
  119. inode->i_fop = &gfs2_file_fops;
  120. } else if (S_ISDIR(mode)) {
  121. inode->i_op = &gfs2_dir_iops;
  122. if (gfs2_localflocks(sdp))
  123. inode->i_fop = &gfs2_dir_fops_nolock;
  124. else
  125. inode->i_fop = &gfs2_dir_fops;
  126. } else if (S_ISLNK(mode)) {
  127. inode->i_op = &gfs2_symlink_iops;
  128. } else {
  129. inode->i_op = &gfs2_file_iops;
  130. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  131. }
  132. unlock_new_inode(inode);
  133. }
  134. /**
  135. * gfs2_inode_lookup - Lookup an inode
  136. * @sb: The super block
  137. * @no_addr: The inode number
  138. * @type: The type of the inode
  139. *
  140. * Returns: A VFS inode, or an error
  141. */
  142. struct inode *gfs2_inode_lookup(struct super_block *sb,
  143. unsigned int type,
  144. u64 no_addr,
  145. u64 no_formal_ino)
  146. {
  147. struct inode *inode;
  148. struct gfs2_inode *ip;
  149. struct gfs2_glock *io_gl;
  150. int error;
  151. inode = gfs2_iget(sb, no_addr);
  152. ip = GFS2_I(inode);
  153. if (!inode)
  154. return ERR_PTR(-ENOBUFS);
  155. if (inode->i_state & I_NEW) {
  156. struct gfs2_sbd *sdp = GFS2_SB(inode);
  157. ip->i_no_formal_ino = no_formal_ino;
  158. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  159. if (unlikely(error))
  160. goto fail;
  161. ip->i_gl->gl_object = ip;
  162. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  163. if (unlikely(error))
  164. goto fail_put;
  165. set_bit(GIF_INVALID, &ip->i_flags);
  166. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  167. if (unlikely(error))
  168. goto fail_iopen;
  169. ip->i_iopen_gh.gh_gl->gl_object = ip;
  170. gfs2_glock_put(io_gl);
  171. if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
  172. goto gfs2_nfsbypass;
  173. inode->i_mode = DT2IF(type);
  174. /*
  175. * We must read the inode in order to work out its type in
  176. * this case. Note that this doesn't happen often as we normally
  177. * know the type beforehand. This code path only occurs during
  178. * unlinked inode recovery (where it is safe to do this glock,
  179. * which is not true in the general case).
  180. */
  181. if (type == DT_UNKNOWN) {
  182. struct gfs2_holder gh;
  183. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  184. if (unlikely(error))
  185. goto fail_glock;
  186. /* Inode is now uptodate */
  187. gfs2_glock_dq_uninit(&gh);
  188. }
  189. gfs2_set_iop(inode);
  190. }
  191. gfs2_nfsbypass:
  192. return inode;
  193. fail_glock:
  194. gfs2_glock_dq(&ip->i_iopen_gh);
  195. fail_iopen:
  196. gfs2_glock_put(io_gl);
  197. fail_put:
  198. if (inode->i_state & I_NEW)
  199. ip->i_gl->gl_object = NULL;
  200. gfs2_glock_put(ip->i_gl);
  201. fail:
  202. if (inode->i_state & I_NEW)
  203. iget_failed(inode);
  204. else
  205. iput(inode);
  206. return ERR_PTR(error);
  207. }
  208. /**
  209. * gfs2_process_unlinked_inode - Lookup an unlinked inode for reclamation
  210. * and try to reclaim it by doing iput.
  211. *
  212. * This function assumes no rgrp locks are currently held.
  213. *
  214. * @sb: The super block
  215. * no_addr: The inode number
  216. *
  217. */
  218. void gfs2_process_unlinked_inode(struct super_block *sb, u64 no_addr)
  219. {
  220. struct gfs2_sbd *sdp;
  221. struct gfs2_inode *ip;
  222. struct gfs2_glock *io_gl;
  223. int error;
  224. struct gfs2_holder gh;
  225. struct inode *inode;
  226. inode = gfs2_iget_skip(sb, no_addr);
  227. if (!inode)
  228. return;
  229. /* If it's not a new inode, someone's using it, so leave it alone. */
  230. if (!(inode->i_state & I_NEW)) {
  231. iput(inode);
  232. return;
  233. }
  234. ip = GFS2_I(inode);
  235. sdp = GFS2_SB(inode);
  236. ip->i_no_formal_ino = -1;
  237. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  238. if (unlikely(error))
  239. goto fail;
  240. ip->i_gl->gl_object = ip;
  241. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  242. if (unlikely(error))
  243. goto fail_put;
  244. set_bit(GIF_INVALID, &ip->i_flags);
  245. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, LM_FLAG_TRY | GL_EXACT,
  246. &ip->i_iopen_gh);
  247. if (unlikely(error))
  248. goto fail_iopen;
  249. ip->i_iopen_gh.gh_gl->gl_object = ip;
  250. gfs2_glock_put(io_gl);
  251. inode->i_mode = DT2IF(DT_UNKNOWN);
  252. /*
  253. * We must read the inode in order to work out its type in
  254. * this case. Note that this doesn't happen often as we normally
  255. * know the type beforehand. This code path only occurs during
  256. * unlinked inode recovery (where it is safe to do this glock,
  257. * which is not true in the general case).
  258. */
  259. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY,
  260. &gh);
  261. if (unlikely(error))
  262. goto fail_glock;
  263. /* Inode is now uptodate */
  264. gfs2_glock_dq_uninit(&gh);
  265. gfs2_set_iop(inode);
  266. /* The iput will cause it to be deleted. */
  267. iput(inode);
  268. return;
  269. fail_glock:
  270. gfs2_glock_dq(&ip->i_iopen_gh);
  271. fail_iopen:
  272. gfs2_glock_put(io_gl);
  273. fail_put:
  274. ip->i_gl->gl_object = NULL;
  275. gfs2_glock_put(ip->i_gl);
  276. fail:
  277. iget_failed(inode);
  278. return;
  279. }
  280. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  281. {
  282. const struct gfs2_dinode *str = buf;
  283. struct timespec atime;
  284. u16 height, depth;
  285. if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
  286. goto corrupt;
  287. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  288. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  289. ip->i_inode.i_rdev = 0;
  290. switch (ip->i_inode.i_mode & S_IFMT) {
  291. case S_IFBLK:
  292. case S_IFCHR:
  293. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  294. be32_to_cpu(str->di_minor));
  295. break;
  296. };
  297. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  298. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  299. /*
  300. * We will need to review setting the nlink count here in the
  301. * light of the forthcoming ro bind mount work. This is a reminder
  302. * to do that.
  303. */
  304. ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
  305. ip->i_disksize = be64_to_cpu(str->di_size);
  306. i_size_write(&ip->i_inode, ip->i_disksize);
  307. gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
  308. atime.tv_sec = be64_to_cpu(str->di_atime);
  309. atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
  310. if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
  311. ip->i_inode.i_atime = atime;
  312. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  313. ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
  314. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  315. ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
  316. ip->i_goal = be64_to_cpu(str->di_goal_meta);
  317. ip->i_generation = be64_to_cpu(str->di_generation);
  318. ip->i_diskflags = be32_to_cpu(str->di_flags);
  319. gfs2_set_inode_flags(&ip->i_inode);
  320. height = be16_to_cpu(str->di_height);
  321. if (unlikely(height > GFS2_MAX_META_HEIGHT))
  322. goto corrupt;
  323. ip->i_height = (u8)height;
  324. depth = be16_to_cpu(str->di_depth);
  325. if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
  326. goto corrupt;
  327. ip->i_depth = (u8)depth;
  328. ip->i_entries = be32_to_cpu(str->di_entries);
  329. ip->i_eattr = be64_to_cpu(str->di_eattr);
  330. if (S_ISREG(ip->i_inode.i_mode))
  331. gfs2_set_aops(&ip->i_inode);
  332. return 0;
  333. corrupt:
  334. if (gfs2_consist_inode(ip))
  335. gfs2_dinode_print(ip);
  336. return -EIO;
  337. }
  338. /**
  339. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  340. * @ip: The GFS2 inode
  341. *
  342. * Returns: errno
  343. */
  344. int gfs2_inode_refresh(struct gfs2_inode *ip)
  345. {
  346. struct buffer_head *dibh;
  347. int error;
  348. error = gfs2_meta_inode_buffer(ip, &dibh);
  349. if (error)
  350. return error;
  351. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  352. brelse(dibh);
  353. return -EIO;
  354. }
  355. error = gfs2_dinode_in(ip, dibh->b_data);
  356. brelse(dibh);
  357. clear_bit(GIF_INVALID, &ip->i_flags);
  358. return error;
  359. }
  360. int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  361. {
  362. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  363. struct gfs2_alloc *al;
  364. struct gfs2_rgrpd *rgd;
  365. int error;
  366. if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
  367. if (gfs2_consist_inode(ip))
  368. gfs2_dinode_print(ip);
  369. return -EIO;
  370. }
  371. al = gfs2_alloc_get(ip);
  372. if (!al)
  373. return -ENOMEM;
  374. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  375. if (error)
  376. goto out;
  377. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  378. if (error)
  379. goto out_qs;
  380. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  381. if (!rgd) {
  382. gfs2_consist_inode(ip);
  383. error = -EIO;
  384. goto out_rindex_relse;
  385. }
  386. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  387. &al->al_rgd_gh);
  388. if (error)
  389. goto out_rindex_relse;
  390. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
  391. if (error)
  392. goto out_rg_gunlock;
  393. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  394. set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
  395. gfs2_free_di(rgd, ip);
  396. gfs2_trans_end(sdp);
  397. out_rg_gunlock:
  398. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  399. out_rindex_relse:
  400. gfs2_glock_dq_uninit(&al->al_ri_gh);
  401. out_qs:
  402. gfs2_quota_unhold(ip);
  403. out:
  404. gfs2_alloc_put(ip);
  405. return error;
  406. }
  407. /**
  408. * gfs2_change_nlink - Change nlink count on inode
  409. * @ip: The GFS2 inode
  410. * @diff: The change in the nlink count required
  411. *
  412. * Returns: errno
  413. */
  414. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  415. {
  416. struct buffer_head *dibh;
  417. u32 nlink;
  418. int error;
  419. BUG_ON(diff != 1 && diff != -1);
  420. nlink = ip->i_inode.i_nlink + diff;
  421. /* If we are reducing the nlink count, but the new value ends up being
  422. bigger than the old one, we must have underflowed. */
  423. if (diff < 0 && nlink > ip->i_inode.i_nlink) {
  424. if (gfs2_consist_inode(ip))
  425. gfs2_dinode_print(ip);
  426. return -EIO;
  427. }
  428. error = gfs2_meta_inode_buffer(ip, &dibh);
  429. if (error)
  430. return error;
  431. if (diff > 0)
  432. inc_nlink(&ip->i_inode);
  433. else
  434. drop_nlink(&ip->i_inode);
  435. ip->i_inode.i_ctime = CURRENT_TIME;
  436. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  437. gfs2_dinode_out(ip, dibh->b_data);
  438. brelse(dibh);
  439. mark_inode_dirty(&ip->i_inode);
  440. if (ip->i_inode.i_nlink == 0)
  441. gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
  442. return error;
  443. }
  444. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  445. {
  446. struct qstr qstr;
  447. struct inode *inode;
  448. gfs2_str2qstr(&qstr, name);
  449. inode = gfs2_lookupi(dip, &qstr, 1);
  450. /* gfs2_lookupi has inconsistent callers: vfs
  451. * related routines expect NULL for no entry found,
  452. * gfs2_lookup_simple callers expect ENOENT
  453. * and do not check for NULL.
  454. */
  455. if (inode == NULL)
  456. return ERR_PTR(-ENOENT);
  457. else
  458. return inode;
  459. }
  460. /**
  461. * gfs2_lookupi - Look up a filename in a directory and return its inode
  462. * @d_gh: An initialized holder for the directory glock
  463. * @name: The name of the inode to look for
  464. * @is_root: If 1, ignore the caller's permissions
  465. * @i_gh: An uninitialized holder for the new inode glock
  466. *
  467. * This can be called via the VFS filldir function when NFS is doing
  468. * a readdirplus and the inode which its intending to stat isn't
  469. * already in cache. In this case we must not take the directory glock
  470. * again, since the readdir call will have already taken that lock.
  471. *
  472. * Returns: errno
  473. */
  474. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  475. int is_root)
  476. {
  477. struct super_block *sb = dir->i_sb;
  478. struct gfs2_inode *dip = GFS2_I(dir);
  479. struct gfs2_holder d_gh;
  480. int error = 0;
  481. struct inode *inode = NULL;
  482. int unlock = 0;
  483. if (!name->len || name->len > GFS2_FNAMESIZE)
  484. return ERR_PTR(-ENAMETOOLONG);
  485. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  486. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  487. dir == sb->s_root->d_inode)) {
  488. igrab(dir);
  489. return dir;
  490. }
  491. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  492. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  493. if (error)
  494. return ERR_PTR(error);
  495. unlock = 1;
  496. }
  497. if (!is_root) {
  498. error = gfs2_permission(dir, MAY_EXEC);
  499. if (error)
  500. goto out;
  501. }
  502. inode = gfs2_dir_search(dir, name);
  503. if (IS_ERR(inode))
  504. error = PTR_ERR(inode);
  505. out:
  506. if (unlock)
  507. gfs2_glock_dq_uninit(&d_gh);
  508. if (error == -ENOENT)
  509. return NULL;
  510. return inode ? inode : ERR_PTR(error);
  511. }
  512. /**
  513. * create_ok - OK to create a new on-disk inode here?
  514. * @dip: Directory in which dinode is to be created
  515. * @name: Name of new dinode
  516. * @mode:
  517. *
  518. * Returns: errno
  519. */
  520. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  521. unsigned int mode)
  522. {
  523. int error;
  524. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  525. if (error)
  526. return error;
  527. /* Don't create entries in an unlinked directory */
  528. if (!dip->i_inode.i_nlink)
  529. return -EPERM;
  530. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  531. switch (error) {
  532. case -ENOENT:
  533. error = 0;
  534. break;
  535. case 0:
  536. return -EEXIST;
  537. default:
  538. return error;
  539. }
  540. if (dip->i_entries == (u32)-1)
  541. return -EFBIG;
  542. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  543. return -EMLINK;
  544. return 0;
  545. }
  546. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  547. unsigned int *uid, unsigned int *gid)
  548. {
  549. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  550. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  551. if (S_ISDIR(*mode))
  552. *mode |= S_ISUID;
  553. else if (dip->i_inode.i_uid != current_fsuid())
  554. *mode &= ~07111;
  555. *uid = dip->i_inode.i_uid;
  556. } else
  557. *uid = current_fsuid();
  558. if (dip->i_inode.i_mode & S_ISGID) {
  559. if (S_ISDIR(*mode))
  560. *mode |= S_ISGID;
  561. *gid = dip->i_inode.i_gid;
  562. } else
  563. *gid = current_fsgid();
  564. }
  565. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  566. {
  567. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  568. int error;
  569. if (gfs2_alloc_get(dip) == NULL)
  570. return -ENOMEM;
  571. dip->i_alloc->al_requested = RES_DINODE;
  572. error = gfs2_inplace_reserve(dip);
  573. if (error)
  574. goto out;
  575. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  576. if (error)
  577. goto out_ipreserv;
  578. error = gfs2_alloc_di(dip, no_addr, generation);
  579. gfs2_trans_end(sdp);
  580. out_ipreserv:
  581. gfs2_inplace_release(dip);
  582. out:
  583. gfs2_alloc_put(dip);
  584. return error;
  585. }
  586. /**
  587. * init_dinode - Fill in a new dinode structure
  588. * @dip: the directory this inode is being created in
  589. * @gl: The glock covering the new inode
  590. * @inum: the inode number
  591. * @mode: the file permissions
  592. * @uid:
  593. * @gid:
  594. *
  595. */
  596. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  597. const struct gfs2_inum_host *inum, unsigned int mode,
  598. unsigned int uid, unsigned int gid,
  599. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  600. {
  601. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  602. struct gfs2_dinode *di;
  603. struct buffer_head *dibh;
  604. struct timespec tv = CURRENT_TIME;
  605. dibh = gfs2_meta_new(gl, inum->no_addr);
  606. gfs2_trans_add_bh(gl, dibh, 1);
  607. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  608. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  609. di = (struct gfs2_dinode *)dibh->b_data;
  610. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  611. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  612. di->di_mode = cpu_to_be32(mode);
  613. di->di_uid = cpu_to_be32(uid);
  614. di->di_gid = cpu_to_be32(gid);
  615. di->di_nlink = 0;
  616. di->di_size = 0;
  617. di->di_blocks = cpu_to_be64(1);
  618. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  619. di->di_major = cpu_to_be32(MAJOR(dev));
  620. di->di_minor = cpu_to_be32(MINOR(dev));
  621. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  622. di->di_generation = cpu_to_be64(*generation);
  623. di->di_flags = 0;
  624. if (S_ISREG(mode)) {
  625. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  626. gfs2_tune_get(sdp, gt_new_files_jdata))
  627. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  628. } else if (S_ISDIR(mode)) {
  629. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  630. GFS2_DIF_INHERIT_JDATA);
  631. }
  632. di->__pad1 = 0;
  633. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  634. di->di_height = 0;
  635. di->__pad2 = 0;
  636. di->__pad3 = 0;
  637. di->di_depth = 0;
  638. di->di_entries = 0;
  639. memset(&di->__pad4, 0, sizeof(di->__pad4));
  640. di->di_eattr = 0;
  641. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  642. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  643. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  644. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  645. set_buffer_uptodate(dibh);
  646. *bhp = dibh;
  647. }
  648. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  649. unsigned int mode, const struct gfs2_inum_host *inum,
  650. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  651. {
  652. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  653. unsigned int uid, gid;
  654. int error;
  655. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  656. if (!gfs2_alloc_get(dip))
  657. return -ENOMEM;
  658. error = gfs2_quota_lock(dip, uid, gid);
  659. if (error)
  660. goto out;
  661. error = gfs2_quota_check(dip, uid, gid);
  662. if (error)
  663. goto out_quota;
  664. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  665. if (error)
  666. goto out_quota;
  667. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
  668. gfs2_quota_change(dip, +1, uid, gid);
  669. gfs2_trans_end(sdp);
  670. out_quota:
  671. gfs2_quota_unlock(dip);
  672. out:
  673. gfs2_alloc_put(dip);
  674. return error;
  675. }
  676. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  677. struct gfs2_inode *ip)
  678. {
  679. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  680. struct gfs2_alloc *al;
  681. int alloc_required;
  682. struct buffer_head *dibh;
  683. int error;
  684. al = gfs2_alloc_get(dip);
  685. if (!al)
  686. return -ENOMEM;
  687. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  688. if (error)
  689. goto fail;
  690. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  691. if (alloc_required < 0)
  692. goto fail_quota_locks;
  693. if (alloc_required) {
  694. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  695. if (error)
  696. goto fail_quota_locks;
  697. al->al_requested = sdp->sd_max_dirres;
  698. error = gfs2_inplace_reserve(dip);
  699. if (error)
  700. goto fail_quota_locks;
  701. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  702. al->al_rgd->rd_length +
  703. 2 * RES_DINODE +
  704. RES_STATFS + RES_QUOTA, 0);
  705. if (error)
  706. goto fail_ipreserv;
  707. } else {
  708. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  709. if (error)
  710. goto fail_quota_locks;
  711. }
  712. error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
  713. if (error)
  714. goto fail_end_trans;
  715. error = gfs2_meta_inode_buffer(ip, &dibh);
  716. if (error)
  717. goto fail_end_trans;
  718. ip->i_inode.i_nlink = 1;
  719. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  720. gfs2_dinode_out(ip, dibh->b_data);
  721. brelse(dibh);
  722. return 0;
  723. fail_end_trans:
  724. gfs2_trans_end(sdp);
  725. fail_ipreserv:
  726. if (dip->i_alloc->al_rgd)
  727. gfs2_inplace_release(dip);
  728. fail_quota_locks:
  729. gfs2_quota_unlock(dip);
  730. fail:
  731. gfs2_alloc_put(dip);
  732. return error;
  733. }
  734. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  735. {
  736. int err;
  737. size_t len;
  738. void *value;
  739. char *name;
  740. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  741. &name, &value, &len);
  742. if (err) {
  743. if (err == -EOPNOTSUPP)
  744. return 0;
  745. return err;
  746. }
  747. err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
  748. GFS2_EATYPE_SECURITY);
  749. kfree(value);
  750. kfree(name);
  751. return err;
  752. }
  753. /**
  754. * gfs2_createi - Create a new inode
  755. * @ghs: An array of two holders
  756. * @name: The name of the new file
  757. * @mode: the permissions on the new inode
  758. *
  759. * @ghs[0] is an initialized holder for the directory
  760. * @ghs[1] is the holder for the inode lock
  761. *
  762. * If the return value is not NULL, the glocks on both the directory and the new
  763. * file are held. A transaction has been started and an inplace reservation
  764. * is held, as well.
  765. *
  766. * Returns: An inode
  767. */
  768. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  769. unsigned int mode, dev_t dev)
  770. {
  771. struct inode *inode = NULL;
  772. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  773. struct inode *dir = &dip->i_inode;
  774. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  775. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  776. int error;
  777. u64 generation;
  778. struct buffer_head *bh = NULL;
  779. if (!name->len || name->len > GFS2_FNAMESIZE)
  780. return ERR_PTR(-ENAMETOOLONG);
  781. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  782. error = gfs2_glock_nq(ghs);
  783. if (error)
  784. goto fail;
  785. error = create_ok(dip, name, mode);
  786. if (error)
  787. goto fail_gunlock;
  788. error = alloc_dinode(dip, &inum.no_addr, &generation);
  789. if (error)
  790. goto fail_gunlock;
  791. inum.no_formal_ino = generation;
  792. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  793. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  794. if (error)
  795. goto fail_gunlock;
  796. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
  797. if (error)
  798. goto fail_gunlock2;
  799. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  800. inum.no_formal_ino);
  801. if (IS_ERR(inode))
  802. goto fail_gunlock2;
  803. error = gfs2_inode_refresh(GFS2_I(inode));
  804. if (error)
  805. goto fail_gunlock2;
  806. error = gfs2_acl_create(dip, inode);
  807. if (error)
  808. goto fail_gunlock2;
  809. error = gfs2_security_init(dip, GFS2_I(inode));
  810. if (error)
  811. goto fail_gunlock2;
  812. error = link_dinode(dip, name, GFS2_I(inode));
  813. if (error)
  814. goto fail_gunlock2;
  815. if (bh)
  816. brelse(bh);
  817. return inode;
  818. fail_gunlock2:
  819. gfs2_glock_dq_uninit(ghs + 1);
  820. if (inode && !IS_ERR(inode))
  821. iput(inode);
  822. fail_gunlock:
  823. gfs2_glock_dq(ghs);
  824. fail:
  825. if (bh)
  826. brelse(bh);
  827. return ERR_PTR(error);
  828. }
  829. static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  830. {
  831. struct buffer_head *dibh;
  832. int error;
  833. error = gfs2_meta_inode_buffer(ip, &dibh);
  834. if (!error) {
  835. error = inode_setattr(&ip->i_inode, attr);
  836. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  837. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  838. gfs2_dinode_out(ip, dibh->b_data);
  839. brelse(dibh);
  840. }
  841. return error;
  842. }
  843. /**
  844. * gfs2_setattr_simple -
  845. * @ip:
  846. * @attr:
  847. *
  848. * Called with a reference on the vnode.
  849. *
  850. * Returns: errno
  851. */
  852. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  853. {
  854. int error;
  855. if (current->journal_info)
  856. return __gfs2_setattr_simple(ip, attr);
  857. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  858. if (error)
  859. return error;
  860. error = __gfs2_setattr_simple(ip, attr);
  861. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  862. return error;
  863. }
  864. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  865. {
  866. struct gfs2_dinode *str = buf;
  867. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  868. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  869. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  870. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  871. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  872. str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  873. str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  874. str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  875. str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
  876. str->di_size = cpu_to_be64(ip->i_disksize);
  877. str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  878. str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  879. str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
  880. str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
  881. str->di_goal_meta = cpu_to_be64(ip->i_goal);
  882. str->di_goal_data = cpu_to_be64(ip->i_goal);
  883. str->di_generation = cpu_to_be64(ip->i_generation);
  884. str->di_flags = cpu_to_be32(ip->i_diskflags);
  885. str->di_height = cpu_to_be16(ip->i_height);
  886. str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
  887. !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
  888. GFS2_FORMAT_DE : 0);
  889. str->di_depth = cpu_to_be16(ip->i_depth);
  890. str->di_entries = cpu_to_be32(ip->i_entries);
  891. str->di_eattr = cpu_to_be64(ip->i_eattr);
  892. str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  893. str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
  894. str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
  895. }
  896. void gfs2_dinode_print(const struct gfs2_inode *ip)
  897. {
  898. printk(KERN_INFO " no_formal_ino = %llu\n",
  899. (unsigned long long)ip->i_no_formal_ino);
  900. printk(KERN_INFO " no_addr = %llu\n",
  901. (unsigned long long)ip->i_no_addr);
  902. printk(KERN_INFO " i_disksize = %llu\n",
  903. (unsigned long long)ip->i_disksize);
  904. printk(KERN_INFO " blocks = %llu\n",
  905. (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
  906. printk(KERN_INFO " i_goal = %llu\n",
  907. (unsigned long long)ip->i_goal);
  908. printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
  909. printk(KERN_INFO " i_height = %u\n", ip->i_height);
  910. printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
  911. printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
  912. printk(KERN_INFO " i_eattr = %llu\n",
  913. (unsigned long long)ip->i_eattr);
  914. }