inode.c 26 KB

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