inode.c 23 KB

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