inode.c 27 KB

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