inode.c 24 KB

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