inode.c 24 KB

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