inode.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  1. /*
  2. * fs/cifs/inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2010
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/slab.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/freezer.h>
  26. #include <linux/sched/signal.h>
  27. #include <linux/wait_bit.h>
  28. #include <asm/div64.h>
  29. #include "cifsfs.h"
  30. #include "cifspdu.h"
  31. #include "cifsglob.h"
  32. #include "cifsproto.h"
  33. #include "cifs_debug.h"
  34. #include "cifs_fs_sb.h"
  35. #include "cifs_unicode.h"
  36. #include "fscache.h"
  37. static void cifs_set_ops(struct inode *inode)
  38. {
  39. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  40. switch (inode->i_mode & S_IFMT) {
  41. case S_IFREG:
  42. inode->i_op = &cifs_file_inode_ops;
  43. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  44. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  45. inode->i_fop = &cifs_file_direct_nobrl_ops;
  46. else
  47. inode->i_fop = &cifs_file_direct_ops;
  48. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  49. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  50. inode->i_fop = &cifs_file_strict_nobrl_ops;
  51. else
  52. inode->i_fop = &cifs_file_strict_ops;
  53. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  54. inode->i_fop = &cifs_file_nobrl_ops;
  55. else { /* not direct, send byte range locks */
  56. inode->i_fop = &cifs_file_ops;
  57. }
  58. /* check if server can support readpages */
  59. if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
  60. PAGE_SIZE + MAX_CIFS_HDR_SIZE)
  61. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  62. else
  63. inode->i_data.a_ops = &cifs_addr_ops;
  64. break;
  65. case S_IFDIR:
  66. #ifdef CONFIG_CIFS_DFS_UPCALL
  67. if (IS_AUTOMOUNT(inode)) {
  68. inode->i_op = &cifs_dfs_referral_inode_operations;
  69. } else {
  70. #else /* NO DFS support, treat as a directory */
  71. {
  72. #endif
  73. inode->i_op = &cifs_dir_inode_ops;
  74. inode->i_fop = &cifs_dir_ops;
  75. }
  76. break;
  77. case S_IFLNK:
  78. inode->i_op = &cifs_symlink_inode_ops;
  79. break;
  80. default:
  81. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  82. break;
  83. }
  84. }
  85. /* check inode attributes against fattr. If they don't match, tag the
  86. * inode for cache invalidation
  87. */
  88. static void
  89. cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
  90. {
  91. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  92. cifs_dbg(FYI, "%s: revalidating inode %llu\n",
  93. __func__, cifs_i->uniqueid);
  94. if (inode->i_state & I_NEW) {
  95. cifs_dbg(FYI, "%s: inode %llu is new\n",
  96. __func__, cifs_i->uniqueid);
  97. return;
  98. }
  99. /* don't bother with revalidation if we have an oplock */
  100. if (CIFS_CACHE_READ(cifs_i)) {
  101. cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
  102. __func__, cifs_i->uniqueid);
  103. return;
  104. }
  105. /* revalidate if mtime or size have changed */
  106. if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
  107. cifs_i->server_eof == fattr->cf_eof) {
  108. cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
  109. __func__, cifs_i->uniqueid);
  110. return;
  111. }
  112. cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
  113. __func__, cifs_i->uniqueid);
  114. set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
  115. }
  116. /*
  117. * copy nlink to the inode, unless it wasn't provided. Provide
  118. * sane values if we don't have an existing one and none was provided
  119. */
  120. static void
  121. cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  122. {
  123. /*
  124. * if we're in a situation where we can't trust what we
  125. * got from the server (readdir, some non-unix cases)
  126. * fake reasonable values
  127. */
  128. if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
  129. /* only provide fake values on a new inode */
  130. if (inode->i_state & I_NEW) {
  131. if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
  132. set_nlink(inode, 2);
  133. else
  134. set_nlink(inode, 1);
  135. }
  136. return;
  137. }
  138. /* we trust the server, so update it */
  139. set_nlink(inode, fattr->cf_nlink);
  140. }
  141. /* populate an inode with info from a cifs_fattr struct */
  142. void
  143. cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  144. {
  145. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  146. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  147. cifs_revalidate_cache(inode, fattr);
  148. spin_lock(&inode->i_lock);
  149. inode->i_atime = fattr->cf_atime;
  150. inode->i_mtime = fattr->cf_mtime;
  151. inode->i_ctime = fattr->cf_ctime;
  152. inode->i_rdev = fattr->cf_rdev;
  153. cifs_nlink_fattr_to_inode(inode, fattr);
  154. inode->i_uid = fattr->cf_uid;
  155. inode->i_gid = fattr->cf_gid;
  156. /* if dynperm is set, don't clobber existing mode */
  157. if (inode->i_state & I_NEW ||
  158. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
  159. inode->i_mode = fattr->cf_mode;
  160. cifs_i->cifsAttrs = fattr->cf_cifsattrs;
  161. if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
  162. cifs_i->time = 0;
  163. else
  164. cifs_i->time = jiffies;
  165. if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
  166. set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
  167. else
  168. clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
  169. cifs_i->server_eof = fattr->cf_eof;
  170. /*
  171. * Can't safely change the file size here if the client is writing to
  172. * it due to potential races.
  173. */
  174. if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
  175. i_size_write(inode, fattr->cf_eof);
  176. /*
  177. * i_blocks is not related to (i_size / i_blksize),
  178. * but instead 512 byte (2**9) size is required for
  179. * calculating num blocks.
  180. */
  181. inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
  182. }
  183. spin_unlock(&inode->i_lock);
  184. if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
  185. inode->i_flags |= S_AUTOMOUNT;
  186. if (inode->i_state & I_NEW)
  187. cifs_set_ops(inode);
  188. }
  189. void
  190. cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
  191. {
  192. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  193. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  194. return;
  195. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  196. }
  197. /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
  198. void
  199. cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
  200. struct cifs_sb_info *cifs_sb)
  201. {
  202. memset(fattr, 0, sizeof(*fattr));
  203. fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
  204. fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
  205. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  206. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  207. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
  208. fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
  209. /* old POSIX extensions don't get create time */
  210. fattr->cf_mode = le64_to_cpu(info->Permissions);
  211. /*
  212. * Since we set the inode type below we need to mask off
  213. * to avoid strange results if bits set above.
  214. */
  215. fattr->cf_mode &= ~S_IFMT;
  216. switch (le32_to_cpu(info->Type)) {
  217. case UNIX_FILE:
  218. fattr->cf_mode |= S_IFREG;
  219. fattr->cf_dtype = DT_REG;
  220. break;
  221. case UNIX_SYMLINK:
  222. fattr->cf_mode |= S_IFLNK;
  223. fattr->cf_dtype = DT_LNK;
  224. break;
  225. case UNIX_DIR:
  226. fattr->cf_mode |= S_IFDIR;
  227. fattr->cf_dtype = DT_DIR;
  228. break;
  229. case UNIX_CHARDEV:
  230. fattr->cf_mode |= S_IFCHR;
  231. fattr->cf_dtype = DT_CHR;
  232. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  233. le64_to_cpu(info->DevMinor) & MINORMASK);
  234. break;
  235. case UNIX_BLOCKDEV:
  236. fattr->cf_mode |= S_IFBLK;
  237. fattr->cf_dtype = DT_BLK;
  238. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  239. le64_to_cpu(info->DevMinor) & MINORMASK);
  240. break;
  241. case UNIX_FIFO:
  242. fattr->cf_mode |= S_IFIFO;
  243. fattr->cf_dtype = DT_FIFO;
  244. break;
  245. case UNIX_SOCKET:
  246. fattr->cf_mode |= S_IFSOCK;
  247. fattr->cf_dtype = DT_SOCK;
  248. break;
  249. default:
  250. /* safest to call it a file if we do not know */
  251. fattr->cf_mode |= S_IFREG;
  252. fattr->cf_dtype = DT_REG;
  253. cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
  254. break;
  255. }
  256. fattr->cf_uid = cifs_sb->mnt_uid;
  257. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
  258. u64 id = le64_to_cpu(info->Uid);
  259. if (id < ((uid_t)-1)) {
  260. kuid_t uid = make_kuid(&init_user_ns, id);
  261. if (uid_valid(uid))
  262. fattr->cf_uid = uid;
  263. }
  264. }
  265. fattr->cf_gid = cifs_sb->mnt_gid;
  266. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
  267. u64 id = le64_to_cpu(info->Gid);
  268. if (id < ((gid_t)-1)) {
  269. kgid_t gid = make_kgid(&init_user_ns, id);
  270. if (gid_valid(gid))
  271. fattr->cf_gid = gid;
  272. }
  273. }
  274. fattr->cf_nlink = le64_to_cpu(info->Nlinks);
  275. }
  276. /*
  277. * Fill a cifs_fattr struct with fake inode info.
  278. *
  279. * Needed to setup cifs_fattr data for the directory which is the
  280. * junction to the new submount (ie to setup the fake directory
  281. * which represents a DFS referral).
  282. */
  283. static void
  284. cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
  285. {
  286. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  287. cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
  288. memset(fattr, 0, sizeof(*fattr));
  289. fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
  290. fattr->cf_uid = cifs_sb->mnt_uid;
  291. fattr->cf_gid = cifs_sb->mnt_gid;
  292. ktime_get_real_ts(&fattr->cf_mtime);
  293. fattr->cf_mtime = timespec_trunc(fattr->cf_mtime, sb->s_time_gran);
  294. fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
  295. fattr->cf_nlink = 2;
  296. fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
  297. }
  298. static int
  299. cifs_get_file_info_unix(struct file *filp)
  300. {
  301. int rc;
  302. unsigned int xid;
  303. FILE_UNIX_BASIC_INFO find_data;
  304. struct cifs_fattr fattr;
  305. struct inode *inode = file_inode(filp);
  306. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  307. struct cifsFileInfo *cfile = filp->private_data;
  308. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  309. xid = get_xid();
  310. rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
  311. if (!rc) {
  312. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  313. } else if (rc == -EREMOTE) {
  314. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  315. rc = 0;
  316. }
  317. cifs_fattr_to_inode(inode, &fattr);
  318. free_xid(xid);
  319. return rc;
  320. }
  321. int cifs_get_inode_info_unix(struct inode **pinode,
  322. const unsigned char *full_path,
  323. struct super_block *sb, unsigned int xid)
  324. {
  325. int rc;
  326. FILE_UNIX_BASIC_INFO find_data;
  327. struct cifs_fattr fattr;
  328. struct cifs_tcon *tcon;
  329. struct tcon_link *tlink;
  330. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  331. cifs_dbg(FYI, "Getting info on %s\n", full_path);
  332. tlink = cifs_sb_tlink(cifs_sb);
  333. if (IS_ERR(tlink))
  334. return PTR_ERR(tlink);
  335. tcon = tlink_tcon(tlink);
  336. /* could have done a find first instead but this returns more info */
  337. rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
  338. cifs_sb->local_nls, cifs_remap(cifs_sb));
  339. cifs_put_tlink(tlink);
  340. if (!rc) {
  341. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  342. } else if (rc == -EREMOTE) {
  343. cifs_create_dfs_fattr(&fattr, sb);
  344. rc = 0;
  345. } else {
  346. return rc;
  347. }
  348. /* check for Minshall+French symlinks */
  349. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  350. int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  351. full_path);
  352. if (tmprc)
  353. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  354. }
  355. if (*pinode == NULL) {
  356. /* get new inode */
  357. cifs_fill_uniqueid(sb, &fattr);
  358. *pinode = cifs_iget(sb, &fattr);
  359. if (!*pinode)
  360. rc = -ENOMEM;
  361. } else {
  362. /* we already have inode, update it */
  363. /* if uniqueid is different, return error */
  364. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  365. CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
  366. rc = -ESTALE;
  367. goto cgiiu_exit;
  368. }
  369. /* if filetype is different, return error */
  370. if (unlikely(((*pinode)->i_mode & S_IFMT) !=
  371. (fattr.cf_mode & S_IFMT))) {
  372. rc = -ESTALE;
  373. goto cgiiu_exit;
  374. }
  375. cifs_fattr_to_inode(*pinode, &fattr);
  376. }
  377. cgiiu_exit:
  378. return rc;
  379. }
  380. static int
  381. cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
  382. struct cifs_sb_info *cifs_sb, unsigned int xid)
  383. {
  384. int rc;
  385. __u32 oplock;
  386. struct tcon_link *tlink;
  387. struct cifs_tcon *tcon;
  388. struct cifs_fid fid;
  389. struct cifs_open_parms oparms;
  390. struct cifs_io_parms io_parms;
  391. char buf[24];
  392. unsigned int bytes_read;
  393. char *pbuf;
  394. int buf_type = CIFS_NO_BUFFER;
  395. pbuf = buf;
  396. fattr->cf_mode &= ~S_IFMT;
  397. if (fattr->cf_eof == 0) {
  398. fattr->cf_mode |= S_IFIFO;
  399. fattr->cf_dtype = DT_FIFO;
  400. return 0;
  401. } else if (fattr->cf_eof < 8) {
  402. fattr->cf_mode |= S_IFREG;
  403. fattr->cf_dtype = DT_REG;
  404. return -EINVAL; /* EOPNOTSUPP? */
  405. }
  406. tlink = cifs_sb_tlink(cifs_sb);
  407. if (IS_ERR(tlink))
  408. return PTR_ERR(tlink);
  409. tcon = tlink_tcon(tlink);
  410. oparms.tcon = tcon;
  411. oparms.cifs_sb = cifs_sb;
  412. oparms.desired_access = GENERIC_READ;
  413. oparms.create_options = CREATE_NOT_DIR;
  414. oparms.disposition = FILE_OPEN;
  415. oparms.path = path;
  416. oparms.fid = &fid;
  417. oparms.reconnect = false;
  418. if (tcon->ses->server->oplocks)
  419. oplock = REQ_OPLOCK;
  420. else
  421. oplock = 0;
  422. rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
  423. if (rc) {
  424. cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
  425. cifs_put_tlink(tlink);
  426. return rc;
  427. }
  428. /* Read header */
  429. io_parms.netfid = fid.netfid;
  430. io_parms.pid = current->tgid;
  431. io_parms.tcon = tcon;
  432. io_parms.offset = 0;
  433. io_parms.length = 24;
  434. rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
  435. &bytes_read, &pbuf, &buf_type);
  436. if ((rc == 0) && (bytes_read >= 8)) {
  437. if (memcmp("IntxBLK", pbuf, 8) == 0) {
  438. cifs_dbg(FYI, "Block device\n");
  439. fattr->cf_mode |= S_IFBLK;
  440. fattr->cf_dtype = DT_BLK;
  441. if (bytes_read == 24) {
  442. /* we have enough to decode dev num */
  443. __u64 mjr; /* major */
  444. __u64 mnr; /* minor */
  445. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  446. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  447. fattr->cf_rdev = MKDEV(mjr, mnr);
  448. }
  449. } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
  450. cifs_dbg(FYI, "Char device\n");
  451. fattr->cf_mode |= S_IFCHR;
  452. fattr->cf_dtype = DT_CHR;
  453. if (bytes_read == 24) {
  454. /* we have enough to decode dev num */
  455. __u64 mjr; /* major */
  456. __u64 mnr; /* minor */
  457. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  458. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  459. fattr->cf_rdev = MKDEV(mjr, mnr);
  460. }
  461. } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
  462. cifs_dbg(FYI, "Symlink\n");
  463. fattr->cf_mode |= S_IFLNK;
  464. fattr->cf_dtype = DT_LNK;
  465. } else {
  466. fattr->cf_mode |= S_IFREG; /* file? */
  467. fattr->cf_dtype = DT_REG;
  468. rc = -EOPNOTSUPP;
  469. }
  470. } else {
  471. fattr->cf_mode |= S_IFREG; /* then it is a file */
  472. fattr->cf_dtype = DT_REG;
  473. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  474. }
  475. tcon->ses->server->ops->close(xid, tcon, &fid);
  476. cifs_put_tlink(tlink);
  477. return rc;
  478. }
  479. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  480. /*
  481. * Fetch mode bits as provided by SFU.
  482. *
  483. * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
  484. */
  485. static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
  486. struct cifs_sb_info *cifs_sb, unsigned int xid)
  487. {
  488. #ifdef CONFIG_CIFS_XATTR
  489. ssize_t rc;
  490. char ea_value[4];
  491. __u32 mode;
  492. struct tcon_link *tlink;
  493. struct cifs_tcon *tcon;
  494. tlink = cifs_sb_tlink(cifs_sb);
  495. if (IS_ERR(tlink))
  496. return PTR_ERR(tlink);
  497. tcon = tlink_tcon(tlink);
  498. if (tcon->ses->server->ops->query_all_EAs == NULL) {
  499. cifs_put_tlink(tlink);
  500. return -EOPNOTSUPP;
  501. }
  502. rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
  503. "SETFILEBITS", ea_value, 4 /* size of buf */,
  504. cifs_sb);
  505. cifs_put_tlink(tlink);
  506. if (rc < 0)
  507. return (int)rc;
  508. else if (rc > 3) {
  509. mode = le32_to_cpu(*((__le32 *)ea_value));
  510. fattr->cf_mode &= ~SFBITS_MASK;
  511. cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
  512. mode, fattr->cf_mode);
  513. fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
  514. cifs_dbg(FYI, "special mode bits 0%o\n", mode);
  515. }
  516. return 0;
  517. #else
  518. return -EOPNOTSUPP;
  519. #endif
  520. }
  521. /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
  522. static void
  523. cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
  524. struct super_block *sb, bool adjust_tz,
  525. bool symlink)
  526. {
  527. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  528. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  529. memset(fattr, 0, sizeof(*fattr));
  530. fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
  531. if (info->DeletePending)
  532. fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
  533. if (info->LastAccessTime)
  534. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  535. else {
  536. ktime_get_real_ts(&fattr->cf_atime);
  537. fattr->cf_atime = timespec_trunc(fattr->cf_atime, sb->s_time_gran);
  538. }
  539. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  540. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  541. if (adjust_tz) {
  542. fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
  543. fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
  544. }
  545. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  546. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  547. fattr->cf_createtime = le64_to_cpu(info->CreationTime);
  548. fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
  549. if (symlink) {
  550. fattr->cf_mode = S_IFLNK;
  551. fattr->cf_dtype = DT_LNK;
  552. } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  553. fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
  554. fattr->cf_dtype = DT_DIR;
  555. /*
  556. * Server can return wrong NumberOfLinks value for directories
  557. * when Unix extensions are disabled - fake it.
  558. */
  559. if (!tcon->unix_ext)
  560. fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
  561. } else {
  562. fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
  563. fattr->cf_dtype = DT_REG;
  564. /* clear write bits if ATTR_READONLY is set */
  565. if (fattr->cf_cifsattrs & ATTR_READONLY)
  566. fattr->cf_mode &= ~(S_IWUGO);
  567. /*
  568. * Don't accept zero nlink from non-unix servers unless
  569. * delete is pending. Instead mark it as unknown.
  570. */
  571. if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
  572. !info->DeletePending) {
  573. cifs_dbg(1, "bogus file nlink value %u\n",
  574. fattr->cf_nlink);
  575. fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
  576. }
  577. }
  578. fattr->cf_uid = cifs_sb->mnt_uid;
  579. fattr->cf_gid = cifs_sb->mnt_gid;
  580. }
  581. static int
  582. cifs_get_file_info(struct file *filp)
  583. {
  584. int rc;
  585. unsigned int xid;
  586. FILE_ALL_INFO find_data;
  587. struct cifs_fattr fattr;
  588. struct inode *inode = file_inode(filp);
  589. struct cifsFileInfo *cfile = filp->private_data;
  590. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  591. struct TCP_Server_Info *server = tcon->ses->server;
  592. if (!server->ops->query_file_info)
  593. return -ENOSYS;
  594. xid = get_xid();
  595. rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
  596. switch (rc) {
  597. case 0:
  598. cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
  599. false);
  600. break;
  601. case -EREMOTE:
  602. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  603. rc = 0;
  604. break;
  605. case -EOPNOTSUPP:
  606. case -EINVAL:
  607. /*
  608. * FIXME: legacy server -- fall back to path-based call?
  609. * for now, just skip revalidating and mark inode for
  610. * immediate reval.
  611. */
  612. rc = 0;
  613. CIFS_I(inode)->time = 0;
  614. default:
  615. goto cgfi_exit;
  616. }
  617. /*
  618. * don't bother with SFU junk here -- just mark inode as needing
  619. * revalidation.
  620. */
  621. fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
  622. fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
  623. cifs_fattr_to_inode(inode, &fattr);
  624. cgfi_exit:
  625. free_xid(xid);
  626. return rc;
  627. }
  628. int
  629. cifs_get_inode_info(struct inode **inode, const char *full_path,
  630. FILE_ALL_INFO *data, struct super_block *sb, int xid,
  631. const struct cifs_fid *fid)
  632. {
  633. bool validinum = false;
  634. __u16 srchflgs;
  635. int rc = 0, tmprc = ENOSYS;
  636. struct cifs_tcon *tcon;
  637. struct TCP_Server_Info *server;
  638. struct tcon_link *tlink;
  639. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  640. char *buf = NULL;
  641. bool adjust_tz = false;
  642. struct cifs_fattr fattr;
  643. struct cifs_search_info *srchinf = NULL;
  644. bool symlink = false;
  645. tlink = cifs_sb_tlink(cifs_sb);
  646. if (IS_ERR(tlink))
  647. return PTR_ERR(tlink);
  648. tcon = tlink_tcon(tlink);
  649. server = tcon->ses->server;
  650. cifs_dbg(FYI, "Getting info on %s\n", full_path);
  651. if ((data == NULL) && (*inode != NULL)) {
  652. if (CIFS_CACHE_READ(CIFS_I(*inode))) {
  653. cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
  654. goto cgii_exit;
  655. }
  656. }
  657. /* if inode info is not passed, get it from server */
  658. if (data == NULL) {
  659. if (!server->ops->query_path_info) {
  660. rc = -ENOSYS;
  661. goto cgii_exit;
  662. }
  663. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  664. if (buf == NULL) {
  665. rc = -ENOMEM;
  666. goto cgii_exit;
  667. }
  668. data = (FILE_ALL_INFO *)buf;
  669. rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
  670. data, &adjust_tz, &symlink);
  671. }
  672. if (!rc) {
  673. cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
  674. symlink);
  675. } else if (rc == -EREMOTE) {
  676. cifs_create_dfs_fattr(&fattr, sb);
  677. rc = 0;
  678. } else if (rc == -EACCES && backup_cred(cifs_sb)) {
  679. srchinf = kzalloc(sizeof(struct cifs_search_info),
  680. GFP_KERNEL);
  681. if (srchinf == NULL) {
  682. rc = -ENOMEM;
  683. goto cgii_exit;
  684. }
  685. srchinf->endOfSearch = false;
  686. srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  687. srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
  688. CIFS_SEARCH_CLOSE_AT_END |
  689. CIFS_SEARCH_BACKUP_SEARCH;
  690. rc = CIFSFindFirst(xid, tcon, full_path,
  691. cifs_sb, NULL, srchflgs, srchinf, false);
  692. if (!rc) {
  693. data =
  694. (FILE_ALL_INFO *)srchinf->srch_entries_start;
  695. cifs_dir_info_to_fattr(&fattr,
  696. (FILE_DIRECTORY_INFO *)data, cifs_sb);
  697. fattr.cf_uniqueid = le64_to_cpu(
  698. ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
  699. validinum = true;
  700. cifs_buf_release(srchinf->ntwrk_buf_start);
  701. }
  702. kfree(srchinf);
  703. if (rc)
  704. goto cgii_exit;
  705. } else
  706. goto cgii_exit;
  707. /*
  708. * If an inode wasn't passed in, then get the inode number
  709. *
  710. * Is an i_ino of zero legal? Can we use that to check if the server
  711. * supports returning inode numbers? Are there other sanity checks we
  712. * can use to ensure that the server is really filling in that field?
  713. */
  714. if (*inode == NULL) {
  715. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  716. if (validinum == false) {
  717. if (server->ops->get_srv_inum)
  718. tmprc = server->ops->get_srv_inum(xid,
  719. tcon, cifs_sb, full_path,
  720. &fattr.cf_uniqueid, data);
  721. if (tmprc) {
  722. cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
  723. tmprc);
  724. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  725. cifs_autodisable_serverino(cifs_sb);
  726. }
  727. }
  728. } else
  729. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  730. } else {
  731. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
  732. validinum == false && server->ops->get_srv_inum) {
  733. /*
  734. * Pass a NULL tcon to ensure we don't make a round
  735. * trip to the server. This only works for SMB2+.
  736. */
  737. tmprc = server->ops->get_srv_inum(xid,
  738. NULL, cifs_sb, full_path,
  739. &fattr.cf_uniqueid, data);
  740. if (tmprc)
  741. fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
  742. } else
  743. fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
  744. }
  745. /* query for SFU type info if supported and needed */
  746. if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
  747. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  748. tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
  749. if (tmprc)
  750. cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
  751. }
  752. #ifdef CONFIG_CIFS_ACL
  753. /* fill in 0777 bits from ACL */
  754. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  755. rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
  756. if (rc) {
  757. cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
  758. __func__, rc);
  759. goto cgii_exit;
  760. }
  761. }
  762. #endif /* CONFIG_CIFS_ACL */
  763. /* fill in remaining high mode bits e.g. SUID, VTX */
  764. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  765. cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
  766. /* check for Minshall+French symlinks */
  767. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  768. tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  769. full_path);
  770. if (tmprc)
  771. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  772. }
  773. if (!*inode) {
  774. *inode = cifs_iget(sb, &fattr);
  775. if (!*inode)
  776. rc = -ENOMEM;
  777. } else {
  778. /* we already have inode, update it */
  779. /* if uniqueid is different, return error */
  780. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  781. CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
  782. rc = -ESTALE;
  783. goto cgii_exit;
  784. }
  785. /* if filetype is different, return error */
  786. if (unlikely(((*inode)->i_mode & S_IFMT) !=
  787. (fattr.cf_mode & S_IFMT))) {
  788. rc = -ESTALE;
  789. goto cgii_exit;
  790. }
  791. cifs_fattr_to_inode(*inode, &fattr);
  792. }
  793. cgii_exit:
  794. kfree(buf);
  795. cifs_put_tlink(tlink);
  796. return rc;
  797. }
  798. static const struct inode_operations cifs_ipc_inode_ops = {
  799. .lookup = cifs_lookup,
  800. };
  801. static int
  802. cifs_find_inode(struct inode *inode, void *opaque)
  803. {
  804. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  805. /* don't match inode with different uniqueid */
  806. if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
  807. return 0;
  808. /* use createtime like an i_generation field */
  809. if (CIFS_I(inode)->createtime != fattr->cf_createtime)
  810. return 0;
  811. /* don't match inode of different type */
  812. if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
  813. return 0;
  814. /* if it's not a directory or has no dentries, then flag it */
  815. if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
  816. fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
  817. return 1;
  818. }
  819. static int
  820. cifs_init_inode(struct inode *inode, void *opaque)
  821. {
  822. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  823. CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
  824. CIFS_I(inode)->createtime = fattr->cf_createtime;
  825. return 0;
  826. }
  827. /*
  828. * walk dentry list for an inode and report whether it has aliases that
  829. * are hashed. We use this to determine if a directory inode can actually
  830. * be used.
  831. */
  832. static bool
  833. inode_has_hashed_dentries(struct inode *inode)
  834. {
  835. struct dentry *dentry;
  836. spin_lock(&inode->i_lock);
  837. hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
  838. if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
  839. spin_unlock(&inode->i_lock);
  840. return true;
  841. }
  842. }
  843. spin_unlock(&inode->i_lock);
  844. return false;
  845. }
  846. /* Given fattrs, get a corresponding inode */
  847. struct inode *
  848. cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
  849. {
  850. unsigned long hash;
  851. struct inode *inode;
  852. retry_iget5_locked:
  853. cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
  854. /* hash down to 32-bits on 32-bit arch */
  855. hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
  856. inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
  857. if (inode) {
  858. /* was there a potentially problematic inode collision? */
  859. if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
  860. fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
  861. if (inode_has_hashed_dentries(inode)) {
  862. cifs_autodisable_serverino(CIFS_SB(sb));
  863. iput(inode);
  864. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  865. goto retry_iget5_locked;
  866. }
  867. }
  868. cifs_fattr_to_inode(inode, fattr);
  869. if (sb->s_flags & SB_NOATIME)
  870. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  871. if (inode->i_state & I_NEW) {
  872. inode->i_ino = hash;
  873. #ifdef CONFIG_CIFS_FSCACHE
  874. /* initialize per-inode cache cookie pointer */
  875. CIFS_I(inode)->fscache = NULL;
  876. #endif
  877. unlock_new_inode(inode);
  878. }
  879. }
  880. return inode;
  881. }
  882. /* gets root inode */
  883. struct inode *cifs_root_iget(struct super_block *sb)
  884. {
  885. unsigned int xid;
  886. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  887. struct inode *inode = NULL;
  888. long rc;
  889. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  890. char *path = NULL;
  891. int len;
  892. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
  893. && cifs_sb->prepath) {
  894. len = strlen(cifs_sb->prepath);
  895. path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
  896. if (path == NULL)
  897. return ERR_PTR(-ENOMEM);
  898. path[0] = '/';
  899. memcpy(path+1, cifs_sb->prepath, len);
  900. } else {
  901. path = kstrdup("", GFP_KERNEL);
  902. if (path == NULL)
  903. return ERR_PTR(-ENOMEM);
  904. }
  905. xid = get_xid();
  906. if (tcon->unix_ext) {
  907. rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
  908. /* some servers mistakenly claim POSIX support */
  909. if (rc != -EOPNOTSUPP)
  910. goto iget_no_retry;
  911. cifs_dbg(VFS, "server does not support POSIX extensions");
  912. tcon->unix_ext = false;
  913. }
  914. convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
  915. rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
  916. iget_no_retry:
  917. if (!inode) {
  918. inode = ERR_PTR(rc);
  919. goto out;
  920. }
  921. #ifdef CONFIG_CIFS_FSCACHE
  922. /* populate tcon->resource_id */
  923. tcon->resource_id = CIFS_I(inode)->uniqueid;
  924. #endif
  925. if (rc && tcon->ipc) {
  926. cifs_dbg(FYI, "ipc connection - fake read inode\n");
  927. spin_lock(&inode->i_lock);
  928. inode->i_mode |= S_IFDIR;
  929. set_nlink(inode, 2);
  930. inode->i_op = &cifs_ipc_inode_ops;
  931. inode->i_fop = &simple_dir_operations;
  932. inode->i_uid = cifs_sb->mnt_uid;
  933. inode->i_gid = cifs_sb->mnt_gid;
  934. spin_unlock(&inode->i_lock);
  935. } else if (rc) {
  936. iget_failed(inode);
  937. inode = ERR_PTR(rc);
  938. }
  939. out:
  940. kfree(path);
  941. /* can not call macro free_xid here since in a void func
  942. * TODO: This is no longer true
  943. */
  944. _free_xid(xid);
  945. return inode;
  946. }
  947. int
  948. cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
  949. char *full_path, __u32 dosattr)
  950. {
  951. bool set_time = false;
  952. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  953. struct TCP_Server_Info *server;
  954. FILE_BASIC_INFO info_buf;
  955. if (attrs == NULL)
  956. return -EINVAL;
  957. server = cifs_sb_master_tcon(cifs_sb)->ses->server;
  958. if (!server->ops->set_file_info)
  959. return -ENOSYS;
  960. if (attrs->ia_valid & ATTR_ATIME) {
  961. set_time = true;
  962. info_buf.LastAccessTime =
  963. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  964. } else
  965. info_buf.LastAccessTime = 0;
  966. if (attrs->ia_valid & ATTR_MTIME) {
  967. set_time = true;
  968. info_buf.LastWriteTime =
  969. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  970. } else
  971. info_buf.LastWriteTime = 0;
  972. /*
  973. * Samba throws this field away, but windows may actually use it.
  974. * Do not set ctime unless other time stamps are changed explicitly
  975. * (i.e. by utimes()) since we would then have a mix of client and
  976. * server times.
  977. */
  978. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  979. cifs_dbg(FYI, "CIFS - CTIME changed\n");
  980. info_buf.ChangeTime =
  981. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  982. } else
  983. info_buf.ChangeTime = 0;
  984. info_buf.CreationTime = 0; /* don't change */
  985. info_buf.Attributes = cpu_to_le32(dosattr);
  986. return server->ops->set_file_info(inode, full_path, &info_buf, xid);
  987. }
  988. /*
  989. * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
  990. * and rename it to a random name that hopefully won't conflict with
  991. * anything else.
  992. */
  993. int
  994. cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
  995. const unsigned int xid)
  996. {
  997. int oplock = 0;
  998. int rc;
  999. struct cifs_fid fid;
  1000. struct cifs_open_parms oparms;
  1001. struct inode *inode = d_inode(dentry);
  1002. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1003. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1004. struct tcon_link *tlink;
  1005. struct cifs_tcon *tcon;
  1006. __u32 dosattr, origattr;
  1007. FILE_BASIC_INFO *info_buf = NULL;
  1008. tlink = cifs_sb_tlink(cifs_sb);
  1009. if (IS_ERR(tlink))
  1010. return PTR_ERR(tlink);
  1011. tcon = tlink_tcon(tlink);
  1012. /*
  1013. * We cannot rename the file if the server doesn't support
  1014. * CAP_INFOLEVEL_PASSTHRU
  1015. */
  1016. if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
  1017. rc = -EBUSY;
  1018. goto out;
  1019. }
  1020. oparms.tcon = tcon;
  1021. oparms.cifs_sb = cifs_sb;
  1022. oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
  1023. oparms.create_options = CREATE_NOT_DIR;
  1024. oparms.disposition = FILE_OPEN;
  1025. oparms.path = full_path;
  1026. oparms.fid = &fid;
  1027. oparms.reconnect = false;
  1028. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1029. if (rc != 0)
  1030. goto out;
  1031. origattr = cifsInode->cifsAttrs;
  1032. if (origattr == 0)
  1033. origattr |= ATTR_NORMAL;
  1034. dosattr = origattr & ~ATTR_READONLY;
  1035. if (dosattr == 0)
  1036. dosattr |= ATTR_NORMAL;
  1037. dosattr |= ATTR_HIDDEN;
  1038. /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
  1039. if (dosattr != origattr) {
  1040. info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
  1041. if (info_buf == NULL) {
  1042. rc = -ENOMEM;
  1043. goto out_close;
  1044. }
  1045. info_buf->Attributes = cpu_to_le32(dosattr);
  1046. rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
  1047. current->tgid);
  1048. /* although we would like to mark the file hidden
  1049. if that fails we will still try to rename it */
  1050. if (!rc)
  1051. cifsInode->cifsAttrs = dosattr;
  1052. else
  1053. dosattr = origattr; /* since not able to change them */
  1054. }
  1055. /* rename the file */
  1056. rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
  1057. cifs_sb->local_nls,
  1058. cifs_remap(cifs_sb));
  1059. if (rc != 0) {
  1060. rc = -EBUSY;
  1061. goto undo_setattr;
  1062. }
  1063. /* try to set DELETE_ON_CLOSE */
  1064. if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
  1065. rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
  1066. current->tgid);
  1067. /*
  1068. * some samba versions return -ENOENT when we try to set the
  1069. * file disposition here. Likely a samba bug, but work around
  1070. * it for now. This means that some cifsXXX files may hang
  1071. * around after they shouldn't.
  1072. *
  1073. * BB: remove this hack after more servers have the fix
  1074. */
  1075. if (rc == -ENOENT)
  1076. rc = 0;
  1077. else if (rc != 0) {
  1078. rc = -EBUSY;
  1079. goto undo_rename;
  1080. }
  1081. set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
  1082. }
  1083. out_close:
  1084. CIFSSMBClose(xid, tcon, fid.netfid);
  1085. out:
  1086. kfree(info_buf);
  1087. cifs_put_tlink(tlink);
  1088. return rc;
  1089. /*
  1090. * reset everything back to the original state. Don't bother
  1091. * dealing with errors here since we can't do anything about
  1092. * them anyway.
  1093. */
  1094. undo_rename:
  1095. CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
  1096. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1097. undo_setattr:
  1098. if (dosattr != origattr) {
  1099. info_buf->Attributes = cpu_to_le32(origattr);
  1100. if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
  1101. current->tgid))
  1102. cifsInode->cifsAttrs = origattr;
  1103. }
  1104. goto out_close;
  1105. }
  1106. /* copied from fs/nfs/dir.c with small changes */
  1107. static void
  1108. cifs_drop_nlink(struct inode *inode)
  1109. {
  1110. spin_lock(&inode->i_lock);
  1111. if (inode->i_nlink > 0)
  1112. drop_nlink(inode);
  1113. spin_unlock(&inode->i_lock);
  1114. }
  1115. /*
  1116. * If d_inode(dentry) is null (usually meaning the cached dentry
  1117. * is a negative dentry) then we would attempt a standard SMB delete, but
  1118. * if that fails we can not attempt the fall back mechanisms on EACCESS
  1119. * but will return the EACCESS to the caller. Note that the VFS does not call
  1120. * unlink on negative dentries currently.
  1121. */
  1122. int cifs_unlink(struct inode *dir, struct dentry *dentry)
  1123. {
  1124. int rc = 0;
  1125. unsigned int xid;
  1126. char *full_path = NULL;
  1127. struct inode *inode = d_inode(dentry);
  1128. struct cifsInodeInfo *cifs_inode;
  1129. struct super_block *sb = dir->i_sb;
  1130. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  1131. struct tcon_link *tlink;
  1132. struct cifs_tcon *tcon;
  1133. struct TCP_Server_Info *server;
  1134. struct iattr *attrs = NULL;
  1135. __u32 dosattr = 0, origattr = 0;
  1136. cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
  1137. tlink = cifs_sb_tlink(cifs_sb);
  1138. if (IS_ERR(tlink))
  1139. return PTR_ERR(tlink);
  1140. tcon = tlink_tcon(tlink);
  1141. server = tcon->ses->server;
  1142. xid = get_xid();
  1143. /* Unlink can be called from rename so we can not take the
  1144. * sb->s_vfs_rename_mutex here */
  1145. full_path = build_path_from_dentry(dentry);
  1146. if (full_path == NULL) {
  1147. rc = -ENOMEM;
  1148. goto unlink_out;
  1149. }
  1150. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1151. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1152. rc = CIFSPOSIXDelFile(xid, tcon, full_path,
  1153. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  1154. cifs_remap(cifs_sb));
  1155. cifs_dbg(FYI, "posix del rc %d\n", rc);
  1156. if ((rc == 0) || (rc == -ENOENT))
  1157. goto psx_del_no_retry;
  1158. }
  1159. retry_std_delete:
  1160. if (!server->ops->unlink) {
  1161. rc = -ENOSYS;
  1162. goto psx_del_no_retry;
  1163. }
  1164. rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
  1165. psx_del_no_retry:
  1166. if (!rc) {
  1167. if (inode)
  1168. cifs_drop_nlink(inode);
  1169. } else if (rc == -ENOENT) {
  1170. d_drop(dentry);
  1171. } else if (rc == -EBUSY) {
  1172. if (server->ops->rename_pending_delete) {
  1173. rc = server->ops->rename_pending_delete(full_path,
  1174. dentry, xid);
  1175. if (rc == 0)
  1176. cifs_drop_nlink(inode);
  1177. }
  1178. } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
  1179. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  1180. if (attrs == NULL) {
  1181. rc = -ENOMEM;
  1182. goto out_reval;
  1183. }
  1184. /* try to reset dos attributes */
  1185. cifs_inode = CIFS_I(inode);
  1186. origattr = cifs_inode->cifsAttrs;
  1187. if (origattr == 0)
  1188. origattr |= ATTR_NORMAL;
  1189. dosattr = origattr & ~ATTR_READONLY;
  1190. if (dosattr == 0)
  1191. dosattr |= ATTR_NORMAL;
  1192. dosattr |= ATTR_HIDDEN;
  1193. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1194. if (rc != 0)
  1195. goto out_reval;
  1196. goto retry_std_delete;
  1197. }
  1198. /* undo the setattr if we errored out and it's needed */
  1199. if (rc != 0 && dosattr != 0)
  1200. cifs_set_file_info(inode, attrs, xid, full_path, origattr);
  1201. out_reval:
  1202. if (inode) {
  1203. cifs_inode = CIFS_I(inode);
  1204. cifs_inode->time = 0; /* will force revalidate to get info
  1205. when needed */
  1206. inode->i_ctime = current_time(inode);
  1207. }
  1208. dir->i_ctime = dir->i_mtime = current_time(dir);
  1209. cifs_inode = CIFS_I(dir);
  1210. CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
  1211. unlink_out:
  1212. kfree(full_path);
  1213. kfree(attrs);
  1214. free_xid(xid);
  1215. cifs_put_tlink(tlink);
  1216. return rc;
  1217. }
  1218. static int
  1219. cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
  1220. const char *full_path, struct cifs_sb_info *cifs_sb,
  1221. struct cifs_tcon *tcon, const unsigned int xid)
  1222. {
  1223. int rc = 0;
  1224. struct inode *inode = NULL;
  1225. if (tcon->unix_ext)
  1226. rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
  1227. xid);
  1228. else
  1229. rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
  1230. xid, NULL);
  1231. if (rc)
  1232. return rc;
  1233. /*
  1234. * setting nlink not necessary except in cases where we failed to get it
  1235. * from the server or was set bogus. Also, since this is a brand new
  1236. * inode, no need to grab the i_lock before setting the i_nlink.
  1237. */
  1238. if (inode->i_nlink < 2)
  1239. set_nlink(inode, 2);
  1240. mode &= ~current_umask();
  1241. /* must turn on setgid bit if parent dir has it */
  1242. if (parent->i_mode & S_ISGID)
  1243. mode |= S_ISGID;
  1244. if (tcon->unix_ext) {
  1245. struct cifs_unix_set_info_args args = {
  1246. .mode = mode,
  1247. .ctime = NO_CHANGE_64,
  1248. .atime = NO_CHANGE_64,
  1249. .mtime = NO_CHANGE_64,
  1250. .device = 0,
  1251. };
  1252. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1253. args.uid = current_fsuid();
  1254. if (parent->i_mode & S_ISGID)
  1255. args.gid = parent->i_gid;
  1256. else
  1257. args.gid = current_fsgid();
  1258. } else {
  1259. args.uid = INVALID_UID; /* no change */
  1260. args.gid = INVALID_GID; /* no change */
  1261. }
  1262. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  1263. cifs_sb->local_nls,
  1264. cifs_remap(cifs_sb));
  1265. } else {
  1266. struct TCP_Server_Info *server = tcon->ses->server;
  1267. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1268. (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
  1269. server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
  1270. tcon, xid);
  1271. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  1272. inode->i_mode = (mode | S_IFDIR);
  1273. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1274. inode->i_uid = current_fsuid();
  1275. if (inode->i_mode & S_ISGID)
  1276. inode->i_gid = parent->i_gid;
  1277. else
  1278. inode->i_gid = current_fsgid();
  1279. }
  1280. }
  1281. d_instantiate(dentry, inode);
  1282. return rc;
  1283. }
  1284. static int
  1285. cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
  1286. const char *full_path, struct cifs_sb_info *cifs_sb,
  1287. struct cifs_tcon *tcon, const unsigned int xid)
  1288. {
  1289. int rc = 0;
  1290. u32 oplock = 0;
  1291. FILE_UNIX_BASIC_INFO *info = NULL;
  1292. struct inode *newinode = NULL;
  1293. struct cifs_fattr fattr;
  1294. info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  1295. if (info == NULL) {
  1296. rc = -ENOMEM;
  1297. goto posix_mkdir_out;
  1298. }
  1299. mode &= ~current_umask();
  1300. rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
  1301. NULL /* netfid */, info, &oplock, full_path,
  1302. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1303. if (rc == -EOPNOTSUPP)
  1304. goto posix_mkdir_out;
  1305. else if (rc) {
  1306. cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
  1307. d_drop(dentry);
  1308. goto posix_mkdir_out;
  1309. }
  1310. if (info->Type == cpu_to_le32(-1))
  1311. /* no return info, go query for it */
  1312. goto posix_mkdir_get_info;
  1313. /*
  1314. * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
  1315. * need to set uid/gid.
  1316. */
  1317. cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
  1318. cifs_fill_uniqueid(inode->i_sb, &fattr);
  1319. newinode = cifs_iget(inode->i_sb, &fattr);
  1320. if (!newinode)
  1321. goto posix_mkdir_get_info;
  1322. d_instantiate(dentry, newinode);
  1323. #ifdef CONFIG_CIFS_DEBUG2
  1324. cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
  1325. dentry, dentry, newinode);
  1326. if (newinode->i_nlink != 2)
  1327. cifs_dbg(FYI, "unexpected number of links %d\n",
  1328. newinode->i_nlink);
  1329. #endif
  1330. posix_mkdir_out:
  1331. kfree(info);
  1332. return rc;
  1333. posix_mkdir_get_info:
  1334. rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
  1335. xid);
  1336. goto posix_mkdir_out;
  1337. }
  1338. int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
  1339. {
  1340. int rc = 0;
  1341. unsigned int xid;
  1342. struct cifs_sb_info *cifs_sb;
  1343. struct tcon_link *tlink;
  1344. struct cifs_tcon *tcon;
  1345. struct TCP_Server_Info *server;
  1346. char *full_path;
  1347. cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
  1348. mode, inode);
  1349. cifs_sb = CIFS_SB(inode->i_sb);
  1350. tlink = cifs_sb_tlink(cifs_sb);
  1351. if (IS_ERR(tlink))
  1352. return PTR_ERR(tlink);
  1353. tcon = tlink_tcon(tlink);
  1354. xid = get_xid();
  1355. full_path = build_path_from_dentry(direntry);
  1356. if (full_path == NULL) {
  1357. rc = -ENOMEM;
  1358. goto mkdir_out;
  1359. }
  1360. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1361. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1362. rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
  1363. tcon, xid);
  1364. if (rc != -EOPNOTSUPP)
  1365. goto mkdir_out;
  1366. }
  1367. server = tcon->ses->server;
  1368. if (!server->ops->mkdir) {
  1369. rc = -ENOSYS;
  1370. goto mkdir_out;
  1371. }
  1372. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  1373. rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
  1374. if (rc) {
  1375. cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
  1376. d_drop(direntry);
  1377. goto mkdir_out;
  1378. }
  1379. rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
  1380. xid);
  1381. mkdir_out:
  1382. /*
  1383. * Force revalidate to get parent dir info when needed since cached
  1384. * attributes are invalid now.
  1385. */
  1386. CIFS_I(inode)->time = 0;
  1387. kfree(full_path);
  1388. free_xid(xid);
  1389. cifs_put_tlink(tlink);
  1390. return rc;
  1391. }
  1392. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  1393. {
  1394. int rc = 0;
  1395. unsigned int xid;
  1396. struct cifs_sb_info *cifs_sb;
  1397. struct tcon_link *tlink;
  1398. struct cifs_tcon *tcon;
  1399. struct TCP_Server_Info *server;
  1400. char *full_path = NULL;
  1401. struct cifsInodeInfo *cifsInode;
  1402. cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
  1403. xid = get_xid();
  1404. full_path = build_path_from_dentry(direntry);
  1405. if (full_path == NULL) {
  1406. rc = -ENOMEM;
  1407. goto rmdir_exit;
  1408. }
  1409. cifs_sb = CIFS_SB(inode->i_sb);
  1410. tlink = cifs_sb_tlink(cifs_sb);
  1411. if (IS_ERR(tlink)) {
  1412. rc = PTR_ERR(tlink);
  1413. goto rmdir_exit;
  1414. }
  1415. tcon = tlink_tcon(tlink);
  1416. server = tcon->ses->server;
  1417. if (!server->ops->rmdir) {
  1418. rc = -ENOSYS;
  1419. cifs_put_tlink(tlink);
  1420. goto rmdir_exit;
  1421. }
  1422. rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
  1423. cifs_put_tlink(tlink);
  1424. if (!rc) {
  1425. spin_lock(&d_inode(direntry)->i_lock);
  1426. i_size_write(d_inode(direntry), 0);
  1427. clear_nlink(d_inode(direntry));
  1428. spin_unlock(&d_inode(direntry)->i_lock);
  1429. }
  1430. cifsInode = CIFS_I(d_inode(direntry));
  1431. /* force revalidate to go get info when needed */
  1432. cifsInode->time = 0;
  1433. cifsInode = CIFS_I(inode);
  1434. /*
  1435. * Force revalidate to get parent dir info when needed since cached
  1436. * attributes are invalid now.
  1437. */
  1438. cifsInode->time = 0;
  1439. d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
  1440. current_time(inode);
  1441. rmdir_exit:
  1442. kfree(full_path);
  1443. free_xid(xid);
  1444. return rc;
  1445. }
  1446. static int
  1447. cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
  1448. const char *from_path, struct dentry *to_dentry,
  1449. const char *to_path)
  1450. {
  1451. struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
  1452. struct tcon_link *tlink;
  1453. struct cifs_tcon *tcon;
  1454. struct TCP_Server_Info *server;
  1455. struct cifs_fid fid;
  1456. struct cifs_open_parms oparms;
  1457. int oplock, rc;
  1458. tlink = cifs_sb_tlink(cifs_sb);
  1459. if (IS_ERR(tlink))
  1460. return PTR_ERR(tlink);
  1461. tcon = tlink_tcon(tlink);
  1462. server = tcon->ses->server;
  1463. if (!server->ops->rename)
  1464. return -ENOSYS;
  1465. /* try path-based rename first */
  1466. rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
  1467. /*
  1468. * Don't bother with rename by filehandle unless file is busy and
  1469. * source. Note that cross directory moves do not work with
  1470. * rename by filehandle to various Windows servers.
  1471. */
  1472. if (rc == 0 || rc != -EBUSY)
  1473. goto do_rename_exit;
  1474. /* open-file renames don't work across directories */
  1475. if (to_dentry->d_parent != from_dentry->d_parent)
  1476. goto do_rename_exit;
  1477. oparms.tcon = tcon;
  1478. oparms.cifs_sb = cifs_sb;
  1479. /* open the file to be renamed -- we need DELETE perms */
  1480. oparms.desired_access = DELETE;
  1481. oparms.create_options = CREATE_NOT_DIR;
  1482. oparms.disposition = FILE_OPEN;
  1483. oparms.path = from_path;
  1484. oparms.fid = &fid;
  1485. oparms.reconnect = false;
  1486. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1487. if (rc == 0) {
  1488. rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
  1489. (const char *) to_dentry->d_name.name,
  1490. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1491. CIFSSMBClose(xid, tcon, fid.netfid);
  1492. }
  1493. do_rename_exit:
  1494. cifs_put_tlink(tlink);
  1495. return rc;
  1496. }
  1497. int
  1498. cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
  1499. struct inode *target_dir, struct dentry *target_dentry,
  1500. unsigned int flags)
  1501. {
  1502. char *from_name = NULL;
  1503. char *to_name = NULL;
  1504. struct cifs_sb_info *cifs_sb;
  1505. struct tcon_link *tlink;
  1506. struct cifs_tcon *tcon;
  1507. FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
  1508. FILE_UNIX_BASIC_INFO *info_buf_target;
  1509. unsigned int xid;
  1510. int rc, tmprc;
  1511. if (flags & ~RENAME_NOREPLACE)
  1512. return -EINVAL;
  1513. cifs_sb = CIFS_SB(source_dir->i_sb);
  1514. tlink = cifs_sb_tlink(cifs_sb);
  1515. if (IS_ERR(tlink))
  1516. return PTR_ERR(tlink);
  1517. tcon = tlink_tcon(tlink);
  1518. xid = get_xid();
  1519. /*
  1520. * we already have the rename sem so we do not need to
  1521. * grab it again here to protect the path integrity
  1522. */
  1523. from_name = build_path_from_dentry(source_dentry);
  1524. if (from_name == NULL) {
  1525. rc = -ENOMEM;
  1526. goto cifs_rename_exit;
  1527. }
  1528. to_name = build_path_from_dentry(target_dentry);
  1529. if (to_name == NULL) {
  1530. rc = -ENOMEM;
  1531. goto cifs_rename_exit;
  1532. }
  1533. rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
  1534. to_name);
  1535. /*
  1536. * No-replace is the natural behavior for CIFS, so skip unlink hacks.
  1537. */
  1538. if (flags & RENAME_NOREPLACE)
  1539. goto cifs_rename_exit;
  1540. if (rc == -EEXIST && tcon->unix_ext) {
  1541. /*
  1542. * Are src and dst hardlinks of same inode? We can only tell
  1543. * with unix extensions enabled.
  1544. */
  1545. info_buf_source =
  1546. kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
  1547. GFP_KERNEL);
  1548. if (info_buf_source == NULL) {
  1549. rc = -ENOMEM;
  1550. goto cifs_rename_exit;
  1551. }
  1552. info_buf_target = info_buf_source + 1;
  1553. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
  1554. info_buf_source,
  1555. cifs_sb->local_nls,
  1556. cifs_remap(cifs_sb));
  1557. if (tmprc != 0)
  1558. goto unlink_target;
  1559. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
  1560. info_buf_target,
  1561. cifs_sb->local_nls,
  1562. cifs_remap(cifs_sb));
  1563. if (tmprc == 0 && (info_buf_source->UniqueId ==
  1564. info_buf_target->UniqueId)) {
  1565. /* same file, POSIX says that this is a noop */
  1566. rc = 0;
  1567. goto cifs_rename_exit;
  1568. }
  1569. }
  1570. /*
  1571. * else ... BB we could add the same check for Windows by
  1572. * checking the UniqueId via FILE_INTERNAL_INFO
  1573. */
  1574. unlink_target:
  1575. /* Try unlinking the target dentry if it's not negative */
  1576. if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
  1577. if (d_is_dir(target_dentry))
  1578. tmprc = cifs_rmdir(target_dir, target_dentry);
  1579. else
  1580. tmprc = cifs_unlink(target_dir, target_dentry);
  1581. if (tmprc)
  1582. goto cifs_rename_exit;
  1583. rc = cifs_do_rename(xid, source_dentry, from_name,
  1584. target_dentry, to_name);
  1585. }
  1586. /* force revalidate to go get info when needed */
  1587. CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
  1588. source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
  1589. target_dir->i_mtime = current_time(source_dir);
  1590. cifs_rename_exit:
  1591. kfree(info_buf_source);
  1592. kfree(from_name);
  1593. kfree(to_name);
  1594. free_xid(xid);
  1595. cifs_put_tlink(tlink);
  1596. return rc;
  1597. }
  1598. static bool
  1599. cifs_inode_needs_reval(struct inode *inode)
  1600. {
  1601. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  1602. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1603. if (CIFS_CACHE_READ(cifs_i))
  1604. return false;
  1605. if (!lookupCacheEnabled)
  1606. return true;
  1607. if (cifs_i->time == 0)
  1608. return true;
  1609. if (!cifs_sb->actimeo)
  1610. return true;
  1611. if (!time_in_range(jiffies, cifs_i->time,
  1612. cifs_i->time + cifs_sb->actimeo))
  1613. return true;
  1614. /* hardlinked files w/ noserverino get "special" treatment */
  1615. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
  1616. S_ISREG(inode->i_mode) && inode->i_nlink != 1)
  1617. return true;
  1618. return false;
  1619. }
  1620. /*
  1621. * Zap the cache. Called when invalid_mapping flag is set.
  1622. */
  1623. int
  1624. cifs_invalidate_mapping(struct inode *inode)
  1625. {
  1626. int rc = 0;
  1627. if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
  1628. rc = invalidate_inode_pages2(inode->i_mapping);
  1629. if (rc)
  1630. cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
  1631. __func__, inode);
  1632. }
  1633. cifs_fscache_reset_inode_cookie(inode);
  1634. return rc;
  1635. }
  1636. /**
  1637. * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
  1638. * @word: long word containing the bit lock
  1639. */
  1640. static int
  1641. cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
  1642. {
  1643. freezable_schedule_unsafe();
  1644. if (signal_pending_state(mode, current))
  1645. return -ERESTARTSYS;
  1646. return 0;
  1647. }
  1648. int
  1649. cifs_revalidate_mapping(struct inode *inode)
  1650. {
  1651. int rc;
  1652. unsigned long *flags = &CIFS_I(inode)->flags;
  1653. rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
  1654. TASK_KILLABLE);
  1655. if (rc)
  1656. return rc;
  1657. if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
  1658. rc = cifs_invalidate_mapping(inode);
  1659. if (rc)
  1660. set_bit(CIFS_INO_INVALID_MAPPING, flags);
  1661. }
  1662. clear_bit_unlock(CIFS_INO_LOCK, flags);
  1663. smp_mb__after_atomic();
  1664. wake_up_bit(flags, CIFS_INO_LOCK);
  1665. return rc;
  1666. }
  1667. int
  1668. cifs_zap_mapping(struct inode *inode)
  1669. {
  1670. set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
  1671. return cifs_revalidate_mapping(inode);
  1672. }
  1673. int cifs_revalidate_file_attr(struct file *filp)
  1674. {
  1675. int rc = 0;
  1676. struct inode *inode = file_inode(filp);
  1677. struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
  1678. if (!cifs_inode_needs_reval(inode))
  1679. return rc;
  1680. if (tlink_tcon(cfile->tlink)->unix_ext)
  1681. rc = cifs_get_file_info_unix(filp);
  1682. else
  1683. rc = cifs_get_file_info(filp);
  1684. return rc;
  1685. }
  1686. int cifs_revalidate_dentry_attr(struct dentry *dentry)
  1687. {
  1688. unsigned int xid;
  1689. int rc = 0;
  1690. struct inode *inode = d_inode(dentry);
  1691. struct super_block *sb = dentry->d_sb;
  1692. char *full_path = NULL;
  1693. if (inode == NULL)
  1694. return -ENOENT;
  1695. if (!cifs_inode_needs_reval(inode))
  1696. return rc;
  1697. xid = get_xid();
  1698. /* can not safely grab the rename sem here if rename calls revalidate
  1699. since that would deadlock */
  1700. full_path = build_path_from_dentry(dentry);
  1701. if (full_path == NULL) {
  1702. rc = -ENOMEM;
  1703. goto out;
  1704. }
  1705. cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
  1706. full_path, inode, inode->i_count.counter,
  1707. dentry, cifs_get_time(dentry), jiffies);
  1708. if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
  1709. rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
  1710. else
  1711. rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
  1712. xid, NULL);
  1713. out:
  1714. kfree(full_path);
  1715. free_xid(xid);
  1716. return rc;
  1717. }
  1718. int cifs_revalidate_file(struct file *filp)
  1719. {
  1720. int rc;
  1721. struct inode *inode = file_inode(filp);
  1722. rc = cifs_revalidate_file_attr(filp);
  1723. if (rc)
  1724. return rc;
  1725. return cifs_revalidate_mapping(inode);
  1726. }
  1727. /* revalidate a dentry's inode attributes */
  1728. int cifs_revalidate_dentry(struct dentry *dentry)
  1729. {
  1730. int rc;
  1731. struct inode *inode = d_inode(dentry);
  1732. rc = cifs_revalidate_dentry_attr(dentry);
  1733. if (rc)
  1734. return rc;
  1735. return cifs_revalidate_mapping(inode);
  1736. }
  1737. int cifs_getattr(const struct path *path, struct kstat *stat,
  1738. u32 request_mask, unsigned int flags)
  1739. {
  1740. struct dentry *dentry = path->dentry;
  1741. struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
  1742. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  1743. struct inode *inode = d_inode(dentry);
  1744. int rc;
  1745. /*
  1746. * We need to be sure that all dirty pages are written and the server
  1747. * has actual ctime, mtime and file length.
  1748. */
  1749. if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
  1750. inode->i_mapping->nrpages != 0) {
  1751. rc = filemap_fdatawait(inode->i_mapping);
  1752. if (rc) {
  1753. mapping_set_error(inode->i_mapping, rc);
  1754. return rc;
  1755. }
  1756. }
  1757. rc = cifs_revalidate_dentry_attr(dentry);
  1758. if (rc)
  1759. return rc;
  1760. generic_fillattr(inode, stat);
  1761. stat->blksize = CIFS_MAX_MSGSIZE;
  1762. stat->ino = CIFS_I(inode)->uniqueid;
  1763. /* old CIFS Unix Extensions doesn't return create time */
  1764. if (CIFS_I(inode)->createtime) {
  1765. stat->result_mask |= STATX_BTIME;
  1766. stat->btime =
  1767. cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
  1768. }
  1769. stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
  1770. if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
  1771. stat->attributes |= STATX_ATTR_COMPRESSED;
  1772. if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
  1773. stat->attributes |= STATX_ATTR_ENCRYPTED;
  1774. /*
  1775. * If on a multiuser mount without unix extensions or cifsacl being
  1776. * enabled, and the admin hasn't overridden them, set the ownership
  1777. * to the fsuid/fsgid of the current process.
  1778. */
  1779. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
  1780. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1781. !tcon->unix_ext) {
  1782. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
  1783. stat->uid = current_fsuid();
  1784. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
  1785. stat->gid = current_fsgid();
  1786. }
  1787. return rc;
  1788. }
  1789. static int cifs_truncate_page(struct address_space *mapping, loff_t from)
  1790. {
  1791. pgoff_t index = from >> PAGE_SHIFT;
  1792. unsigned offset = from & (PAGE_SIZE - 1);
  1793. struct page *page;
  1794. int rc = 0;
  1795. page = grab_cache_page(mapping, index);
  1796. if (!page)
  1797. return -ENOMEM;
  1798. zero_user_segment(page, offset, PAGE_SIZE);
  1799. unlock_page(page);
  1800. put_page(page);
  1801. return rc;
  1802. }
  1803. static void cifs_setsize(struct inode *inode, loff_t offset)
  1804. {
  1805. spin_lock(&inode->i_lock);
  1806. i_size_write(inode, offset);
  1807. spin_unlock(&inode->i_lock);
  1808. truncate_pagecache(inode, offset);
  1809. }
  1810. static int
  1811. cifs_set_file_size(struct inode *inode, struct iattr *attrs,
  1812. unsigned int xid, char *full_path)
  1813. {
  1814. int rc;
  1815. struct cifsFileInfo *open_file;
  1816. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1817. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1818. struct tcon_link *tlink = NULL;
  1819. struct cifs_tcon *tcon = NULL;
  1820. struct TCP_Server_Info *server;
  1821. /*
  1822. * To avoid spurious oplock breaks from server, in the case of
  1823. * inodes that we already have open, avoid doing path based
  1824. * setting of file size if we can do it by handle.
  1825. * This keeps our caching token (oplock) and avoids timeouts
  1826. * when the local oplock break takes longer to flush
  1827. * writebehind data than the SMB timeout for the SetPathInfo
  1828. * request would allow
  1829. */
  1830. open_file = find_writable_file(cifsInode, true);
  1831. if (open_file) {
  1832. tcon = tlink_tcon(open_file->tlink);
  1833. server = tcon->ses->server;
  1834. if (server->ops->set_file_size)
  1835. rc = server->ops->set_file_size(xid, tcon, open_file,
  1836. attrs->ia_size, false);
  1837. else
  1838. rc = -ENOSYS;
  1839. cifsFileInfo_put(open_file);
  1840. cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
  1841. } else
  1842. rc = -EINVAL;
  1843. if (!rc)
  1844. goto set_size_out;
  1845. if (tcon == NULL) {
  1846. tlink = cifs_sb_tlink(cifs_sb);
  1847. if (IS_ERR(tlink))
  1848. return PTR_ERR(tlink);
  1849. tcon = tlink_tcon(tlink);
  1850. server = tcon->ses->server;
  1851. }
  1852. /*
  1853. * Set file size by pathname rather than by handle either because no
  1854. * valid, writeable file handle for it was found or because there was
  1855. * an error setting it by handle.
  1856. */
  1857. if (server->ops->set_path_size)
  1858. rc = server->ops->set_path_size(xid, tcon, full_path,
  1859. attrs->ia_size, cifs_sb, false);
  1860. else
  1861. rc = -ENOSYS;
  1862. cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
  1863. if (tlink)
  1864. cifs_put_tlink(tlink);
  1865. set_size_out:
  1866. if (rc == 0) {
  1867. cifsInode->server_eof = attrs->ia_size;
  1868. cifs_setsize(inode, attrs->ia_size);
  1869. cifs_truncate_page(inode->i_mapping, inode->i_size);
  1870. }
  1871. return rc;
  1872. }
  1873. static int
  1874. cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
  1875. {
  1876. int rc;
  1877. unsigned int xid;
  1878. char *full_path = NULL;
  1879. struct inode *inode = d_inode(direntry);
  1880. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1881. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1882. struct tcon_link *tlink;
  1883. struct cifs_tcon *pTcon;
  1884. struct cifs_unix_set_info_args *args = NULL;
  1885. struct cifsFileInfo *open_file;
  1886. cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
  1887. direntry, attrs->ia_valid);
  1888. xid = get_xid();
  1889. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  1890. attrs->ia_valid |= ATTR_FORCE;
  1891. rc = setattr_prepare(direntry, attrs);
  1892. if (rc < 0)
  1893. goto out;
  1894. full_path = build_path_from_dentry(direntry);
  1895. if (full_path == NULL) {
  1896. rc = -ENOMEM;
  1897. goto out;
  1898. }
  1899. /*
  1900. * Attempt to flush data before changing attributes. We need to do
  1901. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  1902. * ownership or mode then we may also need to do this. Here, we take
  1903. * the safe way out and just do the flush on all setattr requests. If
  1904. * the flush returns error, store it to report later and continue.
  1905. *
  1906. * BB: This should be smarter. Why bother flushing pages that
  1907. * will be truncated anyway? Also, should we error out here if
  1908. * the flush returns error?
  1909. */
  1910. rc = filemap_write_and_wait(inode->i_mapping);
  1911. mapping_set_error(inode->i_mapping, rc);
  1912. rc = 0;
  1913. if (attrs->ia_valid & ATTR_SIZE) {
  1914. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1915. if (rc != 0)
  1916. goto out;
  1917. }
  1918. /* skip mode change if it's just for clearing setuid/setgid */
  1919. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1920. attrs->ia_valid &= ~ATTR_MODE;
  1921. args = kmalloc(sizeof(*args), GFP_KERNEL);
  1922. if (args == NULL) {
  1923. rc = -ENOMEM;
  1924. goto out;
  1925. }
  1926. /* set up the struct */
  1927. if (attrs->ia_valid & ATTR_MODE)
  1928. args->mode = attrs->ia_mode;
  1929. else
  1930. args->mode = NO_CHANGE_64;
  1931. if (attrs->ia_valid & ATTR_UID)
  1932. args->uid = attrs->ia_uid;
  1933. else
  1934. args->uid = INVALID_UID; /* no change */
  1935. if (attrs->ia_valid & ATTR_GID)
  1936. args->gid = attrs->ia_gid;
  1937. else
  1938. args->gid = INVALID_GID; /* no change */
  1939. if (attrs->ia_valid & ATTR_ATIME)
  1940. args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
  1941. else
  1942. args->atime = NO_CHANGE_64;
  1943. if (attrs->ia_valid & ATTR_MTIME)
  1944. args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
  1945. else
  1946. args->mtime = NO_CHANGE_64;
  1947. if (attrs->ia_valid & ATTR_CTIME)
  1948. args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
  1949. else
  1950. args->ctime = NO_CHANGE_64;
  1951. args->device = 0;
  1952. open_file = find_writable_file(cifsInode, true);
  1953. if (open_file) {
  1954. u16 nfid = open_file->fid.netfid;
  1955. u32 npid = open_file->pid;
  1956. pTcon = tlink_tcon(open_file->tlink);
  1957. rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
  1958. cifsFileInfo_put(open_file);
  1959. } else {
  1960. tlink = cifs_sb_tlink(cifs_sb);
  1961. if (IS_ERR(tlink)) {
  1962. rc = PTR_ERR(tlink);
  1963. goto out;
  1964. }
  1965. pTcon = tlink_tcon(tlink);
  1966. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
  1967. cifs_sb->local_nls,
  1968. cifs_remap(cifs_sb));
  1969. cifs_put_tlink(tlink);
  1970. }
  1971. if (rc)
  1972. goto out;
  1973. if ((attrs->ia_valid & ATTR_SIZE) &&
  1974. attrs->ia_size != i_size_read(inode))
  1975. truncate_setsize(inode, attrs->ia_size);
  1976. setattr_copy(inode, attrs);
  1977. mark_inode_dirty(inode);
  1978. /* force revalidate when any of these times are set since some
  1979. of the fs types (eg ext3, fat) do not have fine enough
  1980. time granularity to match protocol, and we do not have a
  1981. a way (yet) to query the server fs's time granularity (and
  1982. whether it rounds times down).
  1983. */
  1984. if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
  1985. cifsInode->time = 0;
  1986. out:
  1987. kfree(args);
  1988. kfree(full_path);
  1989. free_xid(xid);
  1990. return rc;
  1991. }
  1992. static int
  1993. cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
  1994. {
  1995. unsigned int xid;
  1996. kuid_t uid = INVALID_UID;
  1997. kgid_t gid = INVALID_GID;
  1998. struct inode *inode = d_inode(direntry);
  1999. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2000. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  2001. char *full_path = NULL;
  2002. int rc = -EACCES;
  2003. __u32 dosattr = 0;
  2004. __u64 mode = NO_CHANGE_64;
  2005. xid = get_xid();
  2006. cifs_dbg(FYI, "setattr on file %pd attrs->iavalid 0x%x\n",
  2007. direntry, attrs->ia_valid);
  2008. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  2009. attrs->ia_valid |= ATTR_FORCE;
  2010. rc = setattr_prepare(direntry, attrs);
  2011. if (rc < 0) {
  2012. free_xid(xid);
  2013. return rc;
  2014. }
  2015. full_path = build_path_from_dentry(direntry);
  2016. if (full_path == NULL) {
  2017. rc = -ENOMEM;
  2018. free_xid(xid);
  2019. return rc;
  2020. }
  2021. /*
  2022. * Attempt to flush data before changing attributes. We need to do
  2023. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  2024. * ownership or mode then we may also need to do this. Here, we take
  2025. * the safe way out and just do the flush on all setattr requests. If
  2026. * the flush returns error, store it to report later and continue.
  2027. *
  2028. * BB: This should be smarter. Why bother flushing pages that
  2029. * will be truncated anyway? Also, should we error out here if
  2030. * the flush returns error?
  2031. */
  2032. rc = filemap_write_and_wait(inode->i_mapping);
  2033. mapping_set_error(inode->i_mapping, rc);
  2034. rc = 0;
  2035. if (attrs->ia_valid & ATTR_SIZE) {
  2036. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  2037. if (rc != 0)
  2038. goto cifs_setattr_exit;
  2039. }
  2040. if (attrs->ia_valid & ATTR_UID)
  2041. uid = attrs->ia_uid;
  2042. if (attrs->ia_valid & ATTR_GID)
  2043. gid = attrs->ia_gid;
  2044. #ifdef CONFIG_CIFS_ACL
  2045. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  2046. if (uid_valid(uid) || gid_valid(gid)) {
  2047. rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
  2048. uid, gid);
  2049. if (rc) {
  2050. cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
  2051. __func__, rc);
  2052. goto cifs_setattr_exit;
  2053. }
  2054. }
  2055. } else
  2056. #endif /* CONFIG_CIFS_ACL */
  2057. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
  2058. attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
  2059. /* skip mode change if it's just for clearing setuid/setgid */
  2060. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  2061. attrs->ia_valid &= ~ATTR_MODE;
  2062. if (attrs->ia_valid & ATTR_MODE) {
  2063. mode = attrs->ia_mode;
  2064. rc = 0;
  2065. #ifdef CONFIG_CIFS_ACL
  2066. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  2067. rc = id_mode_to_cifs_acl(inode, full_path, mode,
  2068. INVALID_UID, INVALID_GID);
  2069. if (rc) {
  2070. cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
  2071. __func__, rc);
  2072. goto cifs_setattr_exit;
  2073. }
  2074. } else
  2075. #endif /* CONFIG_CIFS_ACL */
  2076. if (((mode & S_IWUGO) == 0) &&
  2077. (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  2078. dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
  2079. /* fix up mode if we're not using dynperm */
  2080. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  2081. attrs->ia_mode = inode->i_mode & ~S_IWUGO;
  2082. } else if ((mode & S_IWUGO) &&
  2083. (cifsInode->cifsAttrs & ATTR_READONLY)) {
  2084. dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
  2085. /* Attributes of 0 are ignored */
  2086. if (dosattr == 0)
  2087. dosattr |= ATTR_NORMAL;
  2088. /* reset local inode permissions to normal */
  2089. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  2090. attrs->ia_mode &= ~(S_IALLUGO);
  2091. if (S_ISDIR(inode->i_mode))
  2092. attrs->ia_mode |=
  2093. cifs_sb->mnt_dir_mode;
  2094. else
  2095. attrs->ia_mode |=
  2096. cifs_sb->mnt_file_mode;
  2097. }
  2098. } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  2099. /* ignore mode change - ATTR_READONLY hasn't changed */
  2100. attrs->ia_valid &= ~ATTR_MODE;
  2101. }
  2102. }
  2103. if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
  2104. ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
  2105. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  2106. /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
  2107. /* Even if error on time set, no sense failing the call if
  2108. the server would set the time to a reasonable value anyway,
  2109. and this check ensures that we are not being called from
  2110. sys_utimes in which case we ought to fail the call back to
  2111. the user when the server rejects the call */
  2112. if ((rc) && (attrs->ia_valid &
  2113. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  2114. rc = 0;
  2115. }
  2116. /* do not need local check to inode_check_ok since the server does
  2117. that */
  2118. if (rc)
  2119. goto cifs_setattr_exit;
  2120. if ((attrs->ia_valid & ATTR_SIZE) &&
  2121. attrs->ia_size != i_size_read(inode))
  2122. truncate_setsize(inode, attrs->ia_size);
  2123. setattr_copy(inode, attrs);
  2124. mark_inode_dirty(inode);
  2125. cifs_setattr_exit:
  2126. kfree(full_path);
  2127. free_xid(xid);
  2128. return rc;
  2129. }
  2130. int
  2131. cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  2132. {
  2133. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  2134. struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
  2135. if (pTcon->unix_ext)
  2136. return cifs_setattr_unix(direntry, attrs);
  2137. return cifs_setattr_nounix(direntry, attrs);
  2138. /* BB: add cifs_setattr_legacy for really old servers */
  2139. }
  2140. #if 0
  2141. void cifs_delete_inode(struct inode *inode)
  2142. {
  2143. cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
  2144. /* may have to add back in if and when safe distributed caching of
  2145. directories added e.g. via FindNotify */
  2146. }
  2147. #endif