inode.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. static int iget_test(struct inode *inode, void *opaque)
  40. {
  41. struct gfs2_inode *ip = GFS2_I(inode);
  42. u64 *no_addr = opaque;
  43. if (ip->i_no_addr == *no_addr)
  44. return 1;
  45. return 0;
  46. }
  47. static int iget_set(struct inode *inode, void *opaque)
  48. {
  49. struct gfs2_inode *ip = GFS2_I(inode);
  50. u64 *no_addr = opaque;
  51. inode->i_ino = (unsigned long)*no_addr;
  52. ip->i_no_addr = *no_addr;
  53. return 0;
  54. }
  55. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
  56. {
  57. unsigned long hash = (unsigned long)no_addr;
  58. return ilookup5(sb, hash, iget_test, &no_addr);
  59. }
  60. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  61. {
  62. unsigned long hash = (unsigned long)no_addr;
  63. return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
  64. }
  65. struct gfs2_skip_data {
  66. u64 no_addr;
  67. int skipped;
  68. };
  69. static int iget_skip_test(struct inode *inode, void *opaque)
  70. {
  71. struct gfs2_inode *ip = GFS2_I(inode);
  72. struct gfs2_skip_data *data = opaque;
  73. if (ip->i_no_addr == data->no_addr) {
  74. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)){
  75. data->skipped = 1;
  76. return 0;
  77. }
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static int iget_skip_set(struct inode *inode, void *opaque)
  83. {
  84. struct gfs2_inode *ip = GFS2_I(inode);
  85. struct gfs2_skip_data *data = opaque;
  86. if (data->skipped)
  87. return 1;
  88. inode->i_ino = (unsigned long)(data->no_addr);
  89. ip->i_no_addr = data->no_addr;
  90. return 0;
  91. }
  92. static struct inode *gfs2_iget_skip(struct super_block *sb,
  93. u64 no_addr)
  94. {
  95. struct gfs2_skip_data data;
  96. unsigned long hash = (unsigned long)no_addr;
  97. data.no_addr = no_addr;
  98. data.skipped = 0;
  99. return iget5_locked(sb, hash, iget_skip_test, iget_skip_set, &data);
  100. }
  101. /**
  102. * GFS2 lookup code fills in vfs inode contents based on info obtained
  103. * from directory entry inside gfs2_inode_lookup(). This has caused issues
  104. * with NFS code path since its get_dentry routine doesn't have the relevant
  105. * directory entry when gfs2_inode_lookup() is invoked. Part of the code
  106. * segment inside gfs2_inode_lookup code needs to get moved around.
  107. *
  108. * Clears I_NEW as well.
  109. **/
  110. void gfs2_set_iop(struct inode *inode)
  111. {
  112. struct gfs2_sbd *sdp = GFS2_SB(inode);
  113. umode_t mode = inode->i_mode;
  114. if (S_ISREG(mode)) {
  115. inode->i_op = &gfs2_file_iops;
  116. if (gfs2_localflocks(sdp))
  117. inode->i_fop = &gfs2_file_fops_nolock;
  118. else
  119. inode->i_fop = &gfs2_file_fops;
  120. } else if (S_ISDIR(mode)) {
  121. inode->i_op = &gfs2_dir_iops;
  122. if (gfs2_localflocks(sdp))
  123. inode->i_fop = &gfs2_dir_fops_nolock;
  124. else
  125. inode->i_fop = &gfs2_dir_fops;
  126. } else if (S_ISLNK(mode)) {
  127. inode->i_op = &gfs2_symlink_iops;
  128. } else {
  129. inode->i_op = &gfs2_file_iops;
  130. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  131. }
  132. unlock_new_inode(inode);
  133. }
  134. /**
  135. * gfs2_inode_lookup - Lookup an inode
  136. * @sb: The super block
  137. * @no_addr: The inode number
  138. * @type: The type of the inode
  139. *
  140. * Returns: A VFS inode, or an error
  141. */
  142. struct inode *gfs2_inode_lookup(struct super_block *sb,
  143. unsigned int type,
  144. u64 no_addr,
  145. u64 no_formal_ino)
  146. {
  147. struct inode *inode;
  148. struct gfs2_inode *ip;
  149. struct gfs2_glock *io_gl = NULL;
  150. int error;
  151. inode = gfs2_iget(sb, no_addr);
  152. ip = GFS2_I(inode);
  153. if (!inode)
  154. return ERR_PTR(-ENOBUFS);
  155. if (inode->i_state & I_NEW) {
  156. struct gfs2_sbd *sdp = GFS2_SB(inode);
  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. io_gl = NULL;
  172. if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
  173. goto gfs2_nfsbypass;
  174. inode->i_mode = DT2IF(type);
  175. /*
  176. * We must read the inode in order to work out its type in
  177. * this case. Note that this doesn't happen often as we normally
  178. * know the type beforehand. This code path only occurs during
  179. * unlinked inode recovery (where it is safe to do this glock,
  180. * which is not true in the general case).
  181. */
  182. if (type == DT_UNKNOWN) {
  183. struct gfs2_holder gh;
  184. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  185. if (unlikely(error))
  186. goto fail_glock;
  187. /* Inode is now uptodate */
  188. gfs2_glock_dq_uninit(&gh);
  189. }
  190. gfs2_set_iop(inode);
  191. }
  192. gfs2_nfsbypass:
  193. return inode;
  194. fail_glock:
  195. gfs2_glock_dq(&ip->i_iopen_gh);
  196. fail_iopen:
  197. if (io_gl)
  198. gfs2_glock_put(io_gl);
  199. fail_put:
  200. if (inode->i_state & I_NEW)
  201. ip->i_gl->gl_object = NULL;
  202. gfs2_glock_put(ip->i_gl);
  203. fail:
  204. if (inode->i_state & I_NEW)
  205. iget_failed(inode);
  206. else
  207. iput(inode);
  208. return ERR_PTR(error);
  209. }
  210. /**
  211. * gfs2_process_unlinked_inode - Lookup an unlinked inode for reclamation
  212. * and try to reclaim it by doing iput.
  213. *
  214. * This function assumes no rgrp locks are currently held.
  215. *
  216. * @sb: The super block
  217. * no_addr: The inode number
  218. *
  219. */
  220. void gfs2_process_unlinked_inode(struct super_block *sb, u64 no_addr)
  221. {
  222. struct gfs2_sbd *sdp;
  223. struct gfs2_inode *ip;
  224. struct gfs2_glock *io_gl = NULL;
  225. int error;
  226. struct gfs2_holder gh;
  227. struct inode *inode;
  228. inode = gfs2_iget_skip(sb, no_addr);
  229. if (!inode)
  230. return;
  231. /* If it's not a new inode, someone's using it, so leave it alone. */
  232. if (!(inode->i_state & I_NEW)) {
  233. iput(inode);
  234. return;
  235. }
  236. ip = GFS2_I(inode);
  237. sdp = GFS2_SB(inode);
  238. ip->i_no_formal_ino = -1;
  239. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  240. if (unlikely(error))
  241. goto fail;
  242. ip->i_gl->gl_object = ip;
  243. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  244. if (unlikely(error))
  245. goto fail_put;
  246. set_bit(GIF_INVALID, &ip->i_flags);
  247. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, LM_FLAG_TRY | GL_EXACT,
  248. &ip->i_iopen_gh);
  249. if (unlikely(error))
  250. goto fail_iopen;
  251. ip->i_iopen_gh.gh_gl->gl_object = ip;
  252. gfs2_glock_put(io_gl);
  253. io_gl = NULL;
  254. inode->i_mode = DT2IF(DT_UNKNOWN);
  255. /*
  256. * We must read the inode in order to work out its type in
  257. * this case. Note that this doesn't happen often as we normally
  258. * know the type beforehand. This code path only occurs during
  259. * unlinked inode recovery (where it is safe to do this glock,
  260. * which is not true in the general case).
  261. */
  262. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY,
  263. &gh);
  264. if (unlikely(error))
  265. goto fail_glock;
  266. /* Inode is now uptodate */
  267. gfs2_glock_dq_uninit(&gh);
  268. gfs2_set_iop(inode);
  269. /* The iput will cause it to be deleted. */
  270. iput(inode);
  271. return;
  272. fail_glock:
  273. gfs2_glock_dq(&ip->i_iopen_gh);
  274. fail_iopen:
  275. if (io_gl)
  276. gfs2_glock_put(io_gl);
  277. fail_put:
  278. ip->i_gl->gl_object = NULL;
  279. gfs2_glock_put(ip->i_gl);
  280. fail:
  281. iget_failed(inode);
  282. return;
  283. }
  284. static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
  285. {
  286. const struct gfs2_dinode *str = buf;
  287. struct timespec atime;
  288. u16 height, depth;
  289. if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
  290. goto corrupt;
  291. ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
  292. ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
  293. ip->i_inode.i_rdev = 0;
  294. switch (ip->i_inode.i_mode & S_IFMT) {
  295. case S_IFBLK:
  296. case S_IFCHR:
  297. ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
  298. be32_to_cpu(str->di_minor));
  299. break;
  300. };
  301. ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
  302. ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
  303. /*
  304. * We will need to review setting the nlink count here in the
  305. * light of the forthcoming ro bind mount work. This is a reminder
  306. * to do that.
  307. */
  308. ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
  309. ip->i_disksize = be64_to_cpu(str->di_size);
  310. i_size_write(&ip->i_inode, ip->i_disksize);
  311. gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
  312. atime.tv_sec = be64_to_cpu(str->di_atime);
  313. atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
  314. if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
  315. ip->i_inode.i_atime = atime;
  316. ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
  317. ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
  318. ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
  319. ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
  320. ip->i_goal = be64_to_cpu(str->di_goal_meta);
  321. ip->i_generation = be64_to_cpu(str->di_generation);
  322. ip->i_diskflags = be32_to_cpu(str->di_flags);
  323. gfs2_set_inode_flags(&ip->i_inode);
  324. height = be16_to_cpu(str->di_height);
  325. if (unlikely(height > GFS2_MAX_META_HEIGHT))
  326. goto corrupt;
  327. ip->i_height = (u8)height;
  328. depth = be16_to_cpu(str->di_depth);
  329. if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
  330. goto corrupt;
  331. ip->i_depth = (u8)depth;
  332. ip->i_entries = be32_to_cpu(str->di_entries);
  333. ip->i_eattr = be64_to_cpu(str->di_eattr);
  334. if (S_ISREG(ip->i_inode.i_mode))
  335. gfs2_set_aops(&ip->i_inode);
  336. return 0;
  337. corrupt:
  338. if (gfs2_consist_inode(ip))
  339. gfs2_dinode_print(ip);
  340. return -EIO;
  341. }
  342. /**
  343. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  344. * @ip: The GFS2 inode
  345. *
  346. * Returns: errno
  347. */
  348. int gfs2_inode_refresh(struct gfs2_inode *ip)
  349. {
  350. struct buffer_head *dibh;
  351. int error;
  352. error = gfs2_meta_inode_buffer(ip, &dibh);
  353. if (error)
  354. return error;
  355. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
  356. brelse(dibh);
  357. return -EIO;
  358. }
  359. error = gfs2_dinode_in(ip, dibh->b_data);
  360. brelse(dibh);
  361. clear_bit(GIF_INVALID, &ip->i_flags);
  362. return error;
  363. }
  364. int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  365. {
  366. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  367. struct gfs2_alloc *al;
  368. struct gfs2_rgrpd *rgd;
  369. int error;
  370. if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
  371. if (gfs2_consist_inode(ip))
  372. gfs2_dinode_print(ip);
  373. return -EIO;
  374. }
  375. al = gfs2_alloc_get(ip);
  376. if (!al)
  377. return -ENOMEM;
  378. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  379. if (error)
  380. goto out;
  381. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  382. if (error)
  383. goto out_qs;
  384. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  385. if (!rgd) {
  386. gfs2_consist_inode(ip);
  387. error = -EIO;
  388. goto out_rindex_relse;
  389. }
  390. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  391. &al->al_rgd_gh);
  392. if (error)
  393. goto out_rindex_relse;
  394. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
  395. if (error)
  396. goto out_rg_gunlock;
  397. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  398. set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
  399. gfs2_free_di(rgd, ip);
  400. gfs2_trans_end(sdp);
  401. out_rg_gunlock:
  402. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  403. out_rindex_relse:
  404. gfs2_glock_dq_uninit(&al->al_ri_gh);
  405. out_qs:
  406. gfs2_quota_unhold(ip);
  407. out:
  408. gfs2_alloc_put(ip);
  409. return error;
  410. }
  411. /**
  412. * gfs2_change_nlink - Change nlink count on inode
  413. * @ip: The GFS2 inode
  414. * @diff: The change in the nlink count required
  415. *
  416. * Returns: errno
  417. */
  418. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  419. {
  420. struct buffer_head *dibh;
  421. u32 nlink;
  422. int error;
  423. BUG_ON(diff != 1 && diff != -1);
  424. nlink = ip->i_inode.i_nlink + diff;
  425. /* If we are reducing the nlink count, but the new value ends up being
  426. bigger than the old one, we must have underflowed. */
  427. if (diff < 0 && nlink > ip->i_inode.i_nlink) {
  428. if (gfs2_consist_inode(ip))
  429. gfs2_dinode_print(ip);
  430. return -EIO;
  431. }
  432. error = gfs2_meta_inode_buffer(ip, &dibh);
  433. if (error)
  434. return error;
  435. if (diff > 0)
  436. inc_nlink(&ip->i_inode);
  437. else
  438. drop_nlink(&ip->i_inode);
  439. ip->i_inode.i_ctime = CURRENT_TIME;
  440. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  441. gfs2_dinode_out(ip, dibh->b_data);
  442. brelse(dibh);
  443. mark_inode_dirty(&ip->i_inode);
  444. if (ip->i_inode.i_nlink == 0)
  445. gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
  446. return error;
  447. }
  448. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  449. {
  450. struct qstr qstr;
  451. struct inode *inode;
  452. gfs2_str2qstr(&qstr, name);
  453. inode = gfs2_lookupi(dip, &qstr, 1);
  454. /* gfs2_lookupi has inconsistent callers: vfs
  455. * related routines expect NULL for no entry found,
  456. * gfs2_lookup_simple callers expect ENOENT
  457. * and do not check for NULL.
  458. */
  459. if (inode == NULL)
  460. return ERR_PTR(-ENOENT);
  461. else
  462. return inode;
  463. }
  464. /**
  465. * gfs2_lookupi - Look up a filename in a directory and return its inode
  466. * @d_gh: An initialized holder for the directory glock
  467. * @name: The name of the inode to look for
  468. * @is_root: If 1, ignore the caller's permissions
  469. * @i_gh: An uninitialized holder for the new inode glock
  470. *
  471. * This can be called via the VFS filldir function when NFS is doing
  472. * a readdirplus and the inode which its intending to stat isn't
  473. * already in cache. In this case we must not take the directory glock
  474. * again, since the readdir call will have already taken that lock.
  475. *
  476. * Returns: errno
  477. */
  478. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  479. int is_root)
  480. {
  481. struct super_block *sb = dir->i_sb;
  482. struct gfs2_inode *dip = GFS2_I(dir);
  483. struct gfs2_holder d_gh;
  484. int error = 0;
  485. struct inode *inode = NULL;
  486. int unlock = 0;
  487. if (!name->len || name->len > GFS2_FNAMESIZE)
  488. return ERR_PTR(-ENAMETOOLONG);
  489. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  490. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  491. dir == sb->s_root->d_inode)) {
  492. igrab(dir);
  493. return dir;
  494. }
  495. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  496. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  497. if (error)
  498. return ERR_PTR(error);
  499. unlock = 1;
  500. }
  501. if (!is_root) {
  502. error = gfs2_permission(dir, MAY_EXEC);
  503. if (error)
  504. goto out;
  505. }
  506. inode = gfs2_dir_search(dir, name);
  507. if (IS_ERR(inode))
  508. error = PTR_ERR(inode);
  509. out:
  510. if (unlock)
  511. gfs2_glock_dq_uninit(&d_gh);
  512. if (error == -ENOENT)
  513. return NULL;
  514. return inode ? inode : ERR_PTR(error);
  515. }
  516. /**
  517. * create_ok - OK to create a new on-disk inode here?
  518. * @dip: Directory in which dinode is to be created
  519. * @name: Name of new dinode
  520. * @mode:
  521. *
  522. * Returns: errno
  523. */
  524. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  525. unsigned int mode)
  526. {
  527. int error;
  528. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  529. if (error)
  530. return error;
  531. /* Don't create entries in an unlinked directory */
  532. if (!dip->i_inode.i_nlink)
  533. return -EPERM;
  534. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  535. switch (error) {
  536. case -ENOENT:
  537. error = 0;
  538. break;
  539. case 0:
  540. return -EEXIST;
  541. default:
  542. return error;
  543. }
  544. if (dip->i_entries == (u32)-1)
  545. return -EFBIG;
  546. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  547. return -EMLINK;
  548. return 0;
  549. }
  550. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  551. unsigned int *uid, unsigned int *gid)
  552. {
  553. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  554. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  555. if (S_ISDIR(*mode))
  556. *mode |= S_ISUID;
  557. else if (dip->i_inode.i_uid != current_fsuid())
  558. *mode &= ~07111;
  559. *uid = dip->i_inode.i_uid;
  560. } else
  561. *uid = current_fsuid();
  562. if (dip->i_inode.i_mode & S_ISGID) {
  563. if (S_ISDIR(*mode))
  564. *mode |= S_ISGID;
  565. *gid = dip->i_inode.i_gid;
  566. } else
  567. *gid = current_fsgid();
  568. }
  569. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  570. {
  571. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  572. int error;
  573. if (gfs2_alloc_get(dip) == NULL)
  574. return -ENOMEM;
  575. dip->i_alloc->al_requested = RES_DINODE;
  576. error = gfs2_inplace_reserve(dip);
  577. if (error)
  578. goto out;
  579. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  580. if (error)
  581. goto out_ipreserv;
  582. error = gfs2_alloc_di(dip, no_addr, generation);
  583. gfs2_trans_end(sdp);
  584. out_ipreserv:
  585. gfs2_inplace_release(dip);
  586. out:
  587. gfs2_alloc_put(dip);
  588. return error;
  589. }
  590. /**
  591. * init_dinode - Fill in a new dinode structure
  592. * @dip: the directory this inode is being created in
  593. * @gl: The glock covering the new inode
  594. * @inum: the inode number
  595. * @mode: the file permissions
  596. * @uid:
  597. * @gid:
  598. *
  599. */
  600. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  601. const struct gfs2_inum_host *inum, unsigned int mode,
  602. unsigned int uid, unsigned int gid,
  603. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  604. {
  605. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  606. struct gfs2_dinode *di;
  607. struct buffer_head *dibh;
  608. struct timespec tv = CURRENT_TIME;
  609. dibh = gfs2_meta_new(gl, inum->no_addr);
  610. gfs2_trans_add_bh(gl, dibh, 1);
  611. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  612. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  613. di = (struct gfs2_dinode *)dibh->b_data;
  614. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  615. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  616. di->di_mode = cpu_to_be32(mode);
  617. di->di_uid = cpu_to_be32(uid);
  618. di->di_gid = cpu_to_be32(gid);
  619. di->di_nlink = 0;
  620. di->di_size = 0;
  621. di->di_blocks = cpu_to_be64(1);
  622. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  623. di->di_major = cpu_to_be32(MAJOR(dev));
  624. di->di_minor = cpu_to_be32(MINOR(dev));
  625. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  626. di->di_generation = cpu_to_be64(*generation);
  627. di->di_flags = 0;
  628. if (S_ISREG(mode)) {
  629. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  630. gfs2_tune_get(sdp, gt_new_files_jdata))
  631. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  632. } else if (S_ISDIR(mode)) {
  633. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  634. GFS2_DIF_INHERIT_JDATA);
  635. }
  636. di->__pad1 = 0;
  637. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  638. di->di_height = 0;
  639. di->__pad2 = 0;
  640. di->__pad3 = 0;
  641. di->di_depth = 0;
  642. di->di_entries = 0;
  643. memset(&di->__pad4, 0, sizeof(di->__pad4));
  644. di->di_eattr = 0;
  645. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  646. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  647. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  648. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  649. set_buffer_uptodate(dibh);
  650. *bhp = dibh;
  651. }
  652. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  653. unsigned int mode, const struct gfs2_inum_host *inum,
  654. const u64 *generation, dev_t dev, struct buffer_head **bhp)
  655. {
  656. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  657. unsigned int uid, gid;
  658. int error;
  659. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  660. if (!gfs2_alloc_get(dip))
  661. return -ENOMEM;
  662. error = gfs2_quota_lock(dip, uid, gid);
  663. if (error)
  664. goto out;
  665. error = gfs2_quota_check(dip, uid, gid);
  666. if (error)
  667. goto out_quota;
  668. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  669. if (error)
  670. goto out_quota;
  671. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
  672. gfs2_quota_change(dip, +1, uid, gid);
  673. gfs2_trans_end(sdp);
  674. out_quota:
  675. gfs2_quota_unlock(dip);
  676. out:
  677. gfs2_alloc_put(dip);
  678. return error;
  679. }
  680. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  681. struct gfs2_inode *ip)
  682. {
  683. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  684. struct gfs2_alloc *al;
  685. int alloc_required;
  686. struct buffer_head *dibh;
  687. int error;
  688. al = gfs2_alloc_get(dip);
  689. if (!al)
  690. return -ENOMEM;
  691. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  692. if (error)
  693. goto fail;
  694. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  695. if (alloc_required < 0)
  696. goto fail_quota_locks;
  697. if (alloc_required) {
  698. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  699. if (error)
  700. goto fail_quota_locks;
  701. al->al_requested = sdp->sd_max_dirres;
  702. error = gfs2_inplace_reserve(dip);
  703. if (error)
  704. goto fail_quota_locks;
  705. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  706. al->al_rgd->rd_length +
  707. 2 * RES_DINODE +
  708. RES_STATFS + RES_QUOTA, 0);
  709. if (error)
  710. goto fail_ipreserv;
  711. } else {
  712. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  713. if (error)
  714. goto fail_quota_locks;
  715. }
  716. error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
  717. if (error)
  718. goto fail_end_trans;
  719. error = gfs2_meta_inode_buffer(ip, &dibh);
  720. if (error)
  721. goto fail_end_trans;
  722. ip->i_inode.i_nlink = 1;
  723. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  724. gfs2_dinode_out(ip, dibh->b_data);
  725. brelse(dibh);
  726. return 0;
  727. fail_end_trans:
  728. gfs2_trans_end(sdp);
  729. fail_ipreserv:
  730. if (dip->i_alloc->al_rgd)
  731. gfs2_inplace_release(dip);
  732. fail_quota_locks:
  733. gfs2_quota_unlock(dip);
  734. fail:
  735. gfs2_alloc_put(dip);
  736. return error;
  737. }
  738. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
  739. {
  740. int err;
  741. size_t len;
  742. void *value;
  743. char *name;
  744. err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
  745. &name, &value, &len);
  746. if (err) {
  747. if (err == -EOPNOTSUPP)
  748. return 0;
  749. return err;
  750. }
  751. err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
  752. GFS2_EATYPE_SECURITY);
  753. kfree(value);
  754. kfree(name);
  755. return err;
  756. }
  757. /**
  758. * gfs2_createi - Create a new inode
  759. * @ghs: An array of two holders
  760. * @name: The name of the new file
  761. * @mode: the permissions on the new inode
  762. *
  763. * @ghs[0] is an initialized holder for the directory
  764. * @ghs[1] is the holder for the inode lock
  765. *
  766. * If the return value is not NULL, the glocks on both the directory and the new
  767. * file are held. A transaction has been started and an inplace reservation
  768. * is held, as well.
  769. *
  770. * Returns: An inode
  771. */
  772. struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
  773. unsigned int mode, dev_t dev)
  774. {
  775. struct inode *inode = NULL;
  776. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  777. struct inode *dir = &dip->i_inode;
  778. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  779. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  780. int error;
  781. u64 generation;
  782. struct buffer_head *bh = NULL;
  783. if (!name->len || name->len > GFS2_FNAMESIZE)
  784. return ERR_PTR(-ENAMETOOLONG);
  785. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  786. error = gfs2_glock_nq(ghs);
  787. if (error)
  788. goto fail;
  789. error = create_ok(dip, name, mode);
  790. if (error)
  791. goto fail_gunlock;
  792. error = alloc_dinode(dip, &inum.no_addr, &generation);
  793. if (error)
  794. goto fail_gunlock;
  795. inum.no_formal_ino = generation;
  796. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  797. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  798. if (error)
  799. goto fail_gunlock;
  800. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
  801. if (error)
  802. goto fail_gunlock2;
  803. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  804. inum.no_formal_ino);
  805. if (IS_ERR(inode))
  806. goto fail_gunlock2;
  807. error = gfs2_inode_refresh(GFS2_I(inode));
  808. if (error)
  809. goto fail_gunlock2;
  810. error = gfs2_acl_create(dip, inode);
  811. if (error)
  812. goto fail_gunlock2;
  813. error = gfs2_security_init(dip, GFS2_I(inode));
  814. if (error)
  815. goto fail_gunlock2;
  816. error = link_dinode(dip, name, GFS2_I(inode));
  817. if (error)
  818. goto fail_gunlock2;
  819. if (bh)
  820. brelse(bh);
  821. return inode;
  822. fail_gunlock2:
  823. gfs2_glock_dq_uninit(ghs + 1);
  824. if (inode && !IS_ERR(inode))
  825. iput(inode);
  826. fail_gunlock:
  827. gfs2_glock_dq(ghs);
  828. fail:
  829. if (bh)
  830. brelse(bh);
  831. return ERR_PTR(error);
  832. }
  833. static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  834. {
  835. struct buffer_head *dibh;
  836. int error;
  837. error = gfs2_meta_inode_buffer(ip, &dibh);
  838. if (!error) {
  839. error = inode_setattr(&ip->i_inode, attr);
  840. gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
  841. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  842. gfs2_dinode_out(ip, dibh->b_data);
  843. brelse(dibh);
  844. }
  845. return error;
  846. }
  847. /**
  848. * gfs2_setattr_simple -
  849. * @ip:
  850. * @attr:
  851. *
  852. * Called with a reference on the vnode.
  853. *
  854. * Returns: errno
  855. */
  856. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  857. {
  858. int error;
  859. if (current->journal_info)
  860. return __gfs2_setattr_simple(ip, attr);
  861. error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
  862. if (error)
  863. return error;
  864. error = __gfs2_setattr_simple(ip, attr);
  865. gfs2_trans_end(GFS2_SB(&ip->i_inode));
  866. return error;
  867. }
  868. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  869. {
  870. struct gfs2_dinode *str = buf;
  871. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  872. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  873. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  874. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  875. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  876. str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  877. str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  878. str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  879. str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
  880. str->di_size = cpu_to_be64(ip->i_disksize);
  881. str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  882. str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  883. str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
  884. str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
  885. str->di_goal_meta = cpu_to_be64(ip->i_goal);
  886. str->di_goal_data = cpu_to_be64(ip->i_goal);
  887. str->di_generation = cpu_to_be64(ip->i_generation);
  888. str->di_flags = cpu_to_be32(ip->i_diskflags);
  889. str->di_height = cpu_to_be16(ip->i_height);
  890. str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
  891. !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
  892. GFS2_FORMAT_DE : 0);
  893. str->di_depth = cpu_to_be16(ip->i_depth);
  894. str->di_entries = cpu_to_be32(ip->i_entries);
  895. str->di_eattr = cpu_to_be64(ip->i_eattr);
  896. str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  897. str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
  898. str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
  899. }
  900. void gfs2_dinode_print(const struct gfs2_inode *ip)
  901. {
  902. printk(KERN_INFO " no_formal_ino = %llu\n",
  903. (unsigned long long)ip->i_no_formal_ino);
  904. printk(KERN_INFO " no_addr = %llu\n",
  905. (unsigned long long)ip->i_no_addr);
  906. printk(KERN_INFO " i_disksize = %llu\n",
  907. (unsigned long long)ip->i_disksize);
  908. printk(KERN_INFO " blocks = %llu\n",
  909. (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
  910. printk(KERN_INFO " i_goal = %llu\n",
  911. (unsigned long long)ip->i_goal);
  912. printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags);
  913. printk(KERN_INFO " i_height = %u\n", ip->i_height);
  914. printk(KERN_INFO " i_depth = %u\n", ip->i_depth);
  915. printk(KERN_INFO " i_entries = %u\n", ip->i_entries);
  916. printk(KERN_INFO " i_eattr = %llu\n",
  917. (unsigned long long)ip->i_eattr);
  918. }