inode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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, int non_block)
  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 = non_block;
  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. /**
  215. * gfs2_set_nlink - Set the inode's link count based on on-disk info
  216. * @inode: The inode in question
  217. * @nlink: The link count
  218. *
  219. * If the link count has hit zero, it must never be raised, whatever the
  220. * on-disk inode might say. When new struct inodes are created the link
  221. * count is set to 1, so that we can safely use this test even when reading
  222. * in on disk information for the first time.
  223. */
  224. static void gfs2_set_nlink(struct inode *inode, u32 nlink)
  225. {
  226. /*
  227. * We will need to review setting the nlink count here in the
  228. * light of the forthcoming ro bind mount work. This is a reminder
  229. * to do that.
  230. */
  231. if ((inode->i_nlink != nlink) && (inode->i_nlink != 0)) {
  232. if (nlink == 0)
  233. clear_nlink(inode);
  234. else
  235. inode->i_nlink = nlink;
  236. }
  237. }
  238. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  239. {
  240. const struct gfs2_dinode *str = buf;
  241. struct timespec atime;
  242. u16 height, depth;
  243. if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
  244. goto corrupt;
  245. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  246. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  247. ip->i_inode.i_rdev = 0;
  248. switch (ip->i_inode.i_mode & S_IFMT) {
  249. case S_IFBLK:
  250. case S_IFCHR:
  251. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  252. be32_to_cpu(str->di_minor));
  253. break;
  254. };
  255. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  256. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  257. gfs2_set_nlink(&ip->i_inode, be32_to_cpu(str->di_nlink));
  258. i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
  259. gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
  260. atime.tv_sec = be64_to_cpu(str->di_atime);
  261. atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
  262. if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
  263. ip->i_inode.i_atime = atime;
  264. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  265. ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
  266. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  267. ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
  268. ip->i_goal = be64_to_cpu(str->di_goal_meta);
  269. ip->i_generation = be64_to_cpu(str->di_generation);
  270. ip->i_diskflags = be32_to_cpu(str->di_flags);
  271. gfs2_set_inode_flags(&ip->i_inode);
  272. height = be16_to_cpu(str->di_height);
  273. if (unlikely(height > GFS2_MAX_META_HEIGHT))
  274. goto corrupt;
  275. ip->i_height = (u8)height;
  276. depth = be16_to_cpu(str->di_depth);
  277. if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
  278. goto corrupt;
  279. ip->i_depth = (u8)depth;
  280. ip->i_entries = be32_to_cpu(str->di_entries);
  281. ip->i_eattr = be64_to_cpu(str->di_eattr);
  282. if (S_ISREG(ip->i_inode.i_mode))
  283. gfs2_set_aops(&ip->i_inode);
  284. return 0;
  285. corrupt:
  286. gfs2_consist_inode(ip);
  287. return -EIO;
  288. }
  289. /**
  290. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  291. * @ip: The GFS2 inode
  292. *
  293. * Returns: errno
  294. */
  295. int gfs2_inode_refresh(struct gfs2_inode *ip)
  296. {
  297. struct buffer_head *dibh;
  298. int error;
  299. error = gfs2_meta_inode_buffer(ip, &dibh);
  300. if (error)
  301. return error;
  302. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  303. brelse(dibh);
  304. return -EIO;
  305. }
  306. error = gfs2_dinode_in(ip, dibh->b_data);
  307. brelse(dibh);
  308. clear_bit(GIF_INVALID, &ip->i_flags);
  309. return error;
  310. }
  311. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  312. {
  313. struct qstr qstr;
  314. struct inode *inode;
  315. gfs2_str2qstr(&qstr, name);
  316. inode = gfs2_lookupi(dip, &qstr, 1);
  317. /* gfs2_lookupi has inconsistent callers: vfs
  318. * related routines expect NULL for no entry found,
  319. * gfs2_lookup_simple callers expect ENOENT
  320. * and do not check for NULL.
  321. */
  322. if (inode == NULL)
  323. return ERR_PTR(-ENOENT);
  324. else
  325. return inode;
  326. }
  327. /**
  328. * gfs2_lookupi - Look up a filename in a directory and return its inode
  329. * @d_gh: An initialized holder for the directory glock
  330. * @name: The name of the inode to look for
  331. * @is_root: If 1, ignore the caller's permissions
  332. * @i_gh: An uninitialized holder for the new inode glock
  333. *
  334. * This can be called via the VFS filldir function when NFS is doing
  335. * a readdirplus and the inode which its intending to stat isn't
  336. * already in cache. In this case we must not take the directory glock
  337. * again, since the readdir call will have already taken that lock.
  338. *
  339. * Returns: errno
  340. */
  341. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  342. int is_root)
  343. {
  344. struct super_block *sb = dir->i_sb;
  345. struct gfs2_inode *dip = GFS2_I(dir);
  346. struct gfs2_holder d_gh;
  347. int error = 0;
  348. struct inode *inode = NULL;
  349. int unlock = 0;
  350. if (!name->len || name->len > GFS2_FNAMESIZE)
  351. return ERR_PTR(-ENAMETOOLONG);
  352. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  353. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  354. dir == sb->s_root->d_inode)) {
  355. igrab(dir);
  356. return dir;
  357. }
  358. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  359. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  360. if (error)
  361. return ERR_PTR(error);
  362. unlock = 1;
  363. }
  364. if (!is_root) {
  365. error = gfs2_permission(dir, MAY_EXEC, 0);
  366. if (error)
  367. goto out;
  368. }
  369. inode = gfs2_dir_search(dir, name);
  370. if (IS_ERR(inode))
  371. error = PTR_ERR(inode);
  372. out:
  373. if (unlock)
  374. gfs2_glock_dq_uninit(&d_gh);
  375. if (error == -ENOENT)
  376. return NULL;
  377. return inode ? inode : ERR_PTR(error);
  378. }
  379. /**
  380. * create_ok - OK to create a new on-disk inode here?
  381. * @dip: Directory in which dinode is to be created
  382. * @name: Name of new dinode
  383. * @mode:
  384. *
  385. * Returns: errno
  386. */
  387. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  388. unsigned int mode)
  389. {
  390. int error;
  391. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
  392. if (error)
  393. return error;
  394. /* Don't create entries in an unlinked directory */
  395. if (!dip->i_inode.i_nlink)
  396. return -ENOENT;
  397. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  398. switch (error) {
  399. case -ENOENT:
  400. error = 0;
  401. break;
  402. case 0:
  403. return -EEXIST;
  404. default:
  405. return error;
  406. }
  407. if (dip->i_entries == (u32)-1)
  408. return -EFBIG;
  409. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  410. return -EMLINK;
  411. return 0;
  412. }
  413. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  414. unsigned int *uid, unsigned int *gid)
  415. {
  416. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  417. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  418. if (S_ISDIR(*mode))
  419. *mode |= S_ISUID;
  420. else if (dip->i_inode.i_uid != current_fsuid())
  421. *mode &= ~07111;
  422. *uid = dip->i_inode.i_uid;
  423. } else
  424. *uid = current_fsuid();
  425. if (dip->i_inode.i_mode & S_ISGID) {
  426. if (S_ISDIR(*mode))
  427. *mode |= S_ISGID;
  428. *gid = dip->i_inode.i_gid;
  429. } else
  430. *gid = current_fsgid();
  431. }
  432. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  433. {
  434. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  435. int error;
  436. if (gfs2_alloc_get(dip) == NULL)
  437. return -ENOMEM;
  438. dip->i_alloc->al_requested = RES_DINODE;
  439. error = gfs2_inplace_reserve(dip);
  440. if (error)
  441. goto out;
  442. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  443. if (error)
  444. goto out_ipreserv;
  445. error = gfs2_alloc_di(dip, no_addr, generation);
  446. gfs2_trans_end(sdp);
  447. out_ipreserv:
  448. gfs2_inplace_release(dip);
  449. out:
  450. gfs2_alloc_put(dip);
  451. return error;
  452. }
  453. /**
  454. * init_dinode - Fill in a new dinode structure
  455. * @dip: the directory this inode is being created in
  456. * @gl: The glock covering the new inode
  457. * @inum: the inode number
  458. * @mode: the file permissions
  459. * @uid:
  460. * @gid:
  461. *
  462. */
  463. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  464. const struct gfs2_inum_host *inum, unsigned int mode,
  465. unsigned int uid, unsigned int gid,
  466. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  467. {
  468. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  469. struct gfs2_dinode *di;
  470. struct buffer_head *dibh;
  471. struct timespec tv = CURRENT_TIME;
  472. dibh = gfs2_meta_new(gl, inum->no_addr);
  473. gfs2_trans_add_bh(gl, dibh, 1);
  474. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  475. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  476. di = (struct gfs2_dinode *)dibh->b_data;
  477. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  478. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  479. di->di_mode = cpu_to_be32(mode);
  480. di->di_uid = cpu_to_be32(uid);
  481. di->di_gid = cpu_to_be32(gid);
  482. di->di_nlink = 0;
  483. di->di_size = 0;
  484. di->di_blocks = cpu_to_be64(1);
  485. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  486. di->di_major = cpu_to_be32(MAJOR(dev));
  487. di->di_minor = cpu_to_be32(MINOR(dev));
  488. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  489. di->di_generation = cpu_to_be64(*generation);
  490. di->di_flags = 0;
  491. if (S_ISREG(mode)) {
  492. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  493. gfs2_tune_get(sdp, gt_new_files_jdata))
  494. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  495. } else if (S_ISDIR(mode)) {
  496. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  497. GFS2_DIF_INHERIT_JDATA);
  498. }
  499. di->__pad1 = 0;
  500. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  501. di->di_height = 0;
  502. di->__pad2 = 0;
  503. di->__pad3 = 0;
  504. di->di_depth = 0;
  505. di->di_entries = 0;
  506. memset(&di->__pad4, 0, sizeof(di->__pad4));
  507. di->di_eattr = 0;
  508. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  509. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  510. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  511. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  512. set_buffer_uptodate(dibh);
  513. *bhp = dibh;
  514. }
  515. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  516. unsigned int mode, const struct gfs2_inum_host *inum,
  517. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  518. {
  519. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  520. unsigned int uid, gid;
  521. int error;
  522. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  523. if (!gfs2_alloc_get(dip))
  524. return -ENOMEM;
  525. error = gfs2_quota_lock(dip, uid, gid);
  526. if (error)
  527. goto out;
  528. error = gfs2_quota_check(dip, uid, gid);
  529. if (error)
  530. goto out_quota;
  531. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  532. if (error)
  533. goto out_quota;
  534. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
  535. gfs2_quota_change(dip, +1, uid, gid);
  536. gfs2_trans_end(sdp);
  537. out_quota:
  538. gfs2_quota_unlock(dip);
  539. out:
  540. gfs2_alloc_put(dip);
  541. return error;
  542. }
  543. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  544. struct gfs2_inode *ip)
  545. {
  546. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  547. struct gfs2_alloc *al;
  548. int alloc_required;
  549. struct buffer_head *dibh;
  550. int error;
  551. al = gfs2_alloc_get(dip);
  552. if (!al)
  553. return -ENOMEM;
  554. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  555. if (error)
  556. goto fail;
  557. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  558. if (alloc_required < 0)
  559. goto fail_quota_locks;
  560. if (alloc_required) {
  561. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  562. if (error)
  563. goto fail_quota_locks;
  564. al->al_requested = sdp->sd_max_dirres;
  565. error = gfs2_inplace_reserve(dip);
  566. if (error)
  567. goto fail_quota_locks;
  568. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  569. al->al_rgd->rd_length +
  570. 2 * RES_DINODE +
  571. RES_STATFS + RES_QUOTA, 0);
  572. if (error)
  573. goto fail_ipreserv;
  574. } else {
  575. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  576. if (error)
  577. goto fail_quota_locks;
  578. }
  579. error = gfs2_dir_add(&dip->i_inode, name, ip);
  580. if (error)
  581. goto fail_end_trans;
  582. error = gfs2_meta_inode_buffer(ip, &dibh);
  583. if (error)
  584. goto fail_end_trans;
  585. ip->i_inode.i_nlink = 1;
  586. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  587. gfs2_dinode_out(ip, dibh->b_data);
  588. brelse(dibh);
  589. return 0;
  590. fail_end_trans:
  591. gfs2_trans_end(sdp);
  592. fail_ipreserv:
  593. if (dip->i_alloc->al_rgd)
  594. gfs2_inplace_release(dip);
  595. fail_quota_locks:
  596. gfs2_quota_unlock(dip);
  597. fail:
  598. gfs2_alloc_put(dip);
  599. return error;
  600. }
  601. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  602. const struct qstr *qstr)
  603. {
  604. int err;
  605. size_t len;
  606. void *value;
  607. char *name;
  608. err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  609. &name, &value, &len);
  610. if (err) {
  611. if (err == -EOPNOTSUPP)
  612. return 0;
  613. return err;
  614. }
  615. err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
  616. GFS2_EATYPE_SECURITY);
  617. kfree(value);
  618. kfree(name);
  619. return err;
  620. }
  621. /**
  622. * gfs2_createi - Create a new inode
  623. * @ghs: An array of two holders
  624. * @name: The name of the new file
  625. * @mode: the permissions on the new inode
  626. *
  627. * @ghs[0] is an initialized holder for the directory
  628. * @ghs[1] is the holder for the inode lock
  629. *
  630. * If the return value is not NULL, the glocks on both the directory and the new
  631. * file are held. A transaction has been started and an inplace reservation
  632. * is held, as well.
  633. *
  634. * Returns: An inode
  635. */
  636. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  637. unsigned int mode, dev_t dev)
  638. {
  639. struct inode *inode = NULL;
  640. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  641. struct inode *dir = &dip->i_inode;
  642. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  643. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  644. int error;
  645. u64 generation;
  646. struct buffer_head *bh = NULL;
  647. if (!name->len || name->len > GFS2_FNAMESIZE)
  648. return ERR_PTR(-ENAMETOOLONG);
  649. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  650. error = gfs2_glock_nq(ghs);
  651. if (error)
  652. goto fail;
  653. error = create_ok(dip, name, mode);
  654. if (error)
  655. goto fail_gunlock;
  656. error = alloc_dinode(dip, &inum.no_addr, &generation);
  657. if (error)
  658. goto fail_gunlock;
  659. inum.no_formal_ino = generation;
  660. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  661. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  662. if (error)
  663. goto fail_gunlock;
  664. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
  665. if (error)
  666. goto fail_gunlock2;
  667. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  668. inum.no_formal_ino, 0);
  669. if (IS_ERR(inode))
  670. goto fail_gunlock2;
  671. error = gfs2_inode_refresh(GFS2_I(inode));
  672. if (error)
  673. goto fail_gunlock2;
  674. error = gfs2_acl_create(dip, inode);
  675. if (error)
  676. goto fail_gunlock2;
  677. error = gfs2_security_init(dip, GFS2_I(inode), name);
  678. if (error)
  679. goto fail_gunlock2;
  680. error = link_dinode(dip, name, GFS2_I(inode));
  681. if (error)
  682. goto fail_gunlock2;
  683. if (bh)
  684. brelse(bh);
  685. return inode;
  686. fail_gunlock2:
  687. gfs2_glock_dq_uninit(ghs + 1);
  688. if (inode && !IS_ERR(inode))
  689. iput(inode);
  690. fail_gunlock:
  691. gfs2_glock_dq(ghs);
  692. fail:
  693. if (bh)
  694. brelse(bh);
  695. return ERR_PTR(error);
  696. }
  697. static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  698. {
  699. struct inode *inode = &ip->i_inode;
  700. struct buffer_head *dibh;
  701. int error;
  702. error = gfs2_meta_inode_buffer(ip, &dibh);
  703. if (error)
  704. return error;
  705. setattr_copy(inode, attr);
  706. mark_inode_dirty(inode);
  707. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  708. gfs2_dinode_out(ip, dibh->b_data);
  709. brelse(dibh);
  710. return 0;
  711. }
  712. /**
  713. * gfs2_setattr_simple -
  714. * @ip:
  715. * @attr:
  716. *
  717. * Called with a reference on the vnode.
  718. *
  719. * Returns: errno
  720. */
  721. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  722. {
  723. int error;
  724. if (current->journal_info)
  725. return __gfs2_setattr_simple(ip, attr);
  726. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  727. if (error)
  728. return error;
  729. error = __gfs2_setattr_simple(ip, attr);
  730. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  731. return error;
  732. }
  733. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  734. {
  735. struct gfs2_dinode *str = buf;
  736. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  737. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  738. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  739. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  740. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  741. str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  742. str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  743. str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  744. str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
  745. str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
  746. str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  747. str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  748. str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
  749. str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
  750. str->di_goal_meta = cpu_to_be64(ip->i_goal);
  751. str->di_goal_data = cpu_to_be64(ip->i_goal);
  752. str->di_generation = cpu_to_be64(ip->i_generation);
  753. str->di_flags = cpu_to_be32(ip->i_diskflags);
  754. str->di_height = cpu_to_be16(ip->i_height);
  755. str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
  756. !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
  757. GFS2_FORMAT_DE : 0);
  758. str->di_depth = cpu_to_be16(ip->i_depth);
  759. str->di_entries = cpu_to_be32(ip->i_entries);
  760. str->di_eattr = cpu_to_be64(ip->i_eattr);
  761. str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  762. str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
  763. str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
  764. }