inode.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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/lm_interface.h>
  19. #include <linux/security.h>
  20. #include "gfs2.h"
  21. #include "incore.h"
  22. #include "acl.h"
  23. #include "bmap.h"
  24. #include "dir.h"
  25. #include "eattr.h"
  26. #include "glock.h"
  27. #include "glops.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "ops_address.h"
  32. #include "ops_inode.h"
  33. #include "quota.h"
  34. #include "rgrp.h"
  35. #include "trans.h"
  36. #include "util.h"
  37. struct gfs2_inum_range_host {
  38. u64 ir_start;
  39. u64 ir_length;
  40. };
  41. static int iget_test(struct inode *inode, void *opaque)
  42. {
  43. struct gfs2_inode *ip = GFS2_I(inode);
  44. u64 *no_addr = opaque;
  45. if (ip->i_no_addr == *no_addr &&
  46. inode->i_private != NULL)
  47. return 1;
  48. return 0;
  49. }
  50. static int iget_set(struct inode *inode, void *opaque)
  51. {
  52. struct gfs2_inode *ip = GFS2_I(inode);
  53. u64 *no_addr = opaque;
  54. inode->i_ino = (unsigned long)*no_addr;
  55. ip->i_no_addr = *no_addr;
  56. return 0;
  57. }
  58. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
  59. {
  60. unsigned long hash = (unsigned long)no_addr;
  61. return ilookup5(sb, hash, iget_test, &no_addr);
  62. }
  63. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  64. {
  65. unsigned long hash = (unsigned long)no_addr;
  66. return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
  67. }
  68. struct gfs2_skip_data {
  69. u64 no_addr;
  70. int skipped;
  71. };
  72. static int iget_skip_test(struct inode *inode, void *opaque)
  73. {
  74. struct gfs2_inode *ip = GFS2_I(inode);
  75. struct gfs2_skip_data *data = opaque;
  76. if (ip->i_no_addr == data->no_addr && inode->i_private != NULL){
  77. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)){
  78. data->skipped = 1;
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. static int iget_skip_set(struct inode *inode, void *opaque)
  86. {
  87. struct gfs2_inode *ip = GFS2_I(inode);
  88. struct gfs2_skip_data *data = opaque;
  89. if (data->skipped)
  90. return 1;
  91. inode->i_ino = (unsigned long)(data->no_addr);
  92. ip->i_no_addr = data->no_addr;
  93. return 0;
  94. }
  95. static struct inode *gfs2_iget_skip(struct super_block *sb,
  96. u64 no_addr)
  97. {
  98. struct gfs2_skip_data data;
  99. unsigned long hash = (unsigned long)no_addr;
  100. data.no_addr = no_addr;
  101. data.skipped = 0;
  102. return iget5_locked(sb, hash, iget_skip_test, iget_skip_set, &data);
  103. }
  104. /**
  105. * GFS2 lookup code fills in vfs inode contents based on info obtained
  106. * from directory entry inside gfs2_inode_lookup(). This has caused issues
  107. * with NFS code path since its get_dentry routine doesn't have the relevant
  108. * directory entry when gfs2_inode_lookup() is invoked. Part of the code
  109. * segment inside gfs2_inode_lookup code needs to get moved around.
  110. *
  111. * Clean up I_LOCK and I_NEW as well.
  112. **/
  113. void gfs2_set_iop(struct inode *inode)
  114. {
  115. umode_t mode = inode->i_mode;
  116. if (S_ISREG(mode)) {
  117. inode->i_op = &gfs2_file_iops;
  118. inode->i_fop = &gfs2_file_fops;
  119. } else if (S_ISDIR(mode)) {
  120. inode->i_op = &gfs2_dir_iops;
  121. inode->i_fop = &gfs2_dir_fops;
  122. } else if (S_ISLNK(mode)) {
  123. inode->i_op = &gfs2_symlink_iops;
  124. } else {
  125. inode->i_op = &gfs2_dev_iops;
  126. }
  127. unlock_new_inode(inode);
  128. }
  129. /**
  130. * gfs2_inode_lookup - Lookup an inode
  131. * @sb: The super block
  132. * @no_addr: The inode number
  133. * @type: The type of the inode
  134. * @skip_freeing: set this not return an inode if it is currently being freed.
  135. *
  136. * Returns: A VFS inode, or an error
  137. */
  138. struct inode *gfs2_inode_lookup(struct super_block *sb,
  139. unsigned int type,
  140. u64 no_addr,
  141. u64 no_formal_ino, int skip_freeing)
  142. {
  143. struct inode *inode;
  144. struct gfs2_inode *ip;
  145. struct gfs2_glock *io_gl;
  146. int error;
  147. if (skip_freeing)
  148. inode = gfs2_iget_skip(sb, no_addr);
  149. else
  150. inode = gfs2_iget(sb, no_addr);
  151. ip = GFS2_I(inode);
  152. if (!inode)
  153. return ERR_PTR(-ENOBUFS);
  154. if (inode->i_state & I_NEW) {
  155. struct gfs2_sbd *sdp = GFS2_SB(inode);
  156. inode->i_private = ip;
  157. ip->i_no_formal_ino = no_formal_ino;
  158. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  159. if (unlikely(error))
  160. goto fail;
  161. ip->i_gl->gl_object = ip;
  162. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  163. if (unlikely(error))
  164. goto fail_put;
  165. set_bit(GIF_INVALID, &ip->i_flags);
  166. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  167. if (unlikely(error))
  168. goto fail_iopen;
  169. ip->i_iopen_gh.gh_gl->gl_object = ip;
  170. gfs2_glock_put(io_gl);
  171. if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
  172. goto gfs2_nfsbypass;
  173. inode->i_mode = DT2IF(type);
  174. /*
  175. * We must read the inode in order to work out its type in
  176. * this case. Note that this doesn't happen often as we normally
  177. * know the type beforehand. This code path only occurs during
  178. * unlinked inode recovery (where it is safe to do this glock,
  179. * which is not true in the general case).
  180. */
  181. if (type == DT_UNKNOWN) {
  182. struct gfs2_holder gh;
  183. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  184. if (unlikely(error))
  185. goto fail_glock;
  186. /* Inode is now uptodate */
  187. gfs2_glock_dq_uninit(&gh);
  188. }
  189. gfs2_set_iop(inode);
  190. }
  191. gfs2_nfsbypass:
  192. return inode;
  193. fail_glock:
  194. gfs2_glock_dq(&ip->i_iopen_gh);
  195. fail_iopen:
  196. gfs2_glock_put(io_gl);
  197. fail_put:
  198. ip->i_gl->gl_object = NULL;
  199. gfs2_glock_put(ip->i_gl);
  200. fail:
  201. iput(inode);
  202. return ERR_PTR(error);
  203. }
  204. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  205. {
  206. struct gfs2_dinode_host *di = &ip->i_di;
  207. const struct gfs2_dinode *str = buf;
  208. if (ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)) {
  209. if (gfs2_consist_inode(ip))
  210. gfs2_dinode_print(ip);
  211. return -EIO;
  212. }
  213. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  214. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  215. ip->i_inode.i_rdev = 0;
  216. switch (ip->i_inode.i_mode & S_IFMT) {
  217. case S_IFBLK:
  218. case S_IFCHR:
  219. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  220. be32_to_cpu(str->di_minor));
  221. break;
  222. };
  223. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  224. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  225. /*
  226. * We will need to review setting the nlink count here in the
  227. * light of the forthcoming ro bind mount work. This is a reminder
  228. * to do that.
  229. */
  230. ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
  231. di->di_size = be64_to_cpu(str->di_size);
  232. i_size_write(&ip->i_inode, di->di_size);
  233. di->di_blocks = be64_to_cpu(str->di_blocks);
  234. gfs2_set_inode_blocks(&ip->i_inode);
  235. ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
  236. ip->i_inode.i_atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
  237. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  238. ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
  239. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  240. ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
  241. di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
  242. di->di_goal_data = be64_to_cpu(str->di_goal_data);
  243. di->di_generation = be64_to_cpu(str->di_generation);
  244. di->di_flags = be32_to_cpu(str->di_flags);
  245. gfs2_set_inode_flags(&ip->i_inode);
  246. di->di_height = be16_to_cpu(str->di_height);
  247. di->di_depth = be16_to_cpu(str->di_depth);
  248. di->di_entries = be32_to_cpu(str->di_entries);
  249. di->di_eattr = be64_to_cpu(str->di_eattr);
  250. if (S_ISREG(ip->i_inode.i_mode))
  251. gfs2_set_aops(&ip->i_inode);
  252. return 0;
  253. }
  254. /**
  255. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  256. * @ip: The GFS2 inode
  257. *
  258. * Returns: errno
  259. */
  260. int gfs2_inode_refresh(struct gfs2_inode *ip)
  261. {
  262. struct buffer_head *dibh;
  263. int error;
  264. error = gfs2_meta_inode_buffer(ip, &dibh);
  265. if (error)
  266. return error;
  267. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  268. brelse(dibh);
  269. return -EIO;
  270. }
  271. error = gfs2_dinode_in(ip, dibh->b_data);
  272. brelse(dibh);
  273. clear_bit(GIF_INVALID, &ip->i_flags);
  274. return error;
  275. }
  276. int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  277. {
  278. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  279. struct gfs2_alloc *al;
  280. struct gfs2_rgrpd *rgd;
  281. int error;
  282. if (ip->i_di.di_blocks != 1) {
  283. if (gfs2_consist_inode(ip))
  284. gfs2_dinode_print(ip);
  285. return -EIO;
  286. }
  287. al = gfs2_alloc_get(ip);
  288. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  289. if (error)
  290. goto out;
  291. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  292. if (error)
  293. goto out_qs;
  294. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  295. if (!rgd) {
  296. gfs2_consist_inode(ip);
  297. error = -EIO;
  298. goto out_rindex_relse;
  299. }
  300. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  301. &al->al_rgd_gh);
  302. if (error)
  303. goto out_rindex_relse;
  304. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
  305. if (error)
  306. goto out_rg_gunlock;
  307. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  308. set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
  309. gfs2_free_di(rgd, ip);
  310. gfs2_trans_end(sdp);
  311. clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  312. out_rg_gunlock:
  313. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  314. out_rindex_relse:
  315. gfs2_glock_dq_uninit(&al->al_ri_gh);
  316. out_qs:
  317. gfs2_quota_unhold(ip);
  318. out:
  319. gfs2_alloc_put(ip);
  320. return error;
  321. }
  322. /**
  323. * gfs2_change_nlink - Change nlink count on inode
  324. * @ip: The GFS2 inode
  325. * @diff: The change in the nlink count required
  326. *
  327. * Returns: errno
  328. */
  329. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  330. {
  331. struct buffer_head *dibh;
  332. u32 nlink;
  333. int error;
  334. BUG_ON(diff != 1 && diff != -1);
  335. nlink = ip->i_inode.i_nlink + diff;
  336. /* If we are reducing the nlink count, but the new value ends up being
  337. bigger than the old one, we must have underflowed. */
  338. if (diff < 0 && nlink > ip->i_inode.i_nlink) {
  339. if (gfs2_consist_inode(ip))
  340. gfs2_dinode_print(ip);
  341. return -EIO;
  342. }
  343. error = gfs2_meta_inode_buffer(ip, &dibh);
  344. if (error)
  345. return error;
  346. if (diff > 0)
  347. inc_nlink(&ip->i_inode);
  348. else
  349. drop_nlink(&ip->i_inode);
  350. ip->i_inode.i_ctime = CURRENT_TIME;
  351. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  352. gfs2_dinode_out(ip, dibh->b_data);
  353. brelse(dibh);
  354. mark_inode_dirty(&ip->i_inode);
  355. if (ip->i_inode.i_nlink == 0)
  356. gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
  357. return error;
  358. }
  359. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  360. {
  361. struct qstr qstr;
  362. struct inode *inode;
  363. gfs2_str2qstr(&qstr, name);
  364. inode = gfs2_lookupi(dip, &qstr, 1, NULL);
  365. /* gfs2_lookupi has inconsistent callers: vfs
  366. * related routines expect NULL for no entry found,
  367. * gfs2_lookup_simple callers expect ENOENT
  368. * and do not check for NULL.
  369. */
  370. if (inode == NULL)
  371. return ERR_PTR(-ENOENT);
  372. else
  373. return inode;
  374. }
  375. /**
  376. * gfs2_lookupi - Look up a filename in a directory and return its inode
  377. * @d_gh: An initialized holder for the directory glock
  378. * @name: The name of the inode to look for
  379. * @is_root: If 1, ignore the caller's permissions
  380. * @i_gh: An uninitialized holder for the new inode glock
  381. *
  382. * This can be called via the VFS filldir function when NFS is doing
  383. * a readdirplus and the inode which its intending to stat isn't
  384. * already in cache. In this case we must not take the directory glock
  385. * again, since the readdir call will have already taken that lock.
  386. *
  387. * Returns: errno
  388. */
  389. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  390. int is_root, struct nameidata *nd)
  391. {
  392. struct super_block *sb = dir->i_sb;
  393. struct gfs2_inode *dip = GFS2_I(dir);
  394. struct gfs2_holder d_gh;
  395. int error = 0;
  396. struct inode *inode = NULL;
  397. int unlock = 0;
  398. if (!name->len || name->len > GFS2_FNAMESIZE)
  399. return ERR_PTR(-ENAMETOOLONG);
  400. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  401. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  402. dir == sb->s_root->d_inode)) {
  403. igrab(dir);
  404. return dir;
  405. }
  406. if (gfs2_glock_is_locked_by_me(dip->i_gl) == 0) {
  407. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  408. if (error)
  409. return ERR_PTR(error);
  410. unlock = 1;
  411. }
  412. if (!is_root) {
  413. error = permission(dir, MAY_EXEC, NULL);
  414. if (error)
  415. goto out;
  416. }
  417. inode = gfs2_dir_search(dir, name);
  418. if (IS_ERR(inode))
  419. error = PTR_ERR(inode);
  420. out:
  421. if (unlock)
  422. gfs2_glock_dq_uninit(&d_gh);
  423. if (error == -ENOENT)
  424. return NULL;
  425. return inode ? inode : ERR_PTR(error);
  426. }
  427. static void gfs2_inum_range_in(struct gfs2_inum_range_host *ir, const void *buf)
  428. {
  429. const struct gfs2_inum_range *str = buf;
  430. ir->ir_start = be64_to_cpu(str->ir_start);
  431. ir->ir_length = be64_to_cpu(str->ir_length);
  432. }
  433. static void gfs2_inum_range_out(const struct gfs2_inum_range_host *ir, void *buf)
  434. {
  435. struct gfs2_inum_range *str = buf;
  436. str->ir_start = cpu_to_be64(ir->ir_start);
  437. str->ir_length = cpu_to_be64(ir->ir_length);
  438. }
  439. static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
  440. {
  441. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  442. struct buffer_head *bh;
  443. struct gfs2_inum_range_host ir;
  444. int error;
  445. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  446. if (error)
  447. return error;
  448. mutex_lock(&sdp->sd_inum_mutex);
  449. error = gfs2_meta_inode_buffer(ip, &bh);
  450. if (error) {
  451. mutex_unlock(&sdp->sd_inum_mutex);
  452. gfs2_trans_end(sdp);
  453. return error;
  454. }
  455. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  456. if (ir.ir_length) {
  457. *formal_ino = ir.ir_start++;
  458. ir.ir_length--;
  459. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  460. gfs2_inum_range_out(&ir,
  461. bh->b_data + sizeof(struct gfs2_dinode));
  462. brelse(bh);
  463. mutex_unlock(&sdp->sd_inum_mutex);
  464. gfs2_trans_end(sdp);
  465. return 0;
  466. }
  467. brelse(bh);
  468. mutex_unlock(&sdp->sd_inum_mutex);
  469. gfs2_trans_end(sdp);
  470. return 1;
  471. }
  472. static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
  473. {
  474. struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
  475. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
  476. struct gfs2_holder gh;
  477. struct buffer_head *bh;
  478. struct gfs2_inum_range_host ir;
  479. int error;
  480. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  481. if (error)
  482. return error;
  483. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  484. if (error)
  485. goto out;
  486. mutex_lock(&sdp->sd_inum_mutex);
  487. error = gfs2_meta_inode_buffer(ip, &bh);
  488. if (error)
  489. goto out_end_trans;
  490. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  491. if (!ir.ir_length) {
  492. struct buffer_head *m_bh;
  493. u64 x, y;
  494. __be64 z;
  495. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  496. if (error)
  497. goto out_brelse;
  498. z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
  499. x = y = be64_to_cpu(z);
  500. ir.ir_start = x;
  501. ir.ir_length = GFS2_INUM_QUANTUM;
  502. x += GFS2_INUM_QUANTUM;
  503. if (x < y)
  504. gfs2_consist_inode(m_ip);
  505. z = cpu_to_be64(x);
  506. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  507. *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
  508. brelse(m_bh);
  509. }
  510. *formal_ino = ir.ir_start++;
  511. ir.ir_length--;
  512. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  513. gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  514. out_brelse:
  515. brelse(bh);
  516. out_end_trans:
  517. mutex_unlock(&sdp->sd_inum_mutex);
  518. gfs2_trans_end(sdp);
  519. out:
  520. gfs2_glock_dq_uninit(&gh);
  521. return error;
  522. }
  523. static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
  524. {
  525. int error;
  526. error = pick_formal_ino_1(sdp, inum);
  527. if (error <= 0)
  528. return error;
  529. error = pick_formal_ino_2(sdp, inum);
  530. return error;
  531. }
  532. /**
  533. * create_ok - OK to create a new on-disk inode here?
  534. * @dip: Directory in which dinode is to be created
  535. * @name: Name of new dinode
  536. * @mode:
  537. *
  538. * Returns: errno
  539. */
  540. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  541. unsigned int mode)
  542. {
  543. int error;
  544. error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
  545. if (error)
  546. return error;
  547. /* Don't create entries in an unlinked directory */
  548. if (!dip->i_inode.i_nlink)
  549. return -EPERM;
  550. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  551. switch (error) {
  552. case -ENOENT:
  553. error = 0;
  554. break;
  555. case 0:
  556. return -EEXIST;
  557. default:
  558. return error;
  559. }
  560. if (dip->i_di.di_entries == (u32)-1)
  561. return -EFBIG;
  562. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  563. return -EMLINK;
  564. return 0;
  565. }
  566. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  567. unsigned int *uid, unsigned int *gid)
  568. {
  569. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  570. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  571. if (S_ISDIR(*mode))
  572. *mode |= S_ISUID;
  573. else if (dip->i_inode.i_uid != current->fsuid)
  574. *mode &= ~07111;
  575. *uid = dip->i_inode.i_uid;
  576. } else
  577. *uid = current->fsuid;
  578. if (dip->i_inode.i_mode & S_ISGID) {
  579. if (S_ISDIR(*mode))
  580. *mode |= S_ISGID;
  581. *gid = dip->i_inode.i_gid;
  582. } else
  583. *gid = current->fsgid;
  584. }
  585. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  586. {
  587. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  588. int error;
  589. gfs2_alloc_get(dip);
  590. dip->i_alloc.al_requested = RES_DINODE;
  591. error = gfs2_inplace_reserve(dip);
  592. if (error)
  593. goto out;
  594. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  595. if (error)
  596. goto out_ipreserv;
  597. *no_addr = gfs2_alloc_di(dip, generation);
  598. gfs2_trans_end(sdp);
  599. out_ipreserv:
  600. gfs2_inplace_release(dip);
  601. out:
  602. gfs2_alloc_put(dip);
  603. return error;
  604. }
  605. /**
  606. * init_dinode - Fill in a new dinode structure
  607. * @dip: the directory this inode is being created in
  608. * @gl: The glock covering the new inode
  609. * @inum: the inode number
  610. * @mode: the file permissions
  611. * @uid:
  612. * @gid:
  613. *
  614. */
  615. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  616. const struct gfs2_inum_host *inum, unsigned int mode,
  617. unsigned int uid, unsigned int gid,
  618. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  619. {
  620. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  621. struct gfs2_dinode *di;
  622. struct buffer_head *dibh;
  623. struct timespec tv = CURRENT_TIME;
  624. dibh = gfs2_meta_new(gl, inum->no_addr);
  625. gfs2_trans_add_bh(gl, dibh, 1);
  626. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  627. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  628. di = (struct gfs2_dinode *)dibh->b_data;
  629. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  630. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  631. di->di_mode = cpu_to_be32(mode);
  632. di->di_uid = cpu_to_be32(uid);
  633. di->di_gid = cpu_to_be32(gid);
  634. di->di_nlink = 0;
  635. di->di_size = 0;
  636. di->di_blocks = cpu_to_be64(1);
  637. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  638. di->di_major = cpu_to_be32(MAJOR(dev));
  639. di->di_minor = cpu_to_be32(MINOR(dev));
  640. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  641. di->di_generation = cpu_to_be64(*generation);
  642. di->di_flags = 0;
  643. if (S_ISREG(mode)) {
  644. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
  645. gfs2_tune_get(sdp, gt_new_files_jdata))
  646. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  647. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
  648. gfs2_tune_get(sdp, gt_new_files_directio))
  649. di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
  650. } else if (S_ISDIR(mode)) {
  651. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  652. GFS2_DIF_INHERIT_DIRECTIO);
  653. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  654. GFS2_DIF_INHERIT_JDATA);
  655. }
  656. di->__pad1 = 0;
  657. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  658. di->di_height = 0;
  659. di->__pad2 = 0;
  660. di->__pad3 = 0;
  661. di->di_depth = 0;
  662. di->di_entries = 0;
  663. memset(&di->__pad4, 0, sizeof(di->__pad4));
  664. di->di_eattr = 0;
  665. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  666. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  667. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  668. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  669. set_buffer_uptodate(dibh);
  670. *bhp = dibh;
  671. }
  672. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  673. unsigned int mode, const struct gfs2_inum_host *inum,
  674. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  675. {
  676. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  677. unsigned int uid, gid;
  678. int error;
  679. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  680. gfs2_alloc_get(dip);
  681. error = gfs2_quota_lock(dip, uid, gid);
  682. if (error)
  683. goto out;
  684. error = gfs2_quota_check(dip, uid, gid);
  685. if (error)
  686. goto out_quota;
  687. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  688. if (error)
  689. goto out_quota;
  690. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
  691. gfs2_quota_change(dip, +1, uid, gid);
  692. gfs2_trans_end(sdp);
  693. out_quota:
  694. gfs2_quota_unlock(dip);
  695. out:
  696. gfs2_alloc_put(dip);
  697. return error;
  698. }
  699. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  700. struct gfs2_inode *ip)
  701. {
  702. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  703. struct gfs2_alloc *al;
  704. int alloc_required;
  705. struct buffer_head *dibh;
  706. int error;
  707. al = gfs2_alloc_get(dip);
  708. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  709. if (error)
  710. goto fail;
  711. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  712. if (alloc_required < 0)
  713. goto fail;
  714. if (alloc_required) {
  715. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  716. if (error)
  717. goto fail_quota_locks;
  718. al->al_requested = sdp->sd_max_dirres;
  719. error = gfs2_inplace_reserve(dip);
  720. if (error)
  721. goto fail_quota_locks;
  722. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  723. al->al_rgd->rd_length +
  724. 2 * RES_DINODE +
  725. RES_STATFS + RES_QUOTA, 0);
  726. if (error)
  727. goto fail_ipreserv;
  728. } else {
  729. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  730. if (error)
  731. goto fail_quota_locks;
  732. }
  733. error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
  734. if (error)
  735. goto fail_end_trans;
  736. error = gfs2_meta_inode_buffer(ip, &dibh);
  737. if (error)
  738. goto fail_end_trans;
  739. ip->i_inode.i_nlink = 1;
  740. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  741. gfs2_dinode_out(ip, dibh->b_data);
  742. brelse(dibh);
  743. return 0;
  744. fail_end_trans:
  745. gfs2_trans_end(sdp);
  746. fail_ipreserv:
  747. if (dip->i_alloc.al_rgd)
  748. gfs2_inplace_release(dip);
  749. fail_quota_locks:
  750. gfs2_quota_unlock(dip);
  751. fail:
  752. gfs2_alloc_put(dip);
  753. return error;
  754. }
  755. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  756. {
  757. int err;
  758. size_t len;
  759. void *value;
  760. char *name;
  761. struct gfs2_ea_request er;
  762. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  763. &name, &value, &len);
  764. if (err) {
  765. if (err == -EOPNOTSUPP)
  766. return 0;
  767. return err;
  768. }
  769. memset(&er, 0, sizeof(struct gfs2_ea_request));
  770. er.er_type = GFS2_EATYPE_SECURITY;
  771. er.er_name = name;
  772. er.er_data = value;
  773. er.er_name_len = strlen(name);
  774. er.er_data_len = len;
  775. err = gfs2_ea_set_i(ip, &er);
  776. kfree(value);
  777. kfree(name);
  778. return err;
  779. }
  780. /**
  781. * gfs2_createi - Create a new inode
  782. * @ghs: An array of two holders
  783. * @name: The name of the new file
  784. * @mode: the permissions on the new inode
  785. *
  786. * @ghs[0] is an initialized holder for the directory
  787. * @ghs[1] is the holder for the inode lock
  788. *
  789. * If the return value is not NULL, the glocks on both the directory and the new
  790. * file are held. A transaction has been started and an inplace reservation
  791. * is held, as well.
  792. *
  793. * Returns: An inode
  794. */
  795. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  796. unsigned int mode, dev_t dev)
  797. {
  798. struct inode *inode = NULL;
  799. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  800. struct inode *dir = &dip->i_inode;
  801. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  802. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  803. int error;
  804. u64 generation;
  805. struct buffer_head *bh = NULL;
  806. if (!name->len || name->len > GFS2_FNAMESIZE)
  807. return ERR_PTR(-ENAMETOOLONG);
  808. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  809. error = gfs2_glock_nq(ghs);
  810. if (error)
  811. goto fail;
  812. error = create_ok(dip, name, mode);
  813. if (error)
  814. goto fail_gunlock;
  815. error = pick_formal_ino(sdp, &inum.no_formal_ino);
  816. if (error)
  817. goto fail_gunlock;
  818. error = alloc_dinode(dip, &inum.no_addr, &generation);
  819. if (error)
  820. goto fail_gunlock;
  821. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  822. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  823. if (error)
  824. goto fail_gunlock;
  825. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
  826. if (error)
  827. goto fail_gunlock2;
  828. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode),
  829. inum.no_addr,
  830. inum.no_formal_ino, 0);
  831. if (IS_ERR(inode))
  832. goto fail_gunlock2;
  833. error = gfs2_inode_refresh(GFS2_I(inode));
  834. if (error)
  835. goto fail_gunlock2;
  836. error = gfs2_acl_create(dip, GFS2_I(inode));
  837. if (error)
  838. goto fail_gunlock2;
  839. error = gfs2_security_init(dip, GFS2_I(inode));
  840. if (error)
  841. goto fail_gunlock2;
  842. error = link_dinode(dip, name, GFS2_I(inode));
  843. if (error)
  844. goto fail_gunlock2;
  845. if (bh)
  846. brelse(bh);
  847. if (!inode)
  848. return ERR_PTR(-ENOMEM);
  849. return inode;
  850. fail_gunlock2:
  851. gfs2_glock_dq_uninit(ghs + 1);
  852. if (inode)
  853. iput(inode);
  854. fail_gunlock:
  855. gfs2_glock_dq(ghs);
  856. fail:
  857. if (bh)
  858. brelse(bh);
  859. return ERR_PTR(error);
  860. }
  861. /**
  862. * gfs2_rmdiri - Remove a directory
  863. * @dip: The parent directory of the directory to be removed
  864. * @name: The name of the directory to be removed
  865. * @ip: The GFS2 inode of the directory to be removed
  866. *
  867. * Assumes Glocks on dip and ip are held
  868. *
  869. * Returns: errno
  870. */
  871. int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  872. struct gfs2_inode *ip)
  873. {
  874. struct qstr dotname;
  875. int error;
  876. if (ip->i_di.di_entries != 2) {
  877. if (gfs2_consist_inode(ip))
  878. gfs2_dinode_print(ip);
  879. return -EIO;
  880. }
  881. error = gfs2_dir_del(dip, name);
  882. if (error)
  883. return error;
  884. error = gfs2_change_nlink(dip, -1);
  885. if (error)
  886. return error;
  887. gfs2_str2qstr(&dotname, ".");
  888. error = gfs2_dir_del(ip, &dotname);
  889. if (error)
  890. return error;
  891. gfs2_str2qstr(&dotname, "..");
  892. error = gfs2_dir_del(ip, &dotname);
  893. if (error)
  894. return error;
  895. /* It looks odd, but it really should be done twice */
  896. error = gfs2_change_nlink(ip, -1);
  897. if (error)
  898. return error;
  899. error = gfs2_change_nlink(ip, -1);
  900. if (error)
  901. return error;
  902. return error;
  903. }
  904. /*
  905. * gfs2_unlink_ok - check to see that a inode is still in a directory
  906. * @dip: the directory
  907. * @name: the name of the file
  908. * @ip: the inode
  909. *
  910. * Assumes that the lock on (at least) @dip is held.
  911. *
  912. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  913. */
  914. int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  915. const struct gfs2_inode *ip)
  916. {
  917. int error;
  918. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  919. return -EPERM;
  920. if ((dip->i_inode.i_mode & S_ISVTX) &&
  921. dip->i_inode.i_uid != current->fsuid &&
  922. ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
  923. return -EPERM;
  924. if (IS_APPEND(&dip->i_inode))
  925. return -EPERM;
  926. error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
  927. if (error)
  928. return error;
  929. error = gfs2_dir_check(&dip->i_inode, name, ip);
  930. if (error)
  931. return error;
  932. return 0;
  933. }
  934. /*
  935. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  936. * @this: move this
  937. * @to: to here
  938. *
  939. * Follow @to back to the root and make sure we don't encounter @this
  940. * Assumes we already hold the rename lock.
  941. *
  942. * Returns: errno
  943. */
  944. int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  945. {
  946. struct inode *dir = &to->i_inode;
  947. struct super_block *sb = dir->i_sb;
  948. struct inode *tmp;
  949. struct qstr dotdot;
  950. int error = 0;
  951. gfs2_str2qstr(&dotdot, "..");
  952. igrab(dir);
  953. for (;;) {
  954. if (dir == &this->i_inode) {
  955. error = -EINVAL;
  956. break;
  957. }
  958. if (dir == sb->s_root->d_inode) {
  959. error = 0;
  960. break;
  961. }
  962. tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
  963. if (IS_ERR(tmp)) {
  964. error = PTR_ERR(tmp);
  965. break;
  966. }
  967. iput(dir);
  968. dir = tmp;
  969. }
  970. iput(dir);
  971. return error;
  972. }
  973. /**
  974. * gfs2_readlinki - return the contents of a symlink
  975. * @ip: the symlink's inode
  976. * @buf: a pointer to the buffer to be filled
  977. * @len: a pointer to the length of @buf
  978. *
  979. * If @buf is too small, a piece of memory is kmalloc()ed and needs
  980. * to be freed by the caller.
  981. *
  982. * Returns: errno
  983. */
  984. int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
  985. {
  986. struct gfs2_holder i_gh;
  987. struct buffer_head *dibh;
  988. unsigned int x;
  989. int error;
  990. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  991. error = gfs2_glock_nq_atime(&i_gh);
  992. if (error) {
  993. gfs2_holder_uninit(&i_gh);
  994. return error;
  995. }
  996. if (!ip->i_di.di_size) {
  997. gfs2_consist_inode(ip);
  998. error = -EIO;
  999. goto out;
  1000. }
  1001. error = gfs2_meta_inode_buffer(ip, &dibh);
  1002. if (error)
  1003. goto out;
  1004. x = ip->i_di.di_size + 1;
  1005. if (x > *len) {
  1006. *buf = kmalloc(x, GFP_KERNEL);
  1007. if (!*buf) {
  1008. error = -ENOMEM;
  1009. goto out_brelse;
  1010. }
  1011. }
  1012. memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  1013. *len = x;
  1014. out_brelse:
  1015. brelse(dibh);
  1016. out:
  1017. gfs2_glock_dq_uninit(&i_gh);
  1018. return error;
  1019. }
  1020. /**
  1021. * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
  1022. * conditionally update the inode's atime
  1023. * @gh: the holder to acquire
  1024. *
  1025. * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
  1026. * Update if the difference between the current time and the inode's current
  1027. * atime is greater than an interval specified at mount.
  1028. *
  1029. * Returns: errno
  1030. */
  1031. int gfs2_glock_nq_atime(struct gfs2_holder *gh)
  1032. {
  1033. struct gfs2_glock *gl = gh->gh_gl;
  1034. struct gfs2_sbd *sdp = gl->gl_sbd;
  1035. struct gfs2_inode *ip = gl->gl_object;
  1036. s64 quantum = gfs2_tune_get(sdp, gt_atime_quantum);
  1037. unsigned int state;
  1038. int flags;
  1039. int error;
  1040. struct timespec tv = CURRENT_TIME;
  1041. if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
  1042. gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
  1043. gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
  1044. return -EINVAL;
  1045. state = gh->gh_state;
  1046. flags = gh->gh_flags;
  1047. error = gfs2_glock_nq(gh);
  1048. if (error)
  1049. return error;
  1050. if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
  1051. (sdp->sd_vfs->s_flags & MS_RDONLY))
  1052. return 0;
  1053. if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
  1054. gfs2_glock_dq(gh);
  1055. gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
  1056. gh);
  1057. error = gfs2_glock_nq(gh);
  1058. if (error)
  1059. return error;
  1060. /* Verify that atime hasn't been updated while we were
  1061. trying to get exclusive lock. */
  1062. tv = CURRENT_TIME;
  1063. if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
  1064. struct buffer_head *dibh;
  1065. struct gfs2_dinode *di;
  1066. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1067. if (error == -EROFS)
  1068. return 0;
  1069. if (error)
  1070. goto fail;
  1071. error = gfs2_meta_inode_buffer(ip, &dibh);
  1072. if (error)
  1073. goto fail_end_trans;
  1074. ip->i_inode.i_atime = tv;
  1075. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1076. di = (struct gfs2_dinode *)dibh->b_data;
  1077. di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  1078. di->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  1079. brelse(dibh);
  1080. gfs2_trans_end(sdp);
  1081. }
  1082. /* If someone else has asked for the glock,
  1083. unlock and let them have it. Then reacquire
  1084. in the original state. */
  1085. if (gfs2_glock_is_blocking(gl)) {
  1086. gfs2_glock_dq(gh);
  1087. gfs2_holder_reinit(state, flags, gh);
  1088. return gfs2_glock_nq(gh);
  1089. }
  1090. }
  1091. return 0;
  1092. fail_end_trans:
  1093. gfs2_trans_end(sdp);
  1094. fail:
  1095. gfs2_glock_dq(gh);
  1096. return error;
  1097. }
  1098. static int
  1099. __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1100. {
  1101. struct buffer_head *dibh;
  1102. int error;
  1103. error = gfs2_meta_inode_buffer(ip, &dibh);
  1104. if (!error) {
  1105. error = inode_setattr(&ip->i_inode, attr);
  1106. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  1107. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1108. gfs2_dinode_out(ip, dibh->b_data);
  1109. brelse(dibh);
  1110. }
  1111. return error;
  1112. }
  1113. /**
  1114. * gfs2_setattr_simple -
  1115. * @ip:
  1116. * @attr:
  1117. *
  1118. * Called with a reference on the vnode.
  1119. *
  1120. * Returns: errno
  1121. */
  1122. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1123. {
  1124. int error;
  1125. if (current->journal_info)
  1126. return __gfs2_setattr_simple(ip, attr);
  1127. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  1128. if (error)
  1129. return error;
  1130. error = __gfs2_setattr_simple(ip, attr);
  1131. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  1132. return error;
  1133. }
  1134. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  1135. {
  1136. const struct gfs2_dinode_host *di = &ip->i_di;
  1137. struct gfs2_dinode *str = buf;
  1138. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  1139. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  1140. str->di_header.__pad0 = 0;
  1141. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  1142. str->di_header.__pad1 = 0;
  1143. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  1144. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  1145. str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  1146. str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  1147. str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  1148. str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
  1149. str->di_size = cpu_to_be64(di->di_size);
  1150. str->di_blocks = cpu_to_be64(di->di_blocks);
  1151. str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  1152. str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
  1153. str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
  1154. str->di_goal_meta = cpu_to_be64(di->di_goal_meta);
  1155. str->di_goal_data = cpu_to_be64(di->di_goal_data);
  1156. str->di_generation = cpu_to_be64(di->di_generation);
  1157. str->di_flags = cpu_to_be32(di->di_flags);
  1158. str->di_height = cpu_to_be16(di->di_height);
  1159. str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
  1160. !(ip->i_di.di_flags & GFS2_DIF_EXHASH) ?
  1161. GFS2_FORMAT_DE : 0);
  1162. str->di_depth = cpu_to_be16(di->di_depth);
  1163. str->di_entries = cpu_to_be32(di->di_entries);
  1164. str->di_eattr = cpu_to_be64(di->di_eattr);
  1165. str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  1166. str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
  1167. str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
  1168. }
  1169. void gfs2_dinode_print(const struct gfs2_inode *ip)
  1170. {
  1171. const struct gfs2_dinode_host *di = &ip->i_di;
  1172. printk(KERN_INFO " no_formal_ino = %llu\n",
  1173. (unsigned long long)ip->i_no_formal_ino);
  1174. printk(KERN_INFO " no_addr = %llu\n",
  1175. (unsigned long long)ip->i_no_addr);
  1176. printk(KERN_INFO " di_size = %llu\n", (unsigned long long)di->di_size);
  1177. printk(KERN_INFO " di_blocks = %llu\n",
  1178. (unsigned long long)di->di_blocks);
  1179. printk(KERN_INFO " di_goal_meta = %llu\n",
  1180. (unsigned long long)di->di_goal_meta);
  1181. printk(KERN_INFO " di_goal_data = %llu\n",
  1182. (unsigned long long)di->di_goal_data);
  1183. printk(KERN_INFO " di_flags = 0x%.8X\n", di->di_flags);
  1184. printk(KERN_INFO " di_height = %u\n", di->di_height);
  1185. printk(KERN_INFO " di_depth = %u\n", di->di_depth);
  1186. printk(KERN_INFO " di_entries = %u\n", di->di_entries);
  1187. printk(KERN_INFO " di_eattr = %llu\n",
  1188. (unsigned long long)di->di_eattr);
  1189. }