inode.c 23 KB

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