inode.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/security.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOBUFS);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  166. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  167. fail_iopen:
  168. if (io_gl)
  169. gfs2_glock_put(io_gl);
  170. fail_put:
  171. ip->i_gl->gl_object = NULL;
  172. gfs2_glock_put(ip->i_gl);
  173. fail:
  174. iget_failed(inode);
  175. return ERR_PTR(error);
  176. }
  177. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  178. u64 *no_formal_ino, unsigned int blktype)
  179. {
  180. struct super_block *sb = sdp->sd_vfs;
  181. struct gfs2_holder i_gh;
  182. struct inode *inode = NULL;
  183. int error;
  184. /* Must not read in block until block type is verified */
  185. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  186. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  187. if (error)
  188. return ERR_PTR(error);
  189. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  190. if (error)
  191. goto fail;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  193. if (IS_ERR(inode))
  194. goto fail;
  195. /* Two extra checks for NFS only */
  196. if (no_formal_ino) {
  197. error = -ESTALE;
  198. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  199. goto fail_iput;
  200. error = -EIO;
  201. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  202. goto fail_iput;
  203. error = 0;
  204. }
  205. fail:
  206. gfs2_glock_dq_uninit(&i_gh);
  207. return error ? ERR_PTR(error) : inode;
  208. fail_iput:
  209. iput(inode);
  210. goto fail;
  211. }
  212. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  213. {
  214. struct qstr qstr;
  215. struct inode *inode;
  216. gfs2_str2qstr(&qstr, name);
  217. inode = gfs2_lookupi(dip, &qstr, 1);
  218. /* gfs2_lookupi has inconsistent callers: vfs
  219. * related routines expect NULL for no entry found,
  220. * gfs2_lookup_simple callers expect ENOENT
  221. * and do not check for NULL.
  222. */
  223. if (inode == NULL)
  224. return ERR_PTR(-ENOENT);
  225. else
  226. return inode;
  227. }
  228. /**
  229. * gfs2_lookupi - Look up a filename in a directory and return its inode
  230. * @d_gh: An initialized holder for the directory glock
  231. * @name: The name of the inode to look for
  232. * @is_root: If 1, ignore the caller's permissions
  233. * @i_gh: An uninitialized holder for the new inode glock
  234. *
  235. * This can be called via the VFS filldir function when NFS is doing
  236. * a readdirplus and the inode which its intending to stat isn't
  237. * already in cache. In this case we must not take the directory glock
  238. * again, since the readdir call will have already taken that lock.
  239. *
  240. * Returns: errno
  241. */
  242. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  243. int is_root)
  244. {
  245. struct super_block *sb = dir->i_sb;
  246. struct gfs2_inode *dip = GFS2_I(dir);
  247. struct gfs2_holder d_gh;
  248. int error = 0;
  249. struct inode *inode = NULL;
  250. int unlock = 0;
  251. if (!name->len || name->len > GFS2_FNAMESIZE)
  252. return ERR_PTR(-ENAMETOOLONG);
  253. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  254. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  255. dir == sb->s_root->d_inode)) {
  256. igrab(dir);
  257. return dir;
  258. }
  259. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  260. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  261. if (error)
  262. return ERR_PTR(error);
  263. unlock = 1;
  264. }
  265. if (!is_root) {
  266. error = gfs2_permission(dir, MAY_EXEC);
  267. if (error)
  268. goto out;
  269. }
  270. inode = gfs2_dir_search(dir, name);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (unlock)
  275. gfs2_glock_dq_uninit(&d_gh);
  276. if (error == -ENOENT)
  277. return NULL;
  278. return inode ? inode : ERR_PTR(error);
  279. }
  280. /**
  281. * create_ok - OK to create a new on-disk inode here?
  282. * @dip: Directory in which dinode is to be created
  283. * @name: Name of new dinode
  284. * @mode:
  285. *
  286. * Returns: errno
  287. */
  288. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  289. umode_t mode)
  290. {
  291. int error;
  292. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  293. if (error)
  294. return error;
  295. /* Don't create entries in an unlinked directory */
  296. if (!dip->i_inode.i_nlink)
  297. return -ENOENT;
  298. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  299. switch (error) {
  300. case -ENOENT:
  301. error = 0;
  302. break;
  303. case 0:
  304. return -EEXIST;
  305. default:
  306. return error;
  307. }
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(struct gfs2_inode *dip, umode_t *mode,
  315. unsigned int *uid, unsigned int *gid)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  319. if (S_ISDIR(*mode))
  320. *mode |= S_ISUID;
  321. else if (dip->i_inode.i_uid != current_fsuid())
  322. *mode &= ~07111;
  323. *uid = dip->i_inode.i_uid;
  324. } else
  325. *uid = current_fsuid();
  326. if (dip->i_inode.i_mode & S_ISGID) {
  327. if (S_ISDIR(*mode))
  328. *mode |= S_ISGID;
  329. *gid = dip->i_inode.i_gid;
  330. } else
  331. *gid = current_fsgid();
  332. }
  333. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  334. {
  335. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  336. int error;
  337. int dblocks = 1;
  338. error = gfs2_inplace_reserve(dip, RES_DINODE);
  339. if (error)
  340. goto out;
  341. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  342. if (error)
  343. goto out_ipreserv;
  344. error = gfs2_alloc_blocks(dip, no_addr, &dblocks, 1, generation);
  345. gfs2_trans_end(sdp);
  346. out_ipreserv:
  347. gfs2_inplace_release(dip);
  348. out:
  349. return error;
  350. }
  351. static void gfs2_init_dir(struct buffer_head *dibh,
  352. const struct gfs2_inode *parent)
  353. {
  354. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  355. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  356. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  357. dent->de_inum = di->di_num; /* already GFS2 endian */
  358. dent->de_type = cpu_to_be16(DT_DIR);
  359. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  360. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  361. gfs2_inum_out(parent, dent);
  362. dent->de_type = cpu_to_be16(DT_DIR);
  363. }
  364. /**
  365. * init_dinode - Fill in a new dinode structure
  366. * @dip: The directory this inode is being created in
  367. * @gl: The glock covering the new inode
  368. * @inum: The inode number
  369. * @mode: The file permissions
  370. * @uid: The uid of the new inode
  371. * @gid: The gid of the new inode
  372. * @generation: The generation number of the new inode
  373. * @dev: The device number (if a device node)
  374. * @symname: The symlink destination (if a symlink)
  375. * @size: The inode size (ignored for directories)
  376. * @bhp: The buffer head (returned to caller)
  377. *
  378. */
  379. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  380. const struct gfs2_inum_host *inum, umode_t mode,
  381. unsigned int uid, unsigned int gid,
  382. const u64 *generation, dev_t dev, const char *symname,
  383. unsigned size, struct buffer_head **bhp)
  384. {
  385. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  386. struct gfs2_dinode *di;
  387. struct buffer_head *dibh;
  388. struct timespec tv = CURRENT_TIME;
  389. dibh = gfs2_meta_new(gl, inum->no_addr);
  390. gfs2_trans_add_bh(gl, dibh, 1);
  391. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  392. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  393. di = (struct gfs2_dinode *)dibh->b_data;
  394. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  395. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  396. di->di_mode = cpu_to_be32(mode);
  397. di->di_uid = cpu_to_be32(uid);
  398. di->di_gid = cpu_to_be32(gid);
  399. di->di_nlink = 0;
  400. di->di_size = cpu_to_be64(size);
  401. di->di_blocks = cpu_to_be64(1);
  402. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  403. di->di_major = cpu_to_be32(MAJOR(dev));
  404. di->di_minor = cpu_to_be32(MINOR(dev));
  405. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  406. di->di_generation = cpu_to_be64(*generation);
  407. di->di_flags = 0;
  408. di->__pad1 = 0;
  409. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  410. di->di_height = 0;
  411. di->__pad2 = 0;
  412. di->__pad3 = 0;
  413. di->di_depth = 0;
  414. di->di_entries = 0;
  415. memset(&di->__pad4, 0, sizeof(di->__pad4));
  416. di->di_eattr = 0;
  417. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  418. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  419. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  420. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  421. switch(mode & S_IFMT) {
  422. case S_IFREG:
  423. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  424. gfs2_tune_get(sdp, gt_new_files_jdata))
  425. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  426. break;
  427. case S_IFDIR:
  428. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  429. GFS2_DIF_INHERIT_JDATA);
  430. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  431. di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  432. di->di_entries = cpu_to_be32(2);
  433. gfs2_init_dir(dibh, dip);
  434. break;
  435. case S_IFLNK:
  436. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, size);
  437. break;
  438. }
  439. set_buffer_uptodate(dibh);
  440. *bhp = dibh;
  441. }
  442. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  443. umode_t mode, const struct gfs2_inum_host *inum,
  444. const u64 *generation, dev_t dev, const char *symname,
  445. unsigned int size, struct buffer_head **bhp)
  446. {
  447. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  448. unsigned int uid, gid;
  449. int error;
  450. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  451. error = gfs2_rindex_update(sdp);
  452. if (error)
  453. return error;
  454. error = gfs2_quota_lock(dip, uid, gid);
  455. if (error)
  456. return error;
  457. error = gfs2_quota_check(dip, uid, gid);
  458. if (error)
  459. goto out_quota;
  460. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  461. if (error)
  462. goto out_quota;
  463. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, symname, size, bhp);
  464. gfs2_quota_change(dip, +1, uid, gid);
  465. gfs2_trans_end(sdp);
  466. out_quota:
  467. gfs2_quota_unlock(dip);
  468. return error;
  469. }
  470. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  471. struct gfs2_inode *ip)
  472. {
  473. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  474. int alloc_required;
  475. struct buffer_head *dibh;
  476. int error;
  477. error = gfs2_rindex_update(sdp);
  478. if (error)
  479. return error;
  480. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  481. if (error)
  482. goto fail;
  483. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  484. if (alloc_required < 0)
  485. goto fail_quota_locks;
  486. if (alloc_required) {
  487. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  488. if (error)
  489. goto fail_quota_locks;
  490. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres);
  491. if (error)
  492. goto fail_quota_locks;
  493. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  494. dip->i_rgd->rd_length +
  495. 2 * RES_DINODE +
  496. RES_STATFS + RES_QUOTA, 0);
  497. if (error)
  498. goto fail_ipreserv;
  499. } else {
  500. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  501. if (error)
  502. goto fail_quota_locks;
  503. }
  504. error = gfs2_dir_add(&dip->i_inode, name, ip);
  505. if (error)
  506. goto fail_end_trans;
  507. error = gfs2_meta_inode_buffer(ip, &dibh);
  508. if (error)
  509. goto fail_end_trans;
  510. set_nlink(&ip->i_inode, S_ISDIR(ip->i_inode.i_mode) ? 2 : 1);
  511. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  512. gfs2_dinode_out(ip, dibh->b_data);
  513. brelse(dibh);
  514. return 0;
  515. fail_end_trans:
  516. gfs2_trans_end(sdp);
  517. fail_ipreserv:
  518. if (alloc_required)
  519. gfs2_inplace_release(dip);
  520. fail_quota_locks:
  521. gfs2_quota_unlock(dip);
  522. fail:
  523. return error;
  524. }
  525. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  526. void *fs_info)
  527. {
  528. const struct xattr *xattr;
  529. int err = 0;
  530. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  531. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  532. xattr->value_len, 0,
  533. GFS2_EATYPE_SECURITY);
  534. if (err < 0)
  535. break;
  536. }
  537. return err;
  538. }
  539. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  540. const struct qstr *qstr)
  541. {
  542. return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  543. &gfs2_initxattrs, NULL);
  544. }
  545. /**
  546. * gfs2_create_inode - Create a new inode
  547. * @dir: The parent directory
  548. * @dentry: The new dentry
  549. * @mode: The permissions on the new inode
  550. * @dev: For device nodes, this is the device number
  551. * @symname: For symlinks, this is the link destination
  552. * @size: The initial size of the inode (ignored for directories)
  553. *
  554. * Returns: 0 on success, or error code
  555. */
  556. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  557. umode_t mode, dev_t dev, const char *symname,
  558. unsigned int size, int excl)
  559. {
  560. const struct qstr *name = &dentry->d_name;
  561. struct gfs2_holder ghs[2];
  562. struct inode *inode = NULL;
  563. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  564. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  565. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  566. int error;
  567. u64 generation;
  568. struct buffer_head *bh = NULL;
  569. if (!name->len || name->len > GFS2_FNAMESIZE)
  570. return -ENAMETOOLONG;
  571. /* We need a reservation to allocate the new dinode block. The
  572. directory ip temporarily points to the reservation, but this is
  573. being done to get a set of contiguous blocks for the new dinode.
  574. Since this is a create, we don't have a sizehint yet, so it will
  575. have to use the minimum reservation size. */
  576. error = gfs2_rs_alloc(dip);
  577. if (error)
  578. return error;
  579. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  580. if (error)
  581. goto fail;
  582. error = create_ok(dip, name, mode);
  583. if ((error == -EEXIST) && S_ISREG(mode) && !excl) {
  584. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  585. gfs2_glock_dq_uninit(ghs);
  586. d_instantiate(dentry, inode);
  587. return IS_ERR(inode) ? PTR_ERR(inode) : 0;
  588. }
  589. if (error)
  590. goto fail_gunlock;
  591. error = alloc_dinode(dip, &inum.no_addr, &generation);
  592. if (error)
  593. goto fail_gunlock;
  594. inum.no_formal_ino = generation;
  595. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  596. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  597. if (error)
  598. goto fail_gunlock;
  599. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, symname, size, &bh);
  600. if (error)
  601. goto fail_gunlock2;
  602. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  603. inum.no_formal_ino, 0);
  604. if (IS_ERR(inode))
  605. goto fail_gunlock2;
  606. ip = GFS2_I(inode);
  607. error = gfs2_inode_refresh(ip);
  608. if (error)
  609. goto fail_gunlock2;
  610. error = gfs2_rs_alloc(ip);
  611. if (error)
  612. goto fail_gunlock2;
  613. error = gfs2_acl_create(dip, inode);
  614. if (error)
  615. goto fail_gunlock2;
  616. error = gfs2_security_init(dip, ip, name);
  617. if (error)
  618. goto fail_gunlock2;
  619. error = link_dinode(dip, name, ip);
  620. if (error)
  621. goto fail_gunlock2;
  622. if (bh)
  623. brelse(bh);
  624. gfs2_trans_end(sdp);
  625. gfs2_inplace_release(dip);
  626. gfs2_quota_unlock(dip);
  627. mark_inode_dirty(inode);
  628. gfs2_glock_dq_uninit_m(2, ghs);
  629. d_instantiate(dentry, inode);
  630. return 0;
  631. fail_gunlock2:
  632. gfs2_glock_dq_uninit(ghs + 1);
  633. fail_gunlock:
  634. gfs2_glock_dq_uninit(ghs);
  635. if (inode && !IS_ERR(inode)) {
  636. set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
  637. iput(inode);
  638. }
  639. fail:
  640. gfs2_rs_delete(dip);
  641. if (bh)
  642. brelse(bh);
  643. return error;
  644. }
  645. /**
  646. * gfs2_create - Create a file
  647. * @dir: The directory in which to create the file
  648. * @dentry: The dentry of the new file
  649. * @mode: The mode of the new file
  650. *
  651. * Returns: errno
  652. */
  653. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  654. umode_t mode, bool excl)
  655. {
  656. return gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0, excl);
  657. }
  658. /**
  659. * gfs2_lookup - Look up a filename in a directory and return its inode
  660. * @dir: The directory inode
  661. * @dentry: The dentry of the new inode
  662. * @nd: passed from Linux VFS, ignored by us
  663. *
  664. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  665. *
  666. * Returns: errno
  667. */
  668. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  669. unsigned int flags)
  670. {
  671. struct inode *inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  672. if (inode && !IS_ERR(inode)) {
  673. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  674. struct gfs2_holder gh;
  675. int error;
  676. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  677. if (error) {
  678. iput(inode);
  679. return ERR_PTR(error);
  680. }
  681. gfs2_glock_dq_uninit(&gh);
  682. }
  683. return d_splice_alias(inode, dentry);
  684. }
  685. /**
  686. * gfs2_link - Link to a file
  687. * @old_dentry: The inode to link
  688. * @dir: Add link to this directory
  689. * @dentry: The name of the link
  690. *
  691. * Link the inode in "old_dentry" into the directory "dir" with the
  692. * name in "dentry".
  693. *
  694. * Returns: errno
  695. */
  696. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  697. struct dentry *dentry)
  698. {
  699. struct gfs2_inode *dip = GFS2_I(dir);
  700. struct gfs2_sbd *sdp = GFS2_SB(dir);
  701. struct inode *inode = old_dentry->d_inode;
  702. struct gfs2_inode *ip = GFS2_I(inode);
  703. struct gfs2_holder ghs[2];
  704. struct buffer_head *dibh;
  705. int alloc_required;
  706. int error;
  707. if (S_ISDIR(inode->i_mode))
  708. return -EPERM;
  709. error = gfs2_rs_alloc(dip);
  710. if (error)
  711. return error;
  712. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  713. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  714. error = gfs2_glock_nq(ghs); /* parent */
  715. if (error)
  716. goto out_parent;
  717. error = gfs2_glock_nq(ghs + 1); /* child */
  718. if (error)
  719. goto out_child;
  720. error = -ENOENT;
  721. if (inode->i_nlink == 0)
  722. goto out_gunlock;
  723. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  724. if (error)
  725. goto out_gunlock;
  726. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  727. switch (error) {
  728. case -ENOENT:
  729. break;
  730. case 0:
  731. error = -EEXIST;
  732. default:
  733. goto out_gunlock;
  734. }
  735. error = -EINVAL;
  736. if (!dip->i_inode.i_nlink)
  737. goto out_gunlock;
  738. error = -EFBIG;
  739. if (dip->i_entries == (u32)-1)
  740. goto out_gunlock;
  741. error = -EPERM;
  742. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  743. goto out_gunlock;
  744. error = -EINVAL;
  745. if (!ip->i_inode.i_nlink)
  746. goto out_gunlock;
  747. error = -EMLINK;
  748. if (ip->i_inode.i_nlink == (u32)-1)
  749. goto out_gunlock;
  750. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  751. if (error < 0)
  752. goto out_gunlock;
  753. error = 0;
  754. if (alloc_required) {
  755. error = gfs2_quota_lock_check(dip);
  756. if (error)
  757. goto out_gunlock;
  758. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres);
  759. if (error)
  760. goto out_gunlock_q;
  761. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  762. gfs2_rg_blocks(dip, sdp->sd_max_dirres) +
  763. 2 * RES_DINODE + RES_STATFS +
  764. RES_QUOTA, 0);
  765. if (error)
  766. goto out_ipres;
  767. } else {
  768. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  769. if (error)
  770. goto out_ipres;
  771. }
  772. error = gfs2_meta_inode_buffer(ip, &dibh);
  773. if (error)
  774. goto out_end_trans;
  775. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  776. if (error)
  777. goto out_brelse;
  778. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  779. inc_nlink(&ip->i_inode);
  780. ip->i_inode.i_ctime = CURRENT_TIME;
  781. ihold(inode);
  782. d_instantiate(dentry, inode);
  783. mark_inode_dirty(inode);
  784. out_brelse:
  785. brelse(dibh);
  786. out_end_trans:
  787. gfs2_trans_end(sdp);
  788. out_ipres:
  789. if (alloc_required)
  790. gfs2_inplace_release(dip);
  791. out_gunlock_q:
  792. if (alloc_required)
  793. gfs2_quota_unlock(dip);
  794. out_gunlock:
  795. gfs2_glock_dq(ghs + 1);
  796. out_child:
  797. gfs2_glock_dq(ghs);
  798. out_parent:
  799. gfs2_holder_uninit(ghs);
  800. gfs2_holder_uninit(ghs + 1);
  801. return error;
  802. }
  803. /*
  804. * gfs2_unlink_ok - check to see that a inode is still in a directory
  805. * @dip: the directory
  806. * @name: the name of the file
  807. * @ip: the inode
  808. *
  809. * Assumes that the lock on (at least) @dip is held.
  810. *
  811. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  812. */
  813. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  814. const struct gfs2_inode *ip)
  815. {
  816. int error;
  817. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  818. return -EPERM;
  819. if ((dip->i_inode.i_mode & S_ISVTX) &&
  820. dip->i_inode.i_uid != current_fsuid() &&
  821. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  822. return -EPERM;
  823. if (IS_APPEND(&dip->i_inode))
  824. return -EPERM;
  825. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  826. if (error)
  827. return error;
  828. error = gfs2_dir_check(&dip->i_inode, name, ip);
  829. if (error)
  830. return error;
  831. return 0;
  832. }
  833. /**
  834. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  835. * @dip: The parent directory
  836. * @name: The name of the entry in the parent directory
  837. * @bh: The inode buffer for the inode to be removed
  838. * @inode: The inode to be removed
  839. *
  840. * Called with all the locks and in a transaction. This will only be
  841. * called for a directory after it has been checked to ensure it is empty.
  842. *
  843. * Returns: 0 on success, or an error
  844. */
  845. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  846. const struct dentry *dentry,
  847. struct buffer_head *bh)
  848. {
  849. struct inode *inode = dentry->d_inode;
  850. struct gfs2_inode *ip = GFS2_I(inode);
  851. int error;
  852. error = gfs2_dir_del(dip, dentry);
  853. if (error)
  854. return error;
  855. ip->i_entries = 0;
  856. inode->i_ctime = CURRENT_TIME;
  857. if (S_ISDIR(inode->i_mode))
  858. clear_nlink(inode);
  859. else
  860. drop_nlink(inode);
  861. mark_inode_dirty(inode);
  862. if (inode->i_nlink == 0)
  863. gfs2_unlink_di(inode);
  864. return 0;
  865. }
  866. /**
  867. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  868. * @dir: The inode of the directory containing the inode to unlink
  869. * @dentry: The file itself
  870. *
  871. * This routine uses the type of the inode as a flag to figure out
  872. * whether this is an unlink or an rmdir.
  873. *
  874. * Returns: errno
  875. */
  876. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  877. {
  878. struct gfs2_inode *dip = GFS2_I(dir);
  879. struct gfs2_sbd *sdp = GFS2_SB(dir);
  880. struct inode *inode = dentry->d_inode;
  881. struct gfs2_inode *ip = GFS2_I(inode);
  882. struct buffer_head *bh;
  883. struct gfs2_holder ghs[3];
  884. struct gfs2_rgrpd *rgd;
  885. int error;
  886. error = gfs2_rindex_update(sdp);
  887. if (error)
  888. return error;
  889. error = -EROFS;
  890. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  891. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  892. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  893. if (!rgd)
  894. goto out_inodes;
  895. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  896. error = gfs2_glock_nq(ghs); /* parent */
  897. if (error)
  898. goto out_parent;
  899. error = gfs2_glock_nq(ghs + 1); /* child */
  900. if (error)
  901. goto out_child;
  902. error = -ENOENT;
  903. if (inode->i_nlink == 0)
  904. goto out_rgrp;
  905. if (S_ISDIR(inode->i_mode)) {
  906. error = -ENOTEMPTY;
  907. if (ip->i_entries > 2 || inode->i_nlink > 2)
  908. goto out_rgrp;
  909. }
  910. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  911. if (error)
  912. goto out_rgrp;
  913. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  914. if (error)
  915. goto out_gunlock;
  916. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  917. if (error)
  918. goto out_gunlock;
  919. error = gfs2_meta_inode_buffer(ip, &bh);
  920. if (error)
  921. goto out_end_trans;
  922. error = gfs2_unlink_inode(dip, dentry, bh);
  923. brelse(bh);
  924. out_end_trans:
  925. gfs2_trans_end(sdp);
  926. out_gunlock:
  927. gfs2_glock_dq(ghs + 2);
  928. out_rgrp:
  929. gfs2_glock_dq(ghs + 1);
  930. out_child:
  931. gfs2_glock_dq(ghs);
  932. out_parent:
  933. gfs2_holder_uninit(ghs + 2);
  934. out_inodes:
  935. gfs2_holder_uninit(ghs + 1);
  936. gfs2_holder_uninit(ghs);
  937. return error;
  938. }
  939. /**
  940. * gfs2_symlink - Create a symlink
  941. * @dir: The directory to create the symlink in
  942. * @dentry: The dentry to put the symlink in
  943. * @symname: The thing which the link points to
  944. *
  945. * Returns: errno
  946. */
  947. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  948. const char *symname)
  949. {
  950. struct gfs2_sbd *sdp = GFS2_SB(dir);
  951. unsigned int size;
  952. size = strlen(symname);
  953. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  954. return -ENAMETOOLONG;
  955. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
  956. }
  957. /**
  958. * gfs2_mkdir - Make a directory
  959. * @dir: The parent directory of the new one
  960. * @dentry: The dentry of the new directory
  961. * @mode: The mode of the new directory
  962. *
  963. * Returns: errno
  964. */
  965. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  966. {
  967. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0, 0);
  968. }
  969. /**
  970. * gfs2_mknod - Make a special file
  971. * @dir: The directory in which the special file will reside
  972. * @dentry: The dentry of the special file
  973. * @mode: The mode of the special file
  974. * @dev: The device specification of the special file
  975. *
  976. */
  977. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  978. dev_t dev)
  979. {
  980. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0, 0);
  981. }
  982. /*
  983. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  984. * @this: move this
  985. * @to: to here
  986. *
  987. * Follow @to back to the root and make sure we don't encounter @this
  988. * Assumes we already hold the rename lock.
  989. *
  990. * Returns: errno
  991. */
  992. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  993. {
  994. struct inode *dir = &to->i_inode;
  995. struct super_block *sb = dir->i_sb;
  996. struct inode *tmp;
  997. int error = 0;
  998. igrab(dir);
  999. for (;;) {
  1000. if (dir == &this->i_inode) {
  1001. error = -EINVAL;
  1002. break;
  1003. }
  1004. if (dir == sb->s_root->d_inode) {
  1005. error = 0;
  1006. break;
  1007. }
  1008. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1009. if (IS_ERR(tmp)) {
  1010. error = PTR_ERR(tmp);
  1011. break;
  1012. }
  1013. iput(dir);
  1014. dir = tmp;
  1015. }
  1016. iput(dir);
  1017. return error;
  1018. }
  1019. /**
  1020. * gfs2_rename - Rename a file
  1021. * @odir: Parent directory of old file name
  1022. * @odentry: The old dentry of the file
  1023. * @ndir: Parent directory of new file name
  1024. * @ndentry: The new dentry of the file
  1025. *
  1026. * Returns: errno
  1027. */
  1028. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1029. struct inode *ndir, struct dentry *ndentry)
  1030. {
  1031. struct gfs2_inode *odip = GFS2_I(odir);
  1032. struct gfs2_inode *ndip = GFS2_I(ndir);
  1033. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1034. struct gfs2_inode *nip = NULL;
  1035. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1036. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  1037. struct gfs2_rgrpd *nrgd;
  1038. unsigned int num_gh;
  1039. int dir_rename = 0;
  1040. int alloc_required = 0;
  1041. unsigned int x;
  1042. int error;
  1043. if (ndentry->d_inode) {
  1044. nip = GFS2_I(ndentry->d_inode);
  1045. if (ip == nip)
  1046. return 0;
  1047. }
  1048. error = gfs2_rindex_update(sdp);
  1049. if (error)
  1050. return error;
  1051. error = gfs2_rs_alloc(ndip);
  1052. if (error)
  1053. return error;
  1054. if (odip != ndip) {
  1055. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1056. 0, &r_gh);
  1057. if (error)
  1058. goto out;
  1059. if (S_ISDIR(ip->i_inode.i_mode)) {
  1060. dir_rename = 1;
  1061. /* don't move a dirctory into it's subdir */
  1062. error = gfs2_ok_to_move(ip, ndip);
  1063. if (error)
  1064. goto out_gunlock_r;
  1065. }
  1066. }
  1067. num_gh = 1;
  1068. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1069. if (odip != ndip) {
  1070. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1071. num_gh++;
  1072. }
  1073. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1074. num_gh++;
  1075. if (nip) {
  1076. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1077. num_gh++;
  1078. /* grab the resource lock for unlink flag twiddling
  1079. * this is the case of the target file already existing
  1080. * so we unlink before doing the rename
  1081. */
  1082. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1083. if (nrgd)
  1084. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1085. }
  1086. for (x = 0; x < num_gh; x++) {
  1087. error = gfs2_glock_nq(ghs + x);
  1088. if (error)
  1089. goto out_gunlock;
  1090. }
  1091. error = -ENOENT;
  1092. if (ip->i_inode.i_nlink == 0)
  1093. goto out_gunlock;
  1094. /* Check out the old directory */
  1095. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1096. if (error)
  1097. goto out_gunlock;
  1098. /* Check out the new directory */
  1099. if (nip) {
  1100. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1101. if (error)
  1102. goto out_gunlock;
  1103. if (nip->i_inode.i_nlink == 0) {
  1104. error = -EAGAIN;
  1105. goto out_gunlock;
  1106. }
  1107. if (S_ISDIR(nip->i_inode.i_mode)) {
  1108. if (nip->i_entries < 2) {
  1109. gfs2_consist_inode(nip);
  1110. error = -EIO;
  1111. goto out_gunlock;
  1112. }
  1113. if (nip->i_entries > 2) {
  1114. error = -ENOTEMPTY;
  1115. goto out_gunlock;
  1116. }
  1117. }
  1118. } else {
  1119. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1120. if (error)
  1121. goto out_gunlock;
  1122. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1123. switch (error) {
  1124. case -ENOENT:
  1125. error = 0;
  1126. break;
  1127. case 0:
  1128. error = -EEXIST;
  1129. default:
  1130. goto out_gunlock;
  1131. };
  1132. if (odip != ndip) {
  1133. if (!ndip->i_inode.i_nlink) {
  1134. error = -ENOENT;
  1135. goto out_gunlock;
  1136. }
  1137. if (ndip->i_entries == (u32)-1) {
  1138. error = -EFBIG;
  1139. goto out_gunlock;
  1140. }
  1141. if (S_ISDIR(ip->i_inode.i_mode) &&
  1142. ndip->i_inode.i_nlink == (u32)-1) {
  1143. error = -EMLINK;
  1144. goto out_gunlock;
  1145. }
  1146. }
  1147. }
  1148. /* Check out the dir to be renamed */
  1149. if (dir_rename) {
  1150. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1151. if (error)
  1152. goto out_gunlock;
  1153. }
  1154. if (nip == NULL)
  1155. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1156. error = alloc_required;
  1157. if (error < 0)
  1158. goto out_gunlock;
  1159. if (alloc_required) {
  1160. error = gfs2_quota_lock_check(ndip);
  1161. if (error)
  1162. goto out_gunlock;
  1163. error = gfs2_inplace_reserve(ndip, sdp->sd_max_dirres);
  1164. if (error)
  1165. goto out_gunlock_q;
  1166. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1167. gfs2_rg_blocks(ndip, sdp->sd_max_dirres) +
  1168. 4 * RES_DINODE + 4 * RES_LEAF +
  1169. RES_STATFS + RES_QUOTA + 4, 0);
  1170. if (error)
  1171. goto out_ipreserv;
  1172. } else {
  1173. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1174. 5 * RES_LEAF + 4, 0);
  1175. if (error)
  1176. goto out_gunlock;
  1177. }
  1178. /* Remove the target file, if it exists */
  1179. if (nip) {
  1180. struct buffer_head *bh;
  1181. error = gfs2_meta_inode_buffer(nip, &bh);
  1182. if (error)
  1183. goto out_end_trans;
  1184. error = gfs2_unlink_inode(ndip, ndentry, bh);
  1185. brelse(bh);
  1186. }
  1187. if (dir_rename) {
  1188. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1189. if (error)
  1190. goto out_end_trans;
  1191. } else {
  1192. struct buffer_head *dibh;
  1193. error = gfs2_meta_inode_buffer(ip, &dibh);
  1194. if (error)
  1195. goto out_end_trans;
  1196. ip->i_inode.i_ctime = CURRENT_TIME;
  1197. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1198. gfs2_dinode_out(ip, dibh->b_data);
  1199. brelse(dibh);
  1200. }
  1201. error = gfs2_dir_del(odip, odentry);
  1202. if (error)
  1203. goto out_end_trans;
  1204. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1205. if (error)
  1206. goto out_end_trans;
  1207. out_end_trans:
  1208. gfs2_trans_end(sdp);
  1209. out_ipreserv:
  1210. if (alloc_required)
  1211. gfs2_inplace_release(ndip);
  1212. out_gunlock_q:
  1213. if (alloc_required)
  1214. gfs2_quota_unlock(ndip);
  1215. out_gunlock:
  1216. while (x--) {
  1217. gfs2_glock_dq(ghs + x);
  1218. gfs2_holder_uninit(ghs + x);
  1219. }
  1220. out_gunlock_r:
  1221. if (r_gh.gh_gl)
  1222. gfs2_glock_dq_uninit(&r_gh);
  1223. out:
  1224. return error;
  1225. }
  1226. /**
  1227. * gfs2_follow_link - Follow a symbolic link
  1228. * @dentry: The dentry of the link
  1229. * @nd: Data that we pass to vfs_follow_link()
  1230. *
  1231. * This can handle symlinks of any size.
  1232. *
  1233. * Returns: 0 on success or error code
  1234. */
  1235. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1236. {
  1237. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1238. struct gfs2_holder i_gh;
  1239. struct buffer_head *dibh;
  1240. unsigned int size;
  1241. char *buf;
  1242. int error;
  1243. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1244. error = gfs2_glock_nq(&i_gh);
  1245. if (error) {
  1246. gfs2_holder_uninit(&i_gh);
  1247. nd_set_link(nd, ERR_PTR(error));
  1248. return NULL;
  1249. }
  1250. size = (unsigned int)i_size_read(&ip->i_inode);
  1251. if (size == 0) {
  1252. gfs2_consist_inode(ip);
  1253. buf = ERR_PTR(-EIO);
  1254. goto out;
  1255. }
  1256. error = gfs2_meta_inode_buffer(ip, &dibh);
  1257. if (error) {
  1258. buf = ERR_PTR(error);
  1259. goto out;
  1260. }
  1261. buf = kzalloc(size + 1, GFP_NOFS);
  1262. if (!buf)
  1263. buf = ERR_PTR(-ENOMEM);
  1264. else
  1265. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1266. brelse(dibh);
  1267. out:
  1268. gfs2_glock_dq_uninit(&i_gh);
  1269. nd_set_link(nd, buf);
  1270. return NULL;
  1271. }
  1272. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1273. {
  1274. char *s = nd_get_link(nd);
  1275. if (!IS_ERR(s))
  1276. kfree(s);
  1277. }
  1278. /**
  1279. * gfs2_permission -
  1280. * @inode: The inode
  1281. * @mask: The mask to be tested
  1282. * @flags: Indicates whether this is an RCU path walk or not
  1283. *
  1284. * This may be called from the VFS directly, or from within GFS2 with the
  1285. * inode locked, so we look to see if the glock is already locked and only
  1286. * lock the glock if its not already been done.
  1287. *
  1288. * Returns: errno
  1289. */
  1290. int gfs2_permission(struct inode *inode, int mask)
  1291. {
  1292. struct gfs2_inode *ip;
  1293. struct gfs2_holder i_gh;
  1294. int error;
  1295. int unlock = 0;
  1296. ip = GFS2_I(inode);
  1297. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1298. if (mask & MAY_NOT_BLOCK)
  1299. return -ECHILD;
  1300. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1301. if (error)
  1302. return error;
  1303. unlock = 1;
  1304. }
  1305. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1306. error = -EACCES;
  1307. else
  1308. error = generic_permission(inode, mask);
  1309. if (unlock)
  1310. gfs2_glock_dq_uninit(&i_gh);
  1311. return error;
  1312. }
  1313. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1314. {
  1315. setattr_copy(inode, attr);
  1316. mark_inode_dirty(inode);
  1317. return 0;
  1318. }
  1319. /**
  1320. * gfs2_setattr_simple -
  1321. * @ip:
  1322. * @attr:
  1323. *
  1324. * Returns: errno
  1325. */
  1326. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1327. {
  1328. int error;
  1329. if (current->journal_info)
  1330. return __gfs2_setattr_simple(inode, attr);
  1331. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1332. if (error)
  1333. return error;
  1334. error = __gfs2_setattr_simple(inode, attr);
  1335. gfs2_trans_end(GFS2_SB(inode));
  1336. return error;
  1337. }
  1338. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1339. {
  1340. struct gfs2_inode *ip = GFS2_I(inode);
  1341. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1342. u32 ouid, ogid, nuid, ngid;
  1343. int error;
  1344. ouid = inode->i_uid;
  1345. ogid = inode->i_gid;
  1346. nuid = attr->ia_uid;
  1347. ngid = attr->ia_gid;
  1348. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  1349. ouid = nuid = NO_QUOTA_CHANGE;
  1350. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  1351. ogid = ngid = NO_QUOTA_CHANGE;
  1352. error = gfs2_quota_lock(ip, nuid, ngid);
  1353. if (error)
  1354. return error;
  1355. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1356. error = gfs2_quota_check(ip, nuid, ngid);
  1357. if (error)
  1358. goto out_gunlock_q;
  1359. }
  1360. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1361. if (error)
  1362. goto out_gunlock_q;
  1363. error = gfs2_setattr_simple(inode, attr);
  1364. if (error)
  1365. goto out_end_trans;
  1366. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1367. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1368. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1369. gfs2_quota_change(ip, blocks, nuid, ngid);
  1370. }
  1371. out_end_trans:
  1372. gfs2_trans_end(sdp);
  1373. out_gunlock_q:
  1374. gfs2_quota_unlock(ip);
  1375. return error;
  1376. }
  1377. /**
  1378. * gfs2_setattr - Change attributes on an inode
  1379. * @dentry: The dentry which is changing
  1380. * @attr: The structure describing the change
  1381. *
  1382. * The VFS layer wants to change one or more of an inodes attributes. Write
  1383. * that change out to disk.
  1384. *
  1385. * Returns: errno
  1386. */
  1387. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1388. {
  1389. struct inode *inode = dentry->d_inode;
  1390. struct gfs2_inode *ip = GFS2_I(inode);
  1391. struct gfs2_holder i_gh;
  1392. int error;
  1393. error = gfs2_rs_alloc(ip);
  1394. if (error)
  1395. return error;
  1396. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1397. if (error)
  1398. return error;
  1399. error = -EPERM;
  1400. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1401. goto out;
  1402. error = inode_change_ok(inode, attr);
  1403. if (error)
  1404. goto out;
  1405. if (attr->ia_valid & ATTR_SIZE)
  1406. error = gfs2_setattr_size(inode, attr->ia_size);
  1407. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1408. error = setattr_chown(inode, attr);
  1409. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1410. error = gfs2_acl_chmod(ip, attr);
  1411. else
  1412. error = gfs2_setattr_simple(inode, attr);
  1413. out:
  1414. if (!error)
  1415. mark_inode_dirty(inode);
  1416. gfs2_glock_dq_uninit(&i_gh);
  1417. return error;
  1418. }
  1419. /**
  1420. * gfs2_getattr - Read out an inode's attributes
  1421. * @mnt: The vfsmount the inode is being accessed from
  1422. * @dentry: The dentry to stat
  1423. * @stat: The inode's stats
  1424. *
  1425. * This may be called from the VFS directly, or from within GFS2 with the
  1426. * inode locked, so we look to see if the glock is already locked and only
  1427. * lock the glock if its not already been done. Note that its the NFS
  1428. * readdirplus operation which causes this to be called (from filldir)
  1429. * with the glock already held.
  1430. *
  1431. * Returns: errno
  1432. */
  1433. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1434. struct kstat *stat)
  1435. {
  1436. struct inode *inode = dentry->d_inode;
  1437. struct gfs2_inode *ip = GFS2_I(inode);
  1438. struct gfs2_holder gh;
  1439. int error;
  1440. int unlock = 0;
  1441. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1442. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1443. if (error)
  1444. return error;
  1445. unlock = 1;
  1446. }
  1447. generic_fillattr(inode, stat);
  1448. if (unlock)
  1449. gfs2_glock_dq_uninit(&gh);
  1450. return 0;
  1451. }
  1452. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1453. const void *data, size_t size, int flags)
  1454. {
  1455. struct inode *inode = dentry->d_inode;
  1456. struct gfs2_inode *ip = GFS2_I(inode);
  1457. struct gfs2_holder gh;
  1458. int ret;
  1459. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1460. ret = gfs2_glock_nq(&gh);
  1461. if (ret == 0) {
  1462. ret = gfs2_rs_alloc(ip);
  1463. if (ret == 0)
  1464. ret = generic_setxattr(dentry, name, data, size, flags);
  1465. gfs2_glock_dq(&gh);
  1466. }
  1467. gfs2_holder_uninit(&gh);
  1468. return ret;
  1469. }
  1470. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1471. void *data, size_t size)
  1472. {
  1473. struct inode *inode = dentry->d_inode;
  1474. struct gfs2_inode *ip = GFS2_I(inode);
  1475. struct gfs2_holder gh;
  1476. int ret;
  1477. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1478. ret = gfs2_glock_nq(&gh);
  1479. if (ret == 0) {
  1480. ret = generic_getxattr(dentry, name, data, size);
  1481. gfs2_glock_dq(&gh);
  1482. }
  1483. gfs2_holder_uninit(&gh);
  1484. return ret;
  1485. }
  1486. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1487. {
  1488. struct inode *inode = dentry->d_inode;
  1489. struct gfs2_inode *ip = GFS2_I(inode);
  1490. struct gfs2_holder gh;
  1491. int ret;
  1492. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1493. ret = gfs2_glock_nq(&gh);
  1494. if (ret == 0) {
  1495. ret = gfs2_rs_alloc(ip);
  1496. if (ret == 0)
  1497. ret = generic_removexattr(dentry, name);
  1498. gfs2_glock_dq(&gh);
  1499. }
  1500. gfs2_holder_uninit(&gh);
  1501. return ret;
  1502. }
  1503. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1504. u64 start, u64 len)
  1505. {
  1506. struct gfs2_inode *ip = GFS2_I(inode);
  1507. struct gfs2_holder gh;
  1508. int ret;
  1509. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1510. if (ret)
  1511. return ret;
  1512. mutex_lock(&inode->i_mutex);
  1513. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1514. if (ret)
  1515. goto out;
  1516. if (gfs2_is_stuffed(ip)) {
  1517. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1518. u64 size = i_size_read(inode);
  1519. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1520. FIEMAP_EXTENT_DATA_INLINE;
  1521. phys += sizeof(struct gfs2_dinode);
  1522. phys += start;
  1523. if (start + len > size)
  1524. len = size - start;
  1525. if (start < size)
  1526. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1527. len, flags);
  1528. if (ret == 1)
  1529. ret = 0;
  1530. } else {
  1531. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1532. gfs2_block_map);
  1533. }
  1534. gfs2_glock_dq_uninit(&gh);
  1535. out:
  1536. mutex_unlock(&inode->i_mutex);
  1537. return ret;
  1538. }
  1539. const struct inode_operations gfs2_file_iops = {
  1540. .permission = gfs2_permission,
  1541. .setattr = gfs2_setattr,
  1542. .getattr = gfs2_getattr,
  1543. .setxattr = gfs2_setxattr,
  1544. .getxattr = gfs2_getxattr,
  1545. .listxattr = gfs2_listxattr,
  1546. .removexattr = gfs2_removexattr,
  1547. .fiemap = gfs2_fiemap,
  1548. .get_acl = gfs2_get_acl,
  1549. };
  1550. const struct inode_operations gfs2_dir_iops = {
  1551. .create = gfs2_create,
  1552. .lookup = gfs2_lookup,
  1553. .link = gfs2_link,
  1554. .unlink = gfs2_unlink,
  1555. .symlink = gfs2_symlink,
  1556. .mkdir = gfs2_mkdir,
  1557. .rmdir = gfs2_unlink,
  1558. .mknod = gfs2_mknod,
  1559. .rename = gfs2_rename,
  1560. .permission = gfs2_permission,
  1561. .setattr = gfs2_setattr,
  1562. .getattr = gfs2_getattr,
  1563. .setxattr = gfs2_setxattr,
  1564. .getxattr = gfs2_getxattr,
  1565. .listxattr = gfs2_listxattr,
  1566. .removexattr = gfs2_removexattr,
  1567. .fiemap = gfs2_fiemap,
  1568. .get_acl = gfs2_get_acl,
  1569. };
  1570. const struct inode_operations gfs2_symlink_iops = {
  1571. .readlink = generic_readlink,
  1572. .follow_link = gfs2_follow_link,
  1573. .put_link = gfs2_put_link,
  1574. .permission = gfs2_permission,
  1575. .setattr = gfs2_setattr,
  1576. .getattr = gfs2_getattr,
  1577. .setxattr = gfs2_setxattr,
  1578. .getxattr = gfs2_getxattr,
  1579. .listxattr = gfs2_listxattr,
  1580. .removexattr = gfs2_removexattr,
  1581. .fiemap = gfs2_fiemap,
  1582. .get_acl = gfs2_get_acl,
  1583. };