inode.c 34 KB

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